bookwyrm/bookwyrm/templatetags/rating_tags.py
Mouse Reeve fbb9d75cc8 Avoid server error when encountering broken edition
If an edition is missing its work, this change allows the page to still
load without a server error; this is important because otherwise the
book will break every page it appears on, including the feed page.
2023-07-17 07:07:01 -07:00

47 lines
1.1 KiB
Python

""" template filters """
from django import template
from django.db.models import Avg
from bookwyrm import models
from bookwyrm.utils import cache
register = template.Library()
@register.filter(name="rating")
def get_rating(book, user):
"""get the overall rating of a book"""
# this shouldn't happen, but it CAN
if not book.parent_work:
return None
return cache.get_or_set(
f"book-rating-{book.parent_work.id}",
lambda u, b: models.Review.objects.filter(
book__parent_work__editions=b, rating__gt=0
).aggregate(Avg("rating"))["rating__avg"]
or 0,
user,
book,
timeout=15552000,
)
@register.filter(name="user_rating")
def get_user_rating(book, user):
"""get a user's rating of a book"""
rating = (
models.Review.objects.filter(
user=user,
book=book,
rating__isnull=False,
deleted=False,
)
.order_by("-published_date")
.first()
)
if rating:
return rating.rating
return 0