Adds shelf tags file

This commit is contained in:
Mouse Reeve 2022-01-18 11:20:27 -08:00
parent e3545517ec
commit 8746409e6d
8 changed files with 78 additions and 70 deletions

View file

@ -1,5 +1,5 @@
{% extends 'layout.html' %} {% extends 'layout.html' %}
{% load bookwyrm_tags %} {% load shelf_tags %}
{% load utilities %} {% load utilities %}
{% load humanize %} {% load humanize %}
{% load i18n %} {% load i18n %}

View file

@ -1,5 +1,5 @@
{% extends "snippets/create_status/layout.html" %} {% extends "snippets/create_status/layout.html" %}
{% load bookwyrm_tags %} {% load shelf_tags %}
{% load i18n %} {% load i18n %}
{% load utilities %} {% load utilities %}
{% load status_display %} {% load status_display %}

View file

@ -1,7 +1,7 @@
{% extends 'components/dropdown.html' %} {% extends 'components/dropdown.html' %}
{% load i18n %} {% load shelf_tags %}
{% load bookwyrm_tags %}
{% load utilities %} {% load utilities %}
{% load i18n %}
{% block dropdown-trigger %} {% block dropdown-trigger %}
<span>{% trans "Move book" %}</span> <span>{% trans "Move book" %}</span>

View file

@ -1,5 +1,5 @@
{% load bookwyrm_tags %}
{% load utilities %} {% load utilities %}
{% load shelf_tags %}
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}

View file

@ -1,5 +1,5 @@
{% load bookwyrm_tags %}
{% load utilities %} {% load utilities %}
{% load shelf_tags %}
{% load i18n %} {% load i18n %}
{% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %} {% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %}

View file

@ -1,5 +1,5 @@
{% load bookwyrm_tags %}
{% load utilities %} {% load utilities %}
{% load shelf_tags %}
{% load i18n %} {% load i18n %}
{% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %} {% with next_shelf_identifier=active_shelf.shelf.identifier|next_shelf %}

View file

@ -43,36 +43,12 @@ 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"""
return book.description or book.parent_work.description 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") @register.filter(name="load_subclass")
def load_subclass(status): def load_subclass(status):
"""sometimes you didn't select_subclass""" """sometimes you didn't select_subclass"""
@ -146,45 +122,6 @@ def related_status(notification):
return load_subclass(notification.related_status) 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) @register.simple_tag(takes_context=False)
def get_landing_books(): def get_landing_books():
"""list of books for the landing page""" """list of books for the landing page"""

View file

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