Merge pull request #1921 from bookwyrm-social/author-page-query

Fixes author page query
This commit is contained in:
Mouse Reeve 2022-02-04 19:51:24 -08:00 committed by GitHub
commit d65a80d9ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View file

@ -50,6 +50,43 @@ class AuthorViews(TestCase):
models.SiteSettings.objects.create() models.SiteSettings.objects.create()
def test_author_page(self): def test_author_page(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica")
self.book.authors.add(author)
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_edition_author(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view()
another_book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=self.work,
isbn_13="9780300112511",
)
author = models.Author.objects.create(name="Jessica")
self.book.authors.add(author)
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)
books = result.context_data["books"]
self.assertEqual(books.object_list.count(), 1)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_author_page_empty(self):
"""there are so many views, this just makes sure it LOADS""" """there are so many views, this just makes sure it LOADS"""
view = views.Author.as_view() view = views.Author.as_view()
author = models.Author.objects.create(name="Jessica") author = models.Author.objects.create(name="Jessica")

View file

@ -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.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import 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
@ -25,9 +26,11 @@ 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( books = (
authors=author, editions__authors=author models.Work.objects.filter(Q(authors=author) | Q(editions__authors=author))
).distinct() .order_by("-published_date")
.distinct()
)
paginated = Paginator(books, PAGE_LENGTH) paginated = Paginator(books, PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page")) page = paginated.get_page(request.GET.get("page"))