mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Query efficiency in user view
This commit is contained in:
parent
aa0ffd86c0
commit
89fad59793
2 changed files with 15 additions and 5 deletions
|
@ -12,7 +12,7 @@ register = template.Library()
|
||||||
def get_rating(book, user):
|
def get_rating(book, user):
|
||||||
"""get the overall rating of a book"""
|
"""get the overall rating of a book"""
|
||||||
queryset = views.helpers.privacy_filter(
|
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"]
|
return queryset.aggregate(Avg("rating"))["rating__avg"]
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ def active_shelf(context, book):
|
||||||
shelf = (
|
shelf = (
|
||||||
models.ShelfBook.objects.filter(
|
models.ShelfBook.objects.filter(
|
||||||
shelf__user=context["request"].user,
|
shelf__user=context["request"].user,
|
||||||
book__in=book.parent_work.editions.all(),
|
book__parent_work__editions=book,
|
||||||
)
|
)
|
||||||
.select_related("book", "shelf")
|
.select_related("book", "shelf")
|
||||||
.first()
|
.first()
|
||||||
|
@ -97,4 +97,4 @@ def mutuals_count(context, user):
|
||||||
viewer = context["request"].user
|
viewer = context["request"].user
|
||||||
if not viewer.is_authenticated:
|
if not viewer.is_authenticated:
|
||||||
return None
|
return None
|
||||||
return user.followers.filter(id__in=viewer.following.all()).count()
|
return user.followers.filter(followers=viewer).count()
|
||||||
|
|
|
@ -59,8 +59,18 @@ class User(View):
|
||||||
request.user,
|
request.user,
|
||||||
user.status_set.select_subclasses(),
|
user.status_set.select_subclasses(),
|
||||||
)
|
)
|
||||||
.select_related("reply_parent")
|
.select_related(
|
||||||
.prefetch_related("mention_books", "mention_users")
|
"user",
|
||||||
|
"reply_parent",
|
||||||
|
"review__book",
|
||||||
|
"comment__book",
|
||||||
|
"quotation__book",
|
||||||
|
)
|
||||||
|
.prefetch_related(
|
||||||
|
"mention_books",
|
||||||
|
"mention_users",
|
||||||
|
"attachments",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
paginated = Paginator(activities, PAGE_LENGTH)
|
paginated = Paginator(activities, PAGE_LENGTH)
|
||||||
|
|
Loading…
Reference in a new issue