From 8bf3225fc43d420619155832e37d1433699477df Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Wed, 25 Mar 2020 14:14:38 +0000 Subject: [PATCH] Generated import status rather than individual statuses. --- fedireads/goodreads_import.py | 6 +++++- fedireads/outgoing.py | 25 +++++++++++++++++++++++++ fedireads/templates/import_results.html | 19 +++++-------------- fedireads/view_actions.py | 21 ++++++++++----------- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/fedireads/goodreads_import.py b/fedireads/goodreads_import.py index 3dd29ed1..5f5f8a7e 100644 --- a/fedireads/goodreads_import.py +++ b/fedireads/goodreads_import.py @@ -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 "".format(self.line['Title']) + + def __str__(self): + return "{} by {}".format(self.line['Title'], self.line['Author']) diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index b47f0fcd..a58ee5d6 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -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 diff --git a/fedireads/templates/import_results.html b/fedireads/templates/import_results.html index 3082878f..2994b6ff 100644 --- a/fedireads/templates/import_results.html +++ b/fedireads/templates/import_results.html @@ -2,26 +2,17 @@ {% block content %}
-

Search results

- {% for book in results %} -
- - {% include 'snippets/book_cover.html' with book=book %} - - {{ book.title }} - {{ book.author }} - {% include 'snippets/shelve_button.html' with book=book pulldown=True %} -
- {% endfor %} +

The following books could not be imported:

-

Failures

    - {% for book in failures %} + {% for item in failures %}
  • - {{ book }} + {{ item }}
  • {% endfor %}
+ +

{{ success_count }} books imported successfully

{% endblock %} diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py index 8eb15cec..c3b272bd 100644 --- a/fedireads/view_actions.py +++ b/fedireads/view_actions.py @@ -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()