From fc3b609adafb57f37f7947bfa448f5c1ec3a635b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 30 May 2022 08:42:48 -0700 Subject: [PATCH] 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. --- bookwyrm/models/status.py | 2 +- bookwyrm/templatetags/rating_tags.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 1e31de77..f37076b5 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -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) diff --git a/bookwyrm/templatetags/rating_tags.py b/bookwyrm/templatetags/rating_tags.py index 670599e2..cc23d330 100644 --- a/bookwyrm/templatetags/rating_tags.py +++ b/bookwyrm/templatetags/rating_tags.py @@ -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,