Moves rating tag to file

This commit is contained in:
Mouse Reeve 2022-01-18 12:02:38 -08:00
parent 4f4d2dc53e
commit 6e6e2ab777
2 changed files with 42 additions and 34 deletions

View file

@ -3,46 +3,12 @@ from django import template
from django.db.models import Avg, StdDev, Count, F, Q
from bookwyrm import models
from bookwyrm.utils import cache
from bookwyrm.views.feed import get_suggested_books
register = template.Library()
@register.filter(name="rating")
def get_rating(book, user):
"""get the overall rating of a book"""
return cache.get_or_set(
f"book-rating-{book.parent_work.id}-{user.id}",
lambda u, b: models.Review.privacy_filter(u)
.filter(book__parent_work__editions=b)
.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
@register.filter(name="book_description")
def get_book_description(book):
"""use the work's text if the book doesn't have it"""

View file

@ -0,0 +1,42 @@
""" 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"""
return cache.get_or_set(
f"book-rating-{book.parent_work.id}-{user.id}",
lambda u, b: models.Review.privacy_filter(u)
.filter(book__parent_work__editions=b)
.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