forked from mirrors/bookwyrm
Cache checking if books are on shelve in shelve button snippet
This commit is contained in:
parent
a09c813f73
commit
8bbc4d3301
6 changed files with 36 additions and 10 deletions
|
@ -1,5 +1,6 @@
|
||||||
""" puttin' books on shelves """
|
""" puttin' books on shelves """
|
||||||
import re
|
import re
|
||||||
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -94,8 +95,15 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel):
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.user:
|
if not self.user:
|
||||||
self.user = self.shelf.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)
|
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:
|
class Meta:
|
||||||
"""an opinionated constraint!
|
"""an opinionated constraint!
|
||||||
you can't put a book on shelf twice"""
|
you can't put a book on shelf twice"""
|
||||||
|
|
|
@ -6,8 +6,9 @@
|
||||||
{% with book.id|uuid as uuid %}
|
{% with book.id|uuid as uuid %}
|
||||||
{% active_shelf book as active_shelf %}
|
{% active_shelf book as active_shelf %}
|
||||||
{% latest_read_through book request.user as readthrough %}
|
{% latest_read_through book request.user as readthrough %}
|
||||||
|
{% with active_shelf_book=active_shelf.book %}
|
||||||
<div class="field has-addons mb-0 has-text-weight-normal" data-shelve-button-book="{{ book.id }}">
|
<div class="field has-addons mb-0 has-text-weight-normal" data-shelve-button-book="{{ book.id }}">
|
||||||
{% if switch_mode and active_shelf.book != book %}
|
{% if switch_mode and active_shelf_book != book %}
|
||||||
<div class="control">
|
<div class="control">
|
||||||
{% include 'snippets/switch_edition_button.html' with edition=book size='is-small' %}
|
{% include 'snippets/switch_edition_button.html' with edition=book size='is-small' %}
|
||||||
</div>
|
</div>
|
||||||
|
@ -20,16 +21,17 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% join "want_to_read" uuid as modal_id %}
|
{% 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 %}
|
{% 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 %}
|
{% 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 %}
|
{% 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 %}
|
{% endwith %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
<form name="shelve-{{ uuid }}-{{ shelf.identifier }}" action="/shelve/" method="post" autocomplete="off">
|
<form name="shelve-{{ uuid }}-{{ shelf.identifier }}" action="/shelve/" method="post" autocomplete="off">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="book" value="{{ active_shelf.book.id }}">
|
<input type="hidden" name="book" value="{{ active_shelf.book.id }}">
|
||||||
<button class="button {{ class }}" name="shelf" type="submit" value="{{ shelf.identifier }}" {% if shelf in book.shelf_set.all %} disabled {% endif %}>
|
<button class="button {{ class }}" name="shelf" type="submit" value="{{ shelf.identifier }}" {% if book|is_book_on_shelf:shelf %} disabled {% endif %}>
|
||||||
<span>{{ shelf.name }}</span>
|
<span>{{ shelf.name }}</span>
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -45,7 +45,13 @@
|
||||||
<form name="shelve-{{ uuid }}-{{ shelf.identifier }}" action="/shelve/" method="post">
|
<form name="shelve-{{ uuid }}-{{ shelf.identifier }}" action="/shelve/" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="hidden" name="book" value="{{ active_shelf.book.id }}">
|
<input type="hidden" name="book" value="{{ active_shelf.book.id }}">
|
||||||
<button class="button {{ class }}" name="shelf" type="submit" value="{{ shelf.identifier }}" {% if shelf in book.shelf_set.all %} disabled {% endif %}>
|
<button
|
||||||
|
class="button {{ class }}"
|
||||||
|
name="shelf"
|
||||||
|
type="submit"
|
||||||
|
value="{{ shelf.identifier }}"
|
||||||
|
{% if book|is_book_on_shelf:shelf %} disabled {% endif %}
|
||||||
|
>
|
||||||
<span>{{ shelf.name }}</span>
|
<span>{{ shelf.name }}</span>
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -43,6 +43,18 @@ def get_user_rating(book, user):
|
||||||
return 0
|
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")
|
@register.filter(name="book_description")
|
||||||
def get_book_description(book):
|
def get_book_description(book):
|
||||||
"""use the work's text if the book doesn't have it"""
|
"""use the work's text if the book doesn't have it"""
|
||||||
|
|
|
@ -46,9 +46,7 @@ class ReadingStatus(View):
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
|
|
||||||
# invalidate related caches
|
# invalidate related caches
|
||||||
cache.delete(
|
cache.delete(f"active_shelf-{request.user.id}-{book_id}")
|
||||||
f"active_shelf-{request.user.id}-{book_id}",
|
|
||||||
)
|
|
||||||
|
|
||||||
desired_shelf = get_object_or_404(
|
desired_shelf = get_object_or_404(
|
||||||
models.Shelf, identifier=identifier, user=request.user
|
models.Shelf, identifier=identifier, user=request.user
|
||||||
|
|
Loading…
Reference in a new issue