2021-03-08 16:49:10 +00:00
|
|
|
""" template filters """
|
2021-05-11 21:41:28 +00:00
|
|
|
from django import template
|
2021-01-31 18:34:25 +00:00
|
|
|
from django.db.models import Avg
|
2020-01-29 23:10:32 +00:00
|
|
|
|
2021-10-06 17:37:09 +00:00
|
|
|
from bookwyrm import models
|
2022-01-09 00:38:52 +00:00
|
|
|
from bookwyrm.utils import cache
|
2022-01-05 21:12:49 +00:00
|
|
|
from bookwyrm.views.feed import get_suggested_books
|
2020-02-21 23:39:25 +00:00
|
|
|
|
|
|
|
|
2020-01-29 23:10:32 +00:00
|
|
|
register = template.Library()
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
@register.filter(name="rating")
|
2020-04-03 19:43:49 +00:00
|
|
|
def get_rating(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get the overall rating of a book"""
|
2021-10-06 17:37:09 +00:00
|
|
|
queryset = models.Review.privacy_filter(user).filter(
|
|
|
|
book__parent_work__editions=book
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
return queryset.aggregate(Avg("rating"))["rating__avg"]
|
2021-01-31 18:34:25 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="user_rating")
|
2021-01-31 18:34:25 +00:00
|
|
|
def get_user_rating(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a user's rating of a book"""
|
2021-03-08 16:49:10 +00:00
|
|
|
rating = (
|
|
|
|
models.Review.objects.filter(
|
|
|
|
user=user,
|
|
|
|
book=book,
|
|
|
|
rating__isnull=False,
|
2021-05-23 15:48:00 +00:00
|
|
|
deleted=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
|
|
|
.order_by("-published_date")
|
|
|
|
.first()
|
|
|
|
)
|
2020-04-03 19:43:49 +00:00
|
|
|
if rating:
|
|
|
|
return rating.rating
|
|
|
|
return 0
|
2020-01-29 23:32:43 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="book_description")
|
2020-10-31 21:18:56 +00:00
|
|
|
def get_book_description(book):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""use the work's text if the book doesn't have it"""
|
2020-10-31 21:18:56 +00:00
|
|
|
return book.description or book.parent_work.description
|
|
|
|
|
2020-12-13 02:25:04 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="next_shelf")
|
2021-01-30 19:43:40 +00:00
|
|
|
def get_next_shelf(current_shelf):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelf you'd use to update reading progress"""
|
2021-03-08 16:49:10 +00:00
|
|
|
if current_shelf == "to-read":
|
|
|
|
return "reading"
|
|
|
|
if current_shelf == "reading":
|
|
|
|
return "read"
|
|
|
|
if current_shelf == "read":
|
2021-09-09 22:36:58 +00:00
|
|
|
return "complete"
|
2021-03-08 16:49:10 +00:00
|
|
|
return "to-read"
|
|
|
|
|
2021-04-23 22:01:35 +00:00
|
|
|
|
2021-10-03 02:22:21 +00:00
|
|
|
@register.filter(name="load_subclass")
|
|
|
|
def load_subclass(status):
|
|
|
|
"""sometimes you didn't select_subclass"""
|
|
|
|
if hasattr(status, "quotation"):
|
|
|
|
return status.quotation
|
|
|
|
if hasattr(status, "review"):
|
|
|
|
return status.review
|
|
|
|
if hasattr(status, "comment"):
|
|
|
|
return status.comment
|
2022-01-06 19:27:02 +00:00
|
|
|
if hasattr(status, "generatednote"):
|
|
|
|
return status.generatednote
|
2021-10-03 02:22:21 +00:00
|
|
|
return status
|
|
|
|
|
|
|
|
|
2021-01-06 23:53:09 +00:00
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def related_status(notification):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""for notifications"""
|
2021-01-06 23:53:09 +00:00
|
|
|
if not notification.related_status:
|
|
|
|
return None
|
2021-10-03 02:22:21 +00:00
|
|
|
return load_subclass(notification.related_status)
|
2021-01-06 23:53:09 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-02-21 23:39:25 +00:00
|
|
|
@register.simple_tag(takes_context=True)
|
2020-11-06 00:48:15 +00:00
|
|
|
def active_shelf(context, book):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""check what shelf a user has a book on, if any"""
|
2022-01-09 00:38:52 +00:00
|
|
|
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()
|
2021-05-23 01:02:14 +00:00
|
|
|
)
|
2022-01-09 00:38:52 +00:00
|
|
|
or {"book": book},
|
|
|
|
user,
|
|
|
|
book,
|
|
|
|
timeout=15552000,
|
2021-05-23 01:02:14 +00:00
|
|
|
)
|
2020-02-21 23:39:25 +00:00
|
|
|
|
|
|
|
|
2020-04-15 12:06:23 +00:00
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def latest_read_through(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the most recent read activity"""
|
2022-01-09 00:38:52 +00:00
|
|
|
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()
|
|
|
|
),
|
|
|
|
user,
|
|
|
|
book,
|
|
|
|
timeout=15552000,
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2020-11-06 00:48:15 +00:00
|
|
|
|
|
|
|
|
2021-04-30 16:48:10 +00:00
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
|
def mutuals_count(context, user):
|
|
|
|
"""how many users that you follow, follow them"""
|
|
|
|
viewer = context["request"].user
|
2021-04-30 18:11:18 +00:00
|
|
|
if not viewer.is_authenticated:
|
|
|
|
return None
|
2021-09-20 22:25:29 +00:00
|
|
|
return user.followers.filter(followers=viewer).count()
|
2022-01-05 21:12:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
|
def suggested_books(context):
|
|
|
|
"""get books for suggested books panel"""
|
|
|
|
# this happens here instead of in the view so that the template snippet can
|
|
|
|
# be cached in the template
|
|
|
|
return get_suggested_books(context["request"].user)
|