mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Merge pull request #1441 from bookwyrm-social/query-efficiency
Query efficiency
This commit is contained in:
commit
467c6ff266
4 changed files with 18 additions and 6 deletions
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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()
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue