From 2d4cec4422fba603bde6a0222a599cadedaf346a Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 May 2021 19:54:50 -0700 Subject: [PATCH] Improves shelf page query efficiency a little --- bookwyrm/views/helpers.py | 4 ++++ bookwyrm/views/shelf.py | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/bookwyrm/views/helpers.py b/bookwyrm/views/helpers.py index 31e5f031..3dc02e0d 100644 --- a/bookwyrm/views/helpers.py +++ b/bookwyrm/views/helpers.py @@ -13,6 +13,10 @@ from bookwyrm.utils import regex def get_user_from_username(viewer, username): """helper function to resolve a localname or a username to a user""" + if viewer.localname == username: + # that's yourself, fool + return viewer + # raises 404 if the user isn't found try: return models.User.viewer_aware_objects(viewer).get(localname=username) diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index 758e290e..99e22fa9 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -2,7 +2,7 @@ from collections import namedtuple from django.db import IntegrityError -from django.db.models import Count, OuterRef, Subquery, F, Q +from django.db.models import OuterRef, Subquery from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.http import HttpResponseBadRequest, HttpResponseNotFound @@ -28,7 +28,12 @@ class Shelf(View): """display a shelf""" user = get_user_from_username(request.user, username) - shelves = privacy_filter(request.user, user.shelf_set) + is_self = user == request.user + + if is_self: + shelves = user.shelf_set + else: + shelves = privacy_filter(request.user, user.shelf_set) # get the shelf and make sure the logged in user should be able to see it if shelf_identifier: @@ -53,26 +58,28 @@ class Shelf(View): if is_api_request(request): return ActivitypubResponse(shelf.to_activity(**request.GET)) - reviews = privacy_filter( - request.user, - models.Review.objects.filter( - user=user, - rating__isnull=False, - book__id=OuterRef("id"), - ), + reviews = models.Review.objects.filter( + user=user, + rating__isnull=False, + book__id=OuterRef("id"), ).order_by("-published_date") - books = books.annotate(rating=Subquery(reviews.values("rating")[:1])) + if not is_self: + reviews = privacy_filter(request.user, reviews) + + books = books.annotate( + rating=Subquery(reviews.values("rating")[:1]) + ).prefetch_related("authors") paginated = Paginator( - books.order_by("-updated_date"), + books.order_by("-shelfbook__updated_date"), PAGE_LENGTH, ) page = paginated.get_page(request.GET.get("page")) data = { "user": user, - "is_self": request.user == user, + "is_self": is_self, "shelves": shelves.all(), "shelf": shelf, "books": page,