bookwyrm/bookwyrm/templatetags/shelf_tags.py
2022-01-30 07:01:38 -08:00

69 lines
1.7 KiB
Python

""" 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,
)