Generated import status rather than individual statuses.

This commit is contained in:
Adam Kelly 2020-03-25 14:14:38 +00:00
parent 323c7f8dbf
commit 8bf3225fc4
4 changed files with 45 additions and 26 deletions

View file

@ -11,6 +11,7 @@ GOODREADS_SHELVES = {
'currently-reading': 'reading',
'to-read': 'to-read',
}
MAX_ENTRIES = 20
def unquote_string(text):
match = re.match(r'="([^"]*)"', text)
@ -32,7 +33,7 @@ class GoodreadsCsv(object):
self.reader = csv.DictReader(csv_file)
def __iter__(self):
for line in itertools.islice(self.reader, 30):
for line in itertools.islice(self.reader, MAX_ENTRIES):
entry = GoodreadsItem(line)
try:
entry.resolve()
@ -69,3 +70,6 @@ class GoodreadsItem(object):
def __repr__(self):
return "<GoodreadsItem {!r}>".format(self.line['Title'])
def __str__(self):
return "{} by {}".format(self.line['Title'], self.line['Author'])

View file

@ -157,6 +157,31 @@ def handle_unshelve(user, book, shelf):
broadcast(user, activity, recipients)
def handle_import_books(user, items):
new_books = []
for item in items:
if item.shelf:
desired_shelf = models.Shelf.objects.get(
identifier=item.shelf,
user=user
)
shelf, created = models.ShelfBook.objects.get_or_create(book=item.book, shelf=desired_shelf, added_by=user)
if created:
new_books.append(item.book)
activity = activitypub.get_add(user, item.book, desired_shelf)
recipients = get_recipients(user, 'public')
broadcast(user, activity, recipients)
if new_books:
message = 'imported {} books'.format(len(new_books))
status = create_status(user, message, mention_books=new_books)
status.status_type = 'Update'
status.save()
create_activity = activitypub.get_create(user, activitypub.get_status(status))
broadcast(user, create_activity, get_recipients(user, 'public'))
def handle_review(user, book, name, content, rating):
''' post a review '''
# validated and saves the review in the database so it has an id

View file

@ -2,26 +2,17 @@
{% block content %}
<div id="content">
<div>
<h1>Search results</h1>
{% for book in results %}
<div>
<a href="{{ book.absolute_id }}">
{% include 'snippets/book_cover.html' with book=book %}
</a>
{{ book.title }}
{{ book.author }}
{% include 'snippets/shelve_button.html' with book=book pulldown=True %}
</div>
{% endfor %}
<h1>The following books could not be imported: </h1>
<h1>Failures</h1>
<ul>
{% for book in failures %}
{% for item in failures %}
<li>
{{ book }}
{{ item }}
</li>
{% endfor %}
</ul>
<p>{{ success_count }} books imported successfully</p>
</div>
</div>
{% endblock %}

View file

@ -299,18 +299,17 @@ def import_data(request):
failures = []
for item in GoodreadsCsv(TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding)):
if item.book:
results.append(item.book)
if item.shelf:
desired_shelf = models.Shelf.objects.get(
identifier=item.shelf,
user=request.user
)
outgoing.handle_shelve(request.user, item.book, desired_shelf)
results.append(item)
else:
failures.append(item)
return TemplateResponse(request, 'import_results.html', {
'results': results,
'failures': failures
})
outgoing.handle_import_books(request.user, results)
if failures:
return TemplateResponse(request, 'import_results.html', {
'success_count': len(results),
'failures': failures,
})
else:
return redirect('/')
else:
return HttpResponseBadRequest()