From 89a385da0a96eece560aa569f4cb95c98b6e8dfe Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 17:36:52 -0700 Subject: [PATCH 1/7] Paginate books on author page --- bookwyrm/views/author.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index e1e9247de..1265ad689 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) From 3eb3225d2c629abe72ca82f2ac09fa7413c9d6e5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 17:42:19 -0700 Subject: [PATCH 2/7] Adds pagination to the template --- bookwyrm/templates/author/author.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bookwyrm/templates/author/author.html b/bookwyrm/templates/author/author.html index 8a15cd0f0..5310f0df8 100644 --- a/bookwyrm/templates/author/author.html +++ b/bookwyrm/templates/author/author.html @@ -114,4 +114,9 @@ {% endfor %} + +
+ {% include 'snippets/pagination.html' with page=books %} +
+ {% endblock %} From de93beca84e5f2dcee9922486d99b6cc662a4525 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 17:51:42 -0700 Subject: [PATCH 3/7] Adds shelve buttons to books on author page --- bookwyrm/templates/author/author.html | 1 + bookwyrm/views/author.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bookwyrm/templates/author/author.html b/bookwyrm/templates/author/author.html index 5310f0df8..6a67b50b3 100644 --- a/bookwyrm/templates/author/author.html +++ b/bookwyrm/templates/author/author.html @@ -110,6 +110,7 @@ {% for book in books %}
{% include 'landing/small-book.html' with book=book %} + {% include 'snippets/shelve_button/shelve_button.html' with book=book %}
{% endfor %} diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index 1265ad689..9a3d582d6 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -29,7 +29,7 @@ class Author(View): ).order_by("-edition_rank") books = ( - models.Edition.objects.filter( + 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])) From 14682ed8c687590b3ebb5be299a9399419fec0db Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 18:04:29 -0700 Subject: [PATCH 4/7] Prefect related data in author view --- bookwyrm/views/author.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index 9a3d582d6..4f29e2af6 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -34,7 +34,7 @@ class Author(View): ) .annotate(default_id=Subquery(default_editions.values("id")[:1])) .filter(default_id=F("id")) - ) + ).prefetch_related("authors") paginated = Paginator(books, PAGE_LENGTH) page = paginated.get_page(request.GET.get("page")) From d706b26ac98b3fbb0b2e82d1d2423defbe3168e4 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 18:11:31 -0700 Subject: [PATCH 5/7] Python formatting --- bookwyrm/views/author.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index 4f29e2af6..4cb7ac2b5 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -29,9 +29,8 @@ class Author(View): ).order_by("-edition_rank") books = ( - models.Edition.viewer_aware_objects(request.user).filter( - Q(authors=author) | Q(parent_work__authors=author) - ) + 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")) ).prefetch_related("authors") From 72dc21e82ab7c076eda0b984ea9f9ad57c687a4b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 18:27:19 -0700 Subject: [PATCH 6/7] Adds tests and fixes unset ordering warnings --- bookwyrm/tests/views/test_author.py | 27 +++++++++++++++++++++++++-- bookwyrm/views/author.py | 4 +++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/bookwyrm/tests/views/test_author.py b/bookwyrm/tests/views/test_author.py index 03c027fae..75c7433fe 100644 --- a/bookwyrm/tests/views/test_author.py +++ b/bookwyrm/tests/views/test_author.py @@ -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,6 +54,7 @@ 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) @@ -59,7 +63,26 @@ class AuthorViews(TestCase): 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.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) @@ -126,5 +149,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) diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index 4cb7ac2b5..6c3ee36ff 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -33,7 +33,9 @@ class Author(View): .filter(Q(authors=author) | Q(parent_work__authors=author)) .annotate(default_id=Subquery(default_editions.values("id")[:1])) .filter(default_id=F("id")) - ).prefetch_related("authors") + .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")) From 278a9de673b54638342a31ea94f0fa5b0a08ae87 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 20 Oct 2021 18:29:00 -0700 Subject: [PATCH 7/7] Removes duplicate assertions in author view test --- bookwyrm/tests/views/test_author.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bookwyrm/tests/views/test_author.py b/bookwyrm/tests/views/test_author.py index 75c7433fe..ccbfe5493 100644 --- a/bookwyrm/tests/views/test_author.py +++ b/bookwyrm/tests/views/test_author.py @@ -61,7 +61,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_author_page_logged_out(self): """there are so many views, this just makes sure it LOADS""" @@ -75,7 +74,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_author_page_api_response(self): """there are so many views, this just makes sure it LOADS""" @@ -101,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"""