diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index c578f082..320d495d 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -1,5 +1,6 @@ """ puttin' books on shelves """ import re +from django.core.cache import cache from django.core.exceptions import PermissionDenied from django.db import models from django.utils import timezone @@ -94,8 +95,15 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel): def save(self, *args, **kwargs): if not self.user: self.user = self.shelf.user + if self.id and self.user.local: + cache.delete(f"book-on-shelf-{self.book.id}-{self.shelf.id}") super().save(*args, **kwargs) + def delete(self, *args, **kwargs): + if self.id and self.user.local: + cache.delete(f"book-on-shelf-{self.book.id}-{self.shelf.id}") + super().delete(*args, **kwargs) + class Meta: """an opinionated constraint! you can't put a book on shelf twice""" diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button.html b/bookwyrm/templates/snippets/shelve_button/shelve_button.html index 38f6be38..37c57fc2 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button.html @@ -6,8 +6,9 @@ {% with book.id|uuid as uuid %} {% active_shelf book as active_shelf %} {% latest_read_through book request.user as readthrough %} +{% with active_shelf_book=active_shelf.book %}
- {% if switch_mode and active_shelf.book != book %} + {% if switch_mode and active_shelf_book != book %}
{% include 'snippets/switch_edition_button.html' with edition=book size='is-small' %}
@@ -20,16 +21,17 @@
{% join "want_to_read" uuid as modal_id %} -{% include 'snippets/reading_modals/want_to_read_modal.html' with book=active_shelf.book id=modal_id class="" %} +{% include 'snippets/reading_modals/want_to_read_modal.html' with book=active_shelf_book id=modal_id class="" %} {% join "start_reading" uuid as modal_id %} -{% include 'snippets/reading_modals/start_reading_modal.html' with book=active_shelf.book id=modal_id class="" %} +{% include 'snippets/reading_modals/start_reading_modal.html' with book=active_shelf_book id=modal_id class="" %} {% join "finish_reading" uuid as modal_id %} -{% include 'snippets/reading_modals/finish_reading_modal.html' with book=active_shelf.book id=modal_id readthrough=readthrough class="" %} +{% include 'snippets/reading_modals/finish_reading_modal.html' with book=active_shelf_book id=modal_id readthrough=readthrough class="" %} {% join "progress_update" uuid as modal_id %} -{% include 'snippets/reading_modals/progress_update_modal.html' with book=active_shelf.book id=modal_id readthrough=readthrough class="" %} +{% include 'snippets/reading_modals/progress_update_modal.html' with book=active_shelf_book id=modal_id readthrough=readthrough class="" %} +{% endwith %} {% endwith %} {% endif %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html index 0b36ddbb..15e2bb51 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html @@ -38,7 +38,7 @@
{% csrf_token %} -
diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html index add2a77c..d6ca9933 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html @@ -45,7 +45,13 @@
{% csrf_token %} -
diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index f9668197..4eff0d82 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -43,6 +43,18 @@ def get_user_rating(book, user): return 0 +@register.filter(name="is_book_on_shelf") +def get_is_book_on_shelf(book, shelf): + """is a book on a shelf""" + return cache.get_or_set( + f"book-on-shelf-{book.id}-{shelf.id}", + lambda b, s: s.books.filter(id=b.id).exists(), + book, + shelf, + timeout=15552000, + ) + + @register.filter(name="book_description") def get_book_description(book): """use the work's text if the book doesn't have it""" diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index d90c0e9d..2cd05202 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -46,9 +46,7 @@ class ReadingStatus(View): return HttpResponseBadRequest() # invalidate related caches - cache.delete( - f"active_shelf-{request.user.id}-{book_id}", - ) + cache.delete(f"active_shelf-{request.user.id}-{book_id}") desired_shelf = get_object_or_404( models.Shelf, identifier=identifier, user=request.user