diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index a31a99e71..7e7e90ed8 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -8,7 +8,7 @@ from django.core.files.base import ContentFile from django.core.paginator import Paginator from django.db import transaction from django.db.models import Avg, Q -from django.http import HttpResponseBadRequest, HttpResponseNotFound +from django.http import HttpResponseBadRequest, Http404 from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.datastructures import MultiValueDictKeyError @@ -30,25 +30,33 @@ class Book(View): def get(self, request, book_id, user_statuses=False): """info about a book""" - user_statuses = user_statuses if request.user.is_authenticated else False - try: - book = models.Book.objects.select_subclasses().get(id=book_id) - except models.Book.DoesNotExist: - return HttpResponseNotFound() - if is_api_request(request): + book = get_object_or_404( + models.Book.objects.select_subclasses(), id=book_id + ) return ActivitypubResponse(book.to_activity()) - if isinstance(book, models.Work): - book = book.default_edition + user_statuses = user_statuses if request.user.is_authenticated else False + + try: + book = models.Edition.viewer_aware_objects(request.user).filter(id=book_id) + except models.Edition.DoesNotExist: + book = ( + models.Edition.viewer_aware_objects(request.user) + .filter( + parent_work__id=book_id, + ) + .order_by("-edition_rank") + ) + book = book.select_related("parent_work").prefetch_related("authors") + book = book.first() + if not book or not book.parent_work: - return HttpResponseNotFound() + raise Http404 - work = book.parent_work - - # all reviews for the book + # all reviews for all editions of the book reviews = privacy_filter( - request.user, models.Review.objects.filter(book__in=work.editions.all()) + request.user, models.Review.objects.filter(book__parent_work__editions=book) ) # the reviews to show diff --git a/bookwyrm/views/shelf.py b/bookwyrm/views/shelf.py index ae67a0c49..26d0f48de 100644 --- a/bookwyrm/views/shelf.py +++ b/bookwyrm/views/shelf.py @@ -49,10 +49,14 @@ class Shelf(View): FakeShelf = namedtuple( "Shelf", ("identifier", "name", "user", "books", "privacy") ) - books = models.Edition.viewer_aware_objects(request.user).filter( - # privacy is ensured because the shelves are already filtered above - shelfbook__shelf__in=shelves.all() - ).distinct() + books = ( + models.Edition.viewer_aware_objects(request.user) + .filter( + # privacy is ensured because the shelves are already filtered above + shelfbook__shelf__in=shelves.all() + ) + .distinct() + ) shelf = FakeShelf("all", _("All books"), user, books, "public") if is_api_request(request):