Merge pull request #1557 from bookwyrm-social/author-page

Author page pagination
This commit is contained in:
Mouse Reeve 2021-10-21 08:02:50 -07:00 committed by GitHub
commit c366f57a8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 9 deletions

View file

@ -110,8 +110,14 @@
{% for book in books %}
<div class="column is-one-fifth">
{% include 'landing/small-book.html' with book=book %}
{% include 'snippets/shelve_button/shelve_button.html' with book=book %}
</div>
{% endfor %}
</div>
</div>
<div>
{% include 'snippets/pagination.html' with page=books %}
</div>
{% endblock %}

View file

@ -1,6 +1,7 @@
""" test for app action functionality """
from unittest.mock import patch
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.models import AnonymousUser, Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse
@ -44,6 +45,8 @@ class AuthorViews(TestCase):
parent_work=self.work,
)
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
models.SiteSettings.objects.create()
def test_author_page(self):
@ -51,15 +54,33 @@ class AuthorViews(TestCase):
view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica")
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.author.is_api_request") as is_api:
is_api.return_value = False
result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_author_page_logged_out(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica")
request = self.factory.get("")
request.user = self.anonymous_user
with patch("bookwyrm.views.author.is_api_request") as is_api:
is_api.return_value = False
result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_author_page_api_response(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica")
request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.author.is_api_request") as is_api:
is_api.return_value = True
result = view(request, author.id)
@ -78,7 +99,6 @@ class AuthorViews(TestCase):
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
self.assertEqual(result.status_code, 200)
def test_edit_author(self):
"""edit an author"""
@ -126,5 +146,5 @@ class AuthorViews(TestCase):
resp = view(request, author.id)
author.refresh_from_db()
self.assertEqual(author.name, "Test Author")
resp.render()
validate_html(resp.render())
self.assertEqual(resp.status_code, 200)

View file

@ -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,27 @@ 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.viewer_aware_objects(request.user)
.filter(Q(authors=author) | Q(parent_work__authors=author))
.annotate(default_id=Subquery(default_editions.values("id")[:1]))
.filter(default_id=F("id"))
.order_by("-first_published_date", "-published_date", "-created_date")
.prefetch_related("authors")
)
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)