Use general ratings rather than privacy filtered

The original system customized how a rating is displayed to every user
based on the privacy settings of the reviews and, relatedly, who the
user follows. This is cool, but the query is too complicated to load in
sessions, and the initial load, which isn't mitigated by caching, is too
much and causes timeouts for many users. Also the cache clearing wasn't
working correctly because I put in a wildcard, which does not work.
This commit is contained in:
Mouse Reeve 2022-05-30 08:42:48 -07:00
parent 4e3c346780
commit fc3b609ada
2 changed files with 5 additions and 5 deletions

View file

@ -374,7 +374,7 @@ class Review(BookStatus):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
"""clear rating caches""" """clear rating caches"""
if self.book.parent_work: 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) super().save(*args, **kwargs)

View file

@ -13,10 +13,10 @@ register = template.Library()
def get_rating(book, user): def get_rating(book, user):
"""get the overall rating of a book""" """get the overall rating of a book"""
return cache.get_or_set( return cache.get_or_set(
f"book-rating-{book.parent_work.id}-{user.id}", f"book-rating-{book.parent_work.id}",
lambda u, b: models.Review.privacy_filter(u) lambda u, b: models.Review.objects.filter(
.filter(book__parent_work__editions=b, rating__gt=0) book__parent_work__editions=b, rating__gt=0
.aggregate(Avg("rating"))["rating__avg"] ).aggregate(Avg("rating"))["rating__avg"]
or 0, or 0,
user, user,
book, book,