mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-09-26 13:30:13 +00:00
fbb9d75cc8
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.
46 lines
1.1 KiB
Python
46 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
|