mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-16 13:16:33 +00:00
Paginate books on author page
This commit is contained in:
parent
74b697d844
commit
89a385da0a
1 changed files with 22 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
""" the good people stuff! the authors! """
|
""" the good people stuff! the authors! """
|
||||||
from django.contrib.auth.decorators import login_required, permission_required
|
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.shortcuts import get_object_or_404, redirect
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
@ -8,7 +9,8 @@ from django.views import View
|
||||||
|
|
||||||
from bookwyrm import forms, models
|
from bookwyrm import forms, models
|
||||||
from bookwyrm.activitypub import ActivitypubResponse
|
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
|
# pylint: disable= no-self-use
|
||||||
|
@ -22,12 +24,26 @@ class Author(View):
|
||||||
if is_api_request(request):
|
if is_api_request(request):
|
||||||
return ActivitypubResponse(author.to_activity())
|
return ActivitypubResponse(author.to_activity())
|
||||||
|
|
||||||
books = models.Work.objects.filter(
|
default_editions = models.Edition.objects.filter(
|
||||||
Q(authors=author) | Q(editions__authors=author)
|
parent_work=OuterRef("parent_work")
|
||||||
).distinct()
|
).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 = {
|
data = {
|
||||||
"author": author,
|
"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)
|
return TemplateResponse(request, "author/author.html", data)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue