Merge pull request #2138 from bookwyrm-social/ratings-query

Use general ratings rather than privacy filtered
This commit is contained in:
Mouse Reeve 2022-05-30 09:33:05 -07:00 committed by GitHub
commit 7905be7de2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 7 deletions

View file

@ -374,7 +374,7 @@ class Review(BookStatus):
def save(self, *args, **kwargs):
"""clear rating caches"""
if self.book.parent_work:
cache.delete(f"book-rating-{self.book.parent_work.id}-*")
cache.delete(f"book-rating-{self.book.parent_work.id}")
super().save(*args, **kwargs)

View file

@ -13,10 +13,10 @@ register = template.Library()
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, rating__gt=0)
.aggregate(Avg("rating"))["rating__avg"]
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,

View file

@ -40,7 +40,8 @@ class RatingTags(TestCase):
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
def test_get_rating(self, *_):
"""privacy filtered rating"""
"""privacy filtered rating. Commented versions are how it ought to work with
subjective ratings, which are currenly not used for performance reasons."""
# follows-only: not included
models.ReviewRating.objects.create(
user=self.remote_user,
@ -48,7 +49,8 @@ class RatingTags(TestCase):
book=self.book,
privacy="followers",
)
self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 0)
# self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 0)
self.assertEqual(rating_tags.get_rating(self.book, self.local_user), 5)
# public: included
models.ReviewRating.objects.create(