Trying a new query to get author books

This commit is contained in:
Mouse Reeve 2022-01-20 15:07:39 -08:00
parent a8892d4dcc
commit d5ddaa874f
2 changed files with 5 additions and 24 deletions

View file

@ -141,12 +141,14 @@
<h2 class="title is-4">{% blocktrans with name=author.name %}Books by {{ name }}{% endblocktrans %}</h2>
<div class="columns is-multiline is-mobile">
{% for book in books %}
{% with book=book.default_edition %}
<div class="column is-one-fifth-tablet is-half-mobile is-flex is-flex-direction-column">
<div class="is-flex-grow-1">
{% include 'landing/small-book.html' with book=book %}
</div>
{% include 'snippets/shelve_button/shelve_button.html' with book=book %}
</div>
{% endwith %}
{% endfor %}
</div>

View file

@ -1,7 +1,6 @@
""" the good people stuff! the authors! """
from django.contrib.auth.decorators import login_required, permission_required
from django.core.paginator import Paginator
from django.db.models import OuterRef, Subquery, F, Q
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
@ -12,7 +11,6 @@ from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.utils import cache
from bookwyrm.views.helpers import is_api_request
@ -27,28 +25,9 @@ class Author(View):
if is_api_request(request):
return ActivitypubResponse(author.to_activity())
default_editions = models.Edition.objects.filter(
parent_work=OuterRef("parent_work")
).order_by("-edition_rank")
book_ids = cache.get_or_set(
f"author-books-{author.id}",
lambda a: models.Edition.objects.filter(
Q(authors=a) | Q(parent_work__authors=a)
)
.annotate(default_id=Subquery(default_editions.values("id")[:1]))
.filter(default_id=F("id"))
.distinct()
.values_list("id", flat=True),
author,
timeout=15552000,
)
books = (
models.Edition.objects.filter(id__in=book_ids)
.order_by("-published_date", "-first_published_date", "-created_date")
.prefetch_related("authors")
)
books = models.Work.objects.filter(
authors=author, editions__authors=author
).distinct()
paginated = Paginator(books, PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page"))