diff --git a/bookwyrm/templates/shelf/shelf.html b/bookwyrm/templates/shelf/shelf.html index 0e295a87..a630918a 100644 --- a/bookwyrm/templates/shelf/shelf.html +++ b/bookwyrm/templates/shelf/shelf.html @@ -1,5 +1,5 @@ {% extends 'layout.html' %} -{% load bookwyrm_tags %} +{% load shelf_tags %} {% load utilities %} {% load humanize %} {% load i18n %} diff --git a/bookwyrm/templates/snippets/create_status/comment.html b/bookwyrm/templates/snippets/create_status/comment.html index 10cdf639..65b32269 100644 --- a/bookwyrm/templates/snippets/create_status/comment.html +++ b/bookwyrm/templates/snippets/create_status/comment.html @@ -1,5 +1,5 @@ {% extends "snippets/create_status/layout.html" %} -{% load bookwyrm_tags %} +{% load shelf_tags %} {% load i18n %} {% load utilities %} {% load status_display %} diff --git a/bookwyrm/templates/snippets/shelf_selector.html b/bookwyrm/templates/snippets/shelf_selector.html index 323e04a2..ea609666 100644 --- a/bookwyrm/templates/snippets/shelf_selector.html +++ b/bookwyrm/templates/snippets/shelf_selector.html @@ -1,7 +1,7 @@ {% extends 'components/dropdown.html' %} -{% load i18n %} -{% load bookwyrm_tags %} +{% load shelf_tags %} {% load utilities %} +{% load i18n %} {% block dropdown-trigger %} {% trans "Move book" %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button.html b/bookwyrm/templates/snippets/shelve_button/shelve_button.html index 37c57fc2..04dc4e4b 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button.html @@ -1,5 +1,5 @@ -{% load bookwyrm_tags %} {% load utilities %} +{% load shelf_tags %} {% if request.user.is_authenticated %} 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 15e2bb51..5a29f9ad 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_dropdown_options.html @@ -1,5 +1,5 @@ -{% load bookwyrm_tags %} {% load utilities %} +{% load shelf_tags %} {% load i18n %} {% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %} diff --git a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html index d6ca9933..04f4bdc2 100644 --- a/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html +++ b/bookwyrm/templates/snippets/shelve_button/shelve_button_options.html @@ -1,5 +1,5 @@ -{% load bookwyrm_tags %} {% load utilities %} +{% load shelf_tags %} {% load i18n %} {% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %} diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 4eff0d82..08d147aa 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -43,36 +43,12 @@ 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""" return book.description or book.parent_work.description -@register.filter(name="next_shelf") -def get_next_shelf(current_shelf): - """shelf you'd use to update reading progress""" - if current_shelf == "to-read": - return "reading" - if current_shelf == "reading": - return "read" - if current_shelf == "read": - return "complete" - return "to-read" - - @register.filter(name="load_subclass") def load_subclass(status): """sometimes you didn't select_subclass""" @@ -146,45 +122,6 @@ def related_status(notification): return load_subclass(notification.related_status) -@register.simple_tag(takes_context=True) -def active_shelf(context, book): - """check what shelf a user has a book on, if any""" - user = context["request"].user - return ( - cache.get_or_set( - f"active_shelf-{user.id}-{book.id}", - lambda u, b: ( - models.ShelfBook.objects.filter( - shelf__user=u, - book__parent_work__editions=b, - ).first() - or False - ), - user, - book, - timeout=15552000, - ) - or {"book": book} - ) - - -@register.simple_tag(takes_context=False) -def latest_read_through(book, user): - """the most recent read activity""" - return cache.get_or_set( - f"latest_read_through-{user.id}-{book.id}", - lambda u, b: ( - models.ReadThrough.objects.filter(user=u, book=b, is_active=True) - .order_by("-start_date") - .first() - or False - ), - user, - book, - timeout=15552000, - ) - - @register.simple_tag(takes_context=False) def get_landing_books(): """list of books for the landing page""" diff --git a/bookwyrm/templatetags/shelf_tags.py b/bookwyrm/templatetags/shelf_tags.py new file mode 100644 index 00000000..7aef638f --- /dev/null +++ b/bookwyrm/templatetags/shelf_tags.py @@ -0,0 +1,71 @@ +""" Filters and tags related to shelving books """ +from django import template + +from bookwyrm import models +from bookwyrm.utils import cache + + +register = template.Library() + + +@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="next_shelf") +def get_next_shelf(current_shelf): + """shelf you'd use to update reading progress""" + if current_shelf == "to-read": + return "reading" + if current_shelf == "reading": + return "read" + if current_shelf == "read": + return "complete" + return "to-read" + + +@register.simple_tag(takes_context=True) +def active_shelf(context, book): + """check what shelf a user has a book on, if any""" + user = context["request"].user + return ( + cache.get_or_set( + f"active_shelf-{user.id}-{book.id}", + lambda u, b: ( + models.ShelfBook.objects.filter( + shelf__user=u, + book__parent_work__editions=b, + ).first() + or False + ), + user, + book, + timeout=15552000, + ) + or {"book": book} + ) + + +@register.simple_tag(takes_context=False) +def latest_read_through(book, user): + """the most recent read activity""" + return cache.get_or_set( + f"latest_read_through-{user.id}-{book.id}", + lambda u, b: ( + models.ReadThrough.objects.filter(user=u, book=b, is_active=True) + .order_by("-start_date") + .first() + or False + ), + user, + book, + timeout=15552000, + )