From 113ced290084ab8238226ce88dd0413e6ad021c3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 9 Jun 2021 10:36:52 -0700 Subject: [PATCH] Merge reading status views into one view --- .../shelve_button/finish_reading_modal.html | 2 +- .../shelve_button/start_reading_modal.html | 2 +- .../shelve_button/want_to_read_modal.html | 2 +- bookwyrm/tests/views/test_reading.py | 4 +- bookwyrm/urls.py | 14 +- bookwyrm/views/__init__.py | 5 +- bookwyrm/views/reading.py | 126 +++++++----------- 7 files changed, 60 insertions(+), 95 deletions(-) diff --git a/bookwyrm/templates/snippets/shelve_button/finish_reading_modal.html b/bookwyrm/templates/snippets/shelve_button/finish_reading_modal.html index 0ed13ec74..36addc7b8 100644 --- a/bookwyrm/templates/snippets/shelve_button/finish_reading_modal.html +++ b/bookwyrm/templates/snippets/shelve_button/finish_reading_modal.html @@ -7,7 +7,7 @@ {% block modal-form-open %} -
+ {% endblock %} {% block modal-body %} diff --git a/bookwyrm/templates/snippets/shelve_button/start_reading_modal.html b/bookwyrm/templates/snippets/shelve_button/start_reading_modal.html index 628841ccb..1858313b3 100644 --- a/bookwyrm/templates/snippets/shelve_button/start_reading_modal.html +++ b/bookwyrm/templates/snippets/shelve_button/start_reading_modal.html @@ -8,7 +8,7 @@ Start "{{ book_title }}" {% endblock %} {% block modal-form-open %} - + {% endblock %} {% block modal-body %} diff --git a/bookwyrm/templates/snippets/shelve_button/want_to_read_modal.html b/bookwyrm/templates/snippets/shelve_button/want_to_read_modal.html index 21209e0a1..643e4a202 100644 --- a/bookwyrm/templates/snippets/shelve_button/want_to_read_modal.html +++ b/bookwyrm/templates/snippets/shelve_button/want_to_read_modal.html @@ -6,7 +6,7 @@ {% endblock %} {% block modal-form-open %} - + {% csrf_token %} diff --git a/bookwyrm/tests/views/test_reading.py b/bookwyrm/tests/views/test_reading.py index e14575e09..fc0d56cf1 100644 --- a/bookwyrm/tests/views/test_reading.py +++ b/bookwyrm/tests/views/test_reading.py @@ -56,7 +56,7 @@ class ReadingViews(TestCase): ) request.user = self.local_user with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - views.StartReading.as_view()(request, self.book.id) + views.ReadingStatus.as_view()(request, "start", self.book.id) self.assertEqual(shelf.books.get(), self.book) @@ -112,7 +112,7 @@ class ReadingViews(TestCase): request.user = self.local_user with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - views.FinishReading.as_view()(request, self.book.id) + views.ReadingStatus.as_view()(request, "finish", self.book.id) self.assertEqual(shelf.books.get(), self.book) diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index da6919cfe..6d70ee1e1 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -317,17 +317,9 @@ urlpatterns = [ re_path(r"^delete-progressupdate/?$", views.delete_progressupdate), # shelve actions re_path( - r"^to-read/(?P\d+)/?$", views.WantToRead.as_view(), name="to-read" - ), - re_path( - r"^start-reading/(?P\d+)/?$", - views.StartReading.as_view(), - name="start-reading", - ), - re_path( - r"^finish-reading/(?P\d+)/?$", - views.FinishReading.as_view(), - name="finish-reading", + r"^reading-status/(?Pwant|start|finish)/(?P\d+)/?$", + views.ReadingStatus.as_view(), + name="reading-status", ), # following re_path(r"^follow/?$", views.follow, name="follow"), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 43743b1cb..486b99ed8 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -24,8 +24,9 @@ from .landing import About, Home, Discover from .list import Lists, List, Curate, UserLists from .notifications import Notifications from .outbox import Outbox -from .reading import edit_readthrough, create_readthrough, delete_readthrough -from .reading import WantToRead, StartReading, FinishReading, delete_progressupdate +from .reading import edit_readthrough, create_readthrough +from .reading import delete_readthrough, delete_progressupdate +from .reading import ReadingStatus from .reports import Report, Reports, make_report, resolve_report, suspend_user from .rss_feed import RssFeed from .password import PasswordResetRequest, PasswordReset, ChangePassword diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index 28809383d..14e02ae61 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -18,96 +18,68 @@ from .helpers import get_edition, handle_reading_status @method_decorator(login_required, name="dispatch") # pylint: disable=no-self-use -class WantToRead(View): +class ReadingStatus(View): """consider reading a book""" - def get(self, request, book_id): + def get(self, request, status, book_id): """modal page""" book = get_edition(book_id) - return TemplateResponse(request, "reading_progress/want.html", {"book": book}) + template = { + "want": "want.html", + "finish": "start.html", + "start": "finish.html", + }.get(status) + if not template: + return HttpResponseNotFound() + return TemplateResponse(request, f"reading_progress/{template}", {"book": book}) - def post(self, request, book_id): + def post(self, request, status, book_id): """desire a book""" + identifier = { + "want": models.Shelf.TO_READ, + "start": models.Shelf.READING, + "finish": models.Shelf.READ_FINISHED, + }.get(status) + if not identifier: + return HttpResponseBadRequest() + desired_shelf = models.Shelf.objects.filter( - identifier=models.Shelf.TO_READ, user=request.user + identifier=identifier, user=request.user ).first() - return handle_shelve(request, book_id, desired_shelf) - -@method_decorator(login_required, name="dispatch") -# pylint: disable=no-self-use -class StartReading(View): - """begin a book""" - - def get(self, request, book_id): - """modal page""" book = get_edition(book_id) - return TemplateResponse(request, "reading_progress/start.html", {"book": book}) - def post(self, request, book_id): - """begin reading a book""" - desired_shelf = models.Shelf.objects.filter( - identifier=models.Shelf.READING, user=request.user - ).first() - return handle_shelve(request, book_id, desired_shelf) - - -@method_decorator(login_required, name="dispatch") -# pylint: disable=no-self-use -class FinishReading(View): - """finish a book""" - - def get(self, request, book_id): - """modal page""" - book = get_edition(book_id) - return TemplateResponse(request, "reading_progress/finish.html", {"book": book}) - - def post(self, request, book_id): - """a user completed a book, yay""" - desired_shelf = models.Shelf.objects.filter( - identifier=models.Shelf.READ_FINISHED, user=request.user - ).first() - return handle_shelve(request, book_id, desired_shelf) - - -def handle_shelve(request, book_id, desired_shelf): - """these are all basically the same""" - book = get_edition(book_id) - - reshelve_book(request.user, book, desired_shelf) - - if desired_shelf.identifier != models.Shelf.TO_READ: - # update or create a readthrough - readthrough = update_readthrough(request, book=book) - if readthrough: - readthrough.save() - - # post about it (if you want) - if request.POST.get("post-status"): - privacy = request.POST.get("privacy") - handle_reading_status(request.user, desired_shelf, book, privacy) - - return redirect(request.headers.get("Referer", "/")) - - -def reshelve_book(user, book, desired_shelf): - """move a book to a new shelf""" - current_status_shelfbook = ( - models.ShelfBook.objects.select_related("shelf") - .filter( - shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS, - user=user, - book=book, + current_status_shelfbook = ( + models.ShelfBook.objects.select_related("shelf") + .filter( + shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS, + user=request.user, + book=book, + ) + .first() ) - .first() - ) - if current_status_shelfbook is not None: - if current_status_shelfbook.shelf.identifier != desired_shelf.identifier: - current_status_shelfbook.delete() - else: # It already was on the shelf - return + if current_status_shelfbook is not None: + if current_status_shelfbook.shelf.identifier != desired_shelf.identifier: + current_status_shelfbook.delete() + else: # It already was on the shelf + return redirect(request.headers.get("Referer", "/")) - models.ShelfBook.objects.create(book=book, shelf=desired_shelf, user=user) + models.ShelfBook.objects.create( + book=book, shelf=desired_shelf, user=request.user + ) + + if desired_shelf.identifier != models.Shelf.TO_READ: + # update or create a readthrough + readthrough = update_readthrough(request, book=book) + if readthrough: + readthrough.save() + + # post about it (if you want) + if request.POST.get("post-status"): + privacy = request.POST.get("privacy") + handle_reading_status(request.user, desired_shelf, book, privacy) + + return redirect(request.headers.get("Referer", "/")) @login_required