mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
Moves rating tag to file
This commit is contained in:
parent
4f4d2dc53e
commit
6e6e2ab777
2 changed files with 42 additions and 34 deletions
|
@ -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"""
|
||||
|
|
42
bookwyrm/templatetags/rating_tags.py
Normal file
42
bookwyrm/templatetags/rating_tags.py
Normal 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
|
Loading…
Reference in a new issue