From d5ddaa874fab1360aa211c501c67736681b16131 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 20 Jan 2022 15:07:39 -0800 Subject: [PATCH] Trying a new query to get author books --- bookwyrm/templates/author/author.html | 2 ++ bookwyrm/views/author.py | 27 +++------------------------ 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/bookwyrm/templates/author/author.html b/bookwyrm/templates/author/author.html index 8061d580c..afbf31784 100644 --- a/bookwyrm/templates/author/author.html +++ b/bookwyrm/templates/author/author.html @@ -141,12 +141,14 @@

{% blocktrans with name=author.name %}Books by {{ name }}{% endblocktrans %}

{% for book in books %} + {% with book=book.default_edition %}
{% include 'landing/small-book.html' with book=book %}
{% include 'snippets/shelve_button/shelve_button.html' with book=book %}
+ {% endwith %} {% endfor %}
diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index 9bc95b79d..3310fef0e 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -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"))