moviewyrm/bookwyrm/templatetags/bookwyrm_tags.py

101 lines
2.8 KiB
Python
Raw Normal View History

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-01-31 18:34:25 +00:00
from bookwyrm import models, views
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-02-24 19:35:19 +00:00
queryset = views.helpers.privacy_filter(
user, models.Review.objects.filter(book__in=book.parent_work.editions.all())
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")
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"""
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":
return "read"
return "to-read"
2021-04-23 22:01:35 +00:00
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-03-08 16:49:10 +00:00
if hasattr(notification.related_status, "quotation"):
2021-01-06 23:53:09 +00:00
return notification.related_status.quotation
2021-03-08 16:49:10 +00:00
if hasattr(notification.related_status, "review"):
2021-01-06 23:53:09 +00:00
return notification.related_status.review
2021-03-08 16:49:10 +00:00
if hasattr(notification.related_status, "comment"):
2021-01-06 23:53:09 +00:00
return notification.related_status.comment
return notification.related_status
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"""
2021-05-23 01:02:14 +00:00
shelf = (
models.ShelfBook.objects.filter(
shelf__user=context["request"].user,
book__in=book.parent_work.editions.all(),
)
.select_related("book", "shelf")
.first()
)
2021-03-08 16:49:10 +00:00
return shelf if shelf else {"book": book}
2020-02-21 23:39:25 +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"""
2021-03-08 16:49:10 +00:00
return (
models.ReadThrough.objects.filter(user=user, book=book)
.order_by("-start_date")
.first()
)
2020-11-06 00:48:15 +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
if not viewer.is_authenticated:
return None
return user.followers.filter(id__in=viewer.following.all()).count()