diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index e1e9247d..1265ad68 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -1,6 +1,7 @@ """ the good people stuff! the authors! """ from django.contrib.auth.decorators import login_required, permission_required -from django.db.models import Q +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 @@ -8,7 +9,8 @@ from django.views import View from bookwyrm import forms, models from bookwyrm.activitypub import ActivitypubResponse -from .helpers import is_api_request +from bookwyrm.settings import PAGE_LENGTH +from bookwyrm.views.helpers import is_api_request # pylint: disable= no-self-use @@ -22,12 +24,26 @@ class Author(View): if is_api_request(request): return ActivitypubResponse(author.to_activity()) - books = models.Work.objects.filter( - Q(authors=author) | Q(editions__authors=author) - ).distinct() + default_editions = models.Edition.objects.filter( + parent_work=OuterRef("parent_work") + ).order_by("-edition_rank") + + books = ( + models.Edition.objects.filter( + Q(authors=author) | Q(parent_work__authors=author) + ) + .annotate(default_id=Subquery(default_editions.values("id")[:1])) + .filter(default_id=F("id")) + ) + + paginated = Paginator(books, PAGE_LENGTH) + page = paginated.get_page(request.GET.get("page")) data = { "author": author, - "books": [b.default_edition for b in books], + "books": page, + "page_range": paginated.get_elided_page_range( + page.number, on_each_side=2, on_ends=1 + ), } return TemplateResponse(request, "author/author.html", data)