mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-16 13:16:33 +00:00
Merge pull request #1557 from bookwyrm-social/author-page
Author page pagination
This commit is contained in:
commit
c366f57a8d
3 changed files with 52 additions and 9 deletions
|
@ -110,8 +110,14 @@
|
||||||
{% for book in books %}
|
{% for book in books %}
|
||||||
<div class="column is-one-fifth">
|
<div class="column is-one-fifth">
|
||||||
{% include 'landing/small-book.html' with book=book %}
|
{% include 'landing/small-book.html' with book=book %}
|
||||||
|
{% include 'snippets/shelve_button/shelve_button.html' with book=book %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{% include 'snippets/pagination.html' with page=books %}
|
||||||
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
""" test for app action functionality """
|
""" test for app action functionality """
|
||||||
from unittest.mock import patch
|
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.contrib.contenttypes.models import ContentType
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
|
@ -44,6 +45,8 @@ class AuthorViews(TestCase):
|
||||||
parent_work=self.work,
|
parent_work=self.work,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.anonymous_user = AnonymousUser
|
||||||
|
self.anonymous_user.is_authenticated = False
|
||||||
models.SiteSettings.objects.create()
|
models.SiteSettings.objects.create()
|
||||||
|
|
||||||
def test_author_page(self):
|
def test_author_page(self):
|
||||||
|
@ -51,15 +54,33 @@ class AuthorViews(TestCase):
|
||||||
view = views.Author.as_view()
|
view = views.Author.as_view()
|
||||||
author = models.Author.objects.create(name="Jessica")
|
author = models.Author.objects.create(name="Jessica")
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
is_api.return_value = False
|
is_api.return_value = False
|
||||||
result = view(request, author.id)
|
result = view(request, author.id)
|
||||||
self.assertIsInstance(result, TemplateResponse)
|
self.assertIsInstance(result, TemplateResponse)
|
||||||
validate_html(result.render())
|
validate_html(result.render())
|
||||||
self.assertEqual(result.status_code, 200)
|
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)
|
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 = self.factory.get("")
|
||||||
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
is_api.return_value = True
|
is_api.return_value = True
|
||||||
result = view(request, author.id)
|
result = view(request, author.id)
|
||||||
|
@ -78,7 +99,6 @@ class AuthorViews(TestCase):
|
||||||
self.assertIsInstance(result, TemplateResponse)
|
self.assertIsInstance(result, TemplateResponse)
|
||||||
validate_html(result.render())
|
validate_html(result.render())
|
||||||
self.assertEqual(result.status_code, 200)
|
self.assertEqual(result.status_code, 200)
|
||||||
self.assertEqual(result.status_code, 200)
|
|
||||||
|
|
||||||
def test_edit_author(self):
|
def test_edit_author(self):
|
||||||
"""edit an author"""
|
"""edit an author"""
|
||||||
|
@ -126,5 +146,5 @@ class AuthorViews(TestCase):
|
||||||
resp = view(request, author.id)
|
resp = view(request, author.id)
|
||||||
author.refresh_from_db()
|
author.refresh_from_db()
|
||||||
self.assertEqual(author.name, "Test Author")
|
self.assertEqual(author.name, "Test Author")
|
||||||
resp.render()
|
validate_html(resp.render())
|
||||||
self.assertEqual(resp.status_code, 200)
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
|
|
@ -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,27 @@ 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.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 = {
|
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