Query tweaks for book page

This commit is contained in:
Mouse Reeve 2021-05-22 20:14:57 -07:00
parent 2d4cec4422
commit 9285f483c2
2 changed files with 9 additions and 6 deletions

View file

@ -1,7 +1,7 @@
{% extends 'layout.html' %} {% extends 'layout.html' %}
{% load i18n %}{% load bookwyrm_tags %}{% load humanize %}{% load utilities %} {% load i18n %}{% load bookwyrm_tags %}{% load humanize %}{% load utilities %}
{% block title %}{{ book.title }}{% endblock %} {% block title %}{{ book|title }}{% endblock %}
{% block content %} {% block content %}
{% with user_authenticated=request.user.is_authenticated can_edit_book=perms.bookwyrm.edit_book %} {% with user_authenticated=request.user.is_authenticated can_edit_book=perms.bookwyrm.edit_book %}
@ -137,7 +137,7 @@
{# user's relationship to the book #} {# user's relationship to the book #}
<div class="block"> <div class="block">
{% for shelf in user_shelves %} {% for shelf in user_shelfbooks %}
<p> <p>
{% blocktrans with path=shelf.shelf.local_path shelf_name=shelf.shelf.name %}This edition is on your <a href="{{ path }}">{{ shelf_name }}</a> shelf.{% endblocktrans %} {% blocktrans with path=shelf.shelf.local_path shelf_name=shelf.shelf.name %}This edition is on your <a href="{{ path }}">{{ shelf_name }}</a> shelf.{% endblocktrans %}
{% include 'snippets/shelf_selector.html' with current=shelf.shelf %} {% include 'snippets/shelf_selector.html' with current=shelf.shelf %}

View file

@ -62,13 +62,16 @@ class Book(View):
queryset = queryset.filter(user=request.user) queryset = queryset.filter(user=request.user)
else: else:
queryset = reviews.exclude(Q(content__isnull=True) | Q(content="")) queryset = reviews.exclude(Q(content__isnull=True) | Q(content=""))
queryset = queryset.select_related("user")
paginated = Paginator(queryset, PAGE_LENGTH) paginated = Paginator(queryset, PAGE_LENGTH)
data = { data = {
"book": book, "book": book,
"statuses": paginated.get_page(request.GET.get("page")), "statuses": paginated.get_page(request.GET.get("page")),
"review_count": reviews.count(), "review_count": reviews.count(),
"ratings": reviews.filter(Q(content__isnull=True) | Q(content="")) "ratings": reviews.filter(
Q(content__isnull=True) | Q(content="")
).select_related("user")
if not user_statuses if not user_statuses
else None, else None,
"rating": reviews.aggregate(Avg("rating"))["rating__avg"], "rating": reviews.aggregate(Avg("rating"))["rating__avg"],
@ -89,15 +92,15 @@ class Book(View):
) )
data["readthroughs"] = readthroughs data["readthroughs"] = readthroughs
data["user_shelves"] = models.ShelfBook.objects.filter( data["user_shelfbooks"] = models.ShelfBook.objects.filter(
user=request.user, book=book user=request.user, book=book
) ).select_related("shelf")
data["other_edition_shelves"] = models.ShelfBook.objects.filter( data["other_edition_shelves"] = models.ShelfBook.objects.filter(
~Q(book=book), ~Q(book=book),
user=request.user, user=request.user,
book__parent_work=book.parent_work, book__parent_work=book.parent_work,
) ).select_related("shelf", "book")
data["user_statuses"] = { data["user_statuses"] = {
"review_count": book.review_set.filter(user=request.user).count(), "review_count": book.review_set.filter(user=request.user).count(),