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(
|
2021-03-08 16:49:10 +00:00
|
|
|
user, models.Review.objects.filter(book=book)
|
|
|
|
)
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
.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":
|
|
|
|
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"""
|
2020-03-27 16:37:11 +00:00
|
|
|
shelf = models.ShelfBook.objects.filter(
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf__user=context["request"].user, book__in=book.parent_work.editions.all()
|
2021-05-23 00:58:08 +00:00
|
|
|
).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
|
|
|
|
|
|
|
|
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"""
|
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
|
|
|
|
|
|
|
|
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-04-30 16:48:10 +00:00
|
|
|
return user.followers.filter(id__in=viewer.following.all()).count()
|