mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
User viewer aware objects for book page
Plus other refactors for that view
This commit is contained in:
parent
6d06edc2c7
commit
995e2c47db
2 changed files with 30 additions and 18 deletions
|
@ -8,7 +8,7 @@ from django.core.files.base import ContentFile
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import Avg, Q
|
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.shortcuts import get_object_or_404, redirect
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.utils.datastructures import MultiValueDictKeyError
|
from django.utils.datastructures import MultiValueDictKeyError
|
||||||
|
@ -30,25 +30,33 @@ class Book(View):
|
||||||
|
|
||||||
def get(self, request, book_id, user_statuses=False):
|
def get(self, request, book_id, user_statuses=False):
|
||||||
"""info about a book"""
|
"""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):
|
if is_api_request(request):
|
||||||
|
book = get_object_or_404(
|
||||||
|
models.Book.objects.select_subclasses(), id=book_id
|
||||||
|
)
|
||||||
return ActivitypubResponse(book.to_activity())
|
return ActivitypubResponse(book.to_activity())
|
||||||
|
|
||||||
if isinstance(book, models.Work):
|
user_statuses = user_statuses if request.user.is_authenticated else False
|
||||||
book = book.default_edition
|
|
||||||
|
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:
|
if not book or not book.parent_work:
|
||||||
return HttpResponseNotFound()
|
raise Http404
|
||||||
|
|
||||||
work = book.parent_work
|
# all reviews for all editions of the book
|
||||||
|
|
||||||
# all reviews for the book
|
|
||||||
reviews = privacy_filter(
|
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
|
# the reviews to show
|
||||||
|
|
|
@ -49,10 +49,14 @@ class Shelf(View):
|
||||||
FakeShelf = namedtuple(
|
FakeShelf = namedtuple(
|
||||||
"Shelf", ("identifier", "name", "user", "books", "privacy")
|
"Shelf", ("identifier", "name", "user", "books", "privacy")
|
||||||
)
|
)
|
||||||
books = models.Edition.viewer_aware_objects(request.user).filter(
|
books = (
|
||||||
# privacy is ensured because the shelves are already filtered above
|
models.Edition.viewer_aware_objects(request.user)
|
||||||
shelfbook__shelf__in=shelves.all()
|
.filter(
|
||||||
).distinct()
|
# privacy is ensured because the shelves are already filtered above
|
||||||
|
shelfbook__shelf__in=shelves.all()
|
||||||
|
)
|
||||||
|
.distinct()
|
||||||
|
)
|
||||||
shelf = FakeShelf("all", _("All books"), user, books, "public")
|
shelf = FakeShelf("all", _("All books"), user, books, "public")
|
||||||
|
|
||||||
if is_api_request(request):
|
if is_api_request(request):
|
||||||
|
|
Loading…
Reference in a new issue