diff --git a/bookwyrm/templates/snippets/shelve_button/finish_reading_modal.html b/bookwyrm/templates/snippets/shelve_button/finish_reading_modal.html index 0ed13ec7..36addc7b 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/shelve_button_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html index 12c8a637..69665a82 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html @@ -9,7 +9,7 @@ {% if shelf.identifier == 'reading' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %} {% trans "Start reading" as button_text %} - {% url 'start-reading' book.id as fallback_url %} + {% url 'reading-status' 'start' book.id as fallback_url %} {% include 'snippets/toggle/toggle_button.html' with class=class text=button_text controls_text="start-reading" controls_uid=button_uuid focus="modal-title-start-reading" disabled=is_current fallback_url=fallback_url %} {% endif %}{% elif shelf.identifier == 'read' and active_shelf.shelf.identifier == 'read' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %} @@ -17,13 +17,13 @@ {% endif %}{% elif shelf.identifier == 'read' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %} {% trans "Finish reading" as button_text %} - {% url 'finish-reading' book.id as fallback_url %} + {% url 'reading-status' 'finish' book.id as fallback_url %} {% include 'snippets/toggle/toggle_button.html' with class=class text=button_text controls_text="finish-reading" controls_uid=button_uuid focus="modal-title-finish-reading" disabled=is_current fallback_url=fallback_url %} {% endif %}{% elif shelf.identifier == 'to-read' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %} {% trans "Want to read" as button_text %} - {% url 'to-read' book.id as fallback_url %} + {% url 'reading-status' 'want' book.id as fallback_url %} {% include 'snippets/toggle/toggle_button.html' with class=class text=button_text controls_text="want-to-read" controls_uid=button_uuid focus="modal-title-want-to-read" disabled=is_current fallback_url=fallback_url %} {% endif %}{% elif shelf.editable %} diff --git a/bookwyrm/templates/snippets/shelve_button/start_reading_modal.html b/bookwyrm/templates/snippets/shelve_button/start_reading_modal.html index 628841cc..1858313b 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 21209e0a..643e4a20 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 e14575e0..a1d0417f 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) @@ -86,7 +86,7 @@ class ReadingViews(TestCase): request = self.factory.post("") 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.assertFalse(to_read_shelf.books.exists()) 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/tests/views/test_readthrough.py b/bookwyrm/tests/views/test_readthrough.py index a686cb7c..764490d8 100644 --- a/bookwyrm/tests/views/test_readthrough.py +++ b/bookwyrm/tests/views/test_readthrough.py @@ -33,7 +33,7 @@ class ReadThrough(TestCase): self.assertEqual(self.edition.readthrough_set.count(), 0) self.client.post( - "/start-reading/{}".format(self.edition.id), + "/reading-status/start/{}".format(self.edition.id), { "start_date": "2020-11-27", }, @@ -54,7 +54,7 @@ class ReadThrough(TestCase): self.assertEqual(self.edition.readthrough_set.count(), 0) self.client.post( - "/start-reading/{}".format(self.edition.id), + "/reading-status/start/{}".format(self.edition.id), { "start_date": "2020-11-27", }, diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index da6919cf..6d70ee1e 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 43743b1c..486b99ed 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 28809383..1c897ab3 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", + "start": "start.html", + "finish": "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