Merge pull request #1441 from bookwyrm-social/query-efficiency

Query efficiency
This commit is contained in:
Mouse Reeve 2021-09-20 16:25:00 -07:00 committed by GitHub
commit 467c6ff266
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 6 deletions

View file

@ -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()

View file

@ -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
)

View file

@ -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()
)

View file

@ -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)