mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
Improves shelf page query efficiency a little
This commit is contained in:
parent
7a4c4248df
commit
2d4cec4422
2 changed files with 23 additions and 12 deletions
|
@ -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)
|
||||
|
|
|
@ -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,6 +28,11 @@ class Shelf(View):
|
|||
"""display a shelf"""
|
||||
user = get_user_from_username(request.user, username)
|
||||
|
||||
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
|
||||
|
@ -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(
|
||||
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,
|
||||
|
|
Loading…
Reference in a new issue