From 22ead6ba46c9fd4963c5253f01eb5111736dcf2a Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 20 Sep 2021 14:29:01 -0700 Subject: [PATCH 1/3] More efficient followers query --- bookwyrm/views/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index b1e5f68c..f9789e9a 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -77,7 +77,7 @@ def privacy_filter(viewer, queryset, privacy_levels=None, following_only=False): elif "followers" in privacy_levels: queryset = queryset.exclude( ~Q( # user isn't following and it isn't their own status - Q(user__in=viewer.following.all()) | Q(user=viewer) + Q(user__followers=viewer) | Q(user=viewer) ), privacy="followers", # and the status is followers only ) From aa0ffd86c0ac3fa227f11afa41432ce9aab7fb41 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 20 Sep 2021 14:49:07 -0700 Subject: [PATCH 2/3] Fetch related items when loading list page --- bookwyrm/views/list.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bookwyrm/views/list.py b/bookwyrm/views/list.py index af99e9f5..8e600490 100644 --- a/bookwyrm/views/list.py +++ b/bookwyrm/views/list.py @@ -36,6 +36,8 @@ class Lists(View): item_count=Count("listitem", filter=Q(listitem__approved=True)) ) .filter(item_count__gt=0) + .select_related("user") + .prefetch_related("listitem_set") .order_by("-updated_date") .distinct() ) From 89fad597936de520284c08c860b286632a6d39b2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 20 Sep 2021 15:25:29 -0700 Subject: [PATCH 3/3] Query efficiency in user view --- bookwyrm/templatetags/bookwyrm_tags.py | 6 +++--- bookwyrm/views/user.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index 30a48e90..e00a8331 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -12,7 +12,7 @@ register = template.Library() def get_rating(book, user): """get the overall rating of a book""" queryset = views.helpers.privacy_filter( - user, models.Review.objects.filter(book__in=book.parent_work.editions.all()) + user, models.Review.objects.filter(book__parent_work__editions=book) ) return queryset.aggregate(Avg("rating"))["rating__avg"] @@ -73,7 +73,7 @@ def active_shelf(context, book): shelf = ( models.ShelfBook.objects.filter( shelf__user=context["request"].user, - book__in=book.parent_work.editions.all(), + book__parent_work__editions=book, ) .select_related("book", "shelf") .first() @@ -97,4 +97,4 @@ def mutuals_count(context, user): viewer = context["request"].user if not viewer.is_authenticated: return None - return user.followers.filter(id__in=viewer.following.all()).count() + return user.followers.filter(followers=viewer).count() diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index e335b02c..ca6eb0a5 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -59,8 +59,18 @@ class User(View): request.user, user.status_set.select_subclasses(), ) - .select_related("reply_parent") - .prefetch_related("mention_books", "mention_users") + .select_related( + "user", + "reply_parent", + "review__book", + "comment__book", + "quotation__book", + ) + .prefetch_related( + "mention_books", + "mention_users", + "attachments", + ) ) paginated = Paginator(activities, PAGE_LENGTH)