2021-09-27 22:55:55 +00:00
|
|
|
""" shelf views """
|
2021-03-31 17:23:20 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2021-07-14 04:25:17 +00:00
|
|
|
from django.db.models import OuterRef, Subquery, F
|
2021-01-13 19:45:08 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-03-29 18:48:19 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-09-27 22:55:55 +00:00
|
|
|
from django.http import HttpResponseBadRequest
|
2021-01-13 19:45:08 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
2021-08-17 21:35:28 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-01-13 19:45:08 +00:00
|
|
|
from django.views import View
|
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
2021-03-29 18:48:19 +00:00
|
|
|
from bookwyrm.settings import PAGE_LENGTH
|
2021-10-20 20:15:43 +00:00
|
|
|
from bookwyrm.views.helpers import is_api_request, get_user_from_username
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
|
2021-06-08 18:10:39 +00:00
|
|
|
# pylint: disable=no-self-use
|
2021-01-13 19:45:08 +00:00
|
|
|
class Shelf(View):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelf page"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-03-31 17:23:20 +00:00
|
|
|
def get(self, request, username, shelf_identifier=None):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""display a shelf"""
|
2021-04-30 16:33:36 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-05-23 02:54:50 +00:00
|
|
|
is_self = user == request.user
|
|
|
|
|
|
|
|
if is_self:
|
2021-09-27 18:27:46 +00:00
|
|
|
shelves = user.shelf_set.all()
|
2021-05-23 02:54:50 +00:00
|
|
|
else:
|
2021-10-06 17:37:09 +00:00
|
|
|
shelves = models.Shelf.privacy_filter(request.user).filter(user=user).all()
|
2021-03-31 17:23:20 +00:00
|
|
|
|
|
|
|
# get the shelf and make sure the logged in user should be able to see it
|
2021-01-13 19:45:08 +00:00
|
|
|
if shelf_identifier:
|
2021-09-27 22:55:55 +00:00
|
|
|
shelf = get_object_or_404(user.shelf_set, identifier=shelf_identifier)
|
|
|
|
shelf.raise_visible_to_user(request.user)
|
2021-05-11 20:54:38 +00:00
|
|
|
books = shelf.books
|
2021-01-13 19:45:08 +00:00
|
|
|
else:
|
2021-09-27 22:55:55 +00:00
|
|
|
# this is a constructed "all books" view, with a fake "shelf" obj
|
2021-03-31 17:32:50 +00:00
|
|
|
FakeShelf = namedtuple(
|
|
|
|
"Shelf", ("identifier", "name", "user", "books", "privacy")
|
|
|
|
)
|
2021-09-27 18:13:47 +00:00
|
|
|
books = (
|
|
|
|
models.Edition.viewer_aware_objects(request.user)
|
|
|
|
.filter(
|
|
|
|
# privacy is ensured because the shelves are already filtered above
|
2021-09-27 18:27:46 +00:00
|
|
|
shelfbook__shelf__in=shelves
|
2021-09-27 18:13:47 +00:00
|
|
|
)
|
|
|
|
.distinct()
|
|
|
|
)
|
2021-03-31 17:32:50 +00:00
|
|
|
shelf = FakeShelf("all", _("All books"), user, books, "public")
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
if is_api_request(request):
|
|
|
|
return ActivitypubResponse(shelf.to_activity(**request.GET))
|
|
|
|
|
2021-10-06 17:37:09 +00:00
|
|
|
reviews = models.Review.objects
|
|
|
|
if not is_self:
|
|
|
|
reviews = models.Review.privacy_filter(request.user)
|
|
|
|
|
|
|
|
reviews = reviews.filter(
|
2021-05-23 02:54:50 +00:00
|
|
|
user=user,
|
|
|
|
rating__isnull=False,
|
|
|
|
book__id=OuterRef("id"),
|
2021-05-23 15:48:00 +00:00
|
|
|
deleted=False,
|
2021-05-11 20:54:38 +00:00
|
|
|
).order_by("-published_date")
|
|
|
|
|
2021-10-11 20:22:12 +00:00
|
|
|
reading = models.ReadThrough.objects
|
|
|
|
|
|
|
|
reading = reading.filter(user=user, book__id=OuterRef("id")).order_by(
|
2021-10-14 10:39:15 +00:00
|
|
|
"start_date"
|
2021-10-11 20:22:12 +00:00
|
|
|
)
|
|
|
|
|
2021-05-23 02:54:50 +00:00
|
|
|
books = books.annotate(
|
2021-07-14 04:25:17 +00:00
|
|
|
rating=Subquery(reviews.values("rating")[:1]),
|
|
|
|
shelved_date=F("shelfbook__shelved_date"),
|
2021-10-14 10:39:15 +00:00
|
|
|
start_date=Subquery(reading.values("start_date")[:1]),
|
|
|
|
finish_date=Subquery(reading.values("finish_date")[:1]),
|
|
|
|
author=Subquery(
|
|
|
|
models.Book.objects.filter(id=OuterRef("id")).values("authors__name")[
|
|
|
|
:1
|
|
|
|
]
|
|
|
|
),
|
|
|
|
).prefetch_related("authors")
|
2021-10-11 20:22:12 +00:00
|
|
|
|
|
|
|
books = sort_books(books, request.GET.get("sort"))
|
2021-05-11 20:54:38 +00:00
|
|
|
|
2021-03-29 18:48:19 +00:00
|
|
|
paginated = Paginator(
|
2021-10-11 20:22:12 +00:00
|
|
|
books,
|
2021-03-29 18:48:19 +00:00
|
|
|
PAGE_LENGTH,
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-05-03 21:47:27 +00:00
|
|
|
page = paginated.get_page(request.GET.get("page"))
|
2021-01-13 19:45:08 +00:00
|
|
|
data = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"user": user,
|
2021-05-23 02:54:50 +00:00
|
|
|
"is_self": is_self,
|
2021-09-27 18:27:46 +00:00
|
|
|
"shelves": shelves,
|
2021-03-08 16:49:10 +00:00
|
|
|
"shelf": shelf,
|
2021-05-03 21:47:27 +00:00
|
|
|
"books": page,
|
2021-09-29 00:17:01 +00:00
|
|
|
"edit_form": forms.ShelfForm(instance=shelf if shelf_identifier else None),
|
2021-09-28 23:37:24 +00:00
|
|
|
"create_form": forms.ShelfForm(),
|
2021-10-11 20:22:12 +00:00
|
|
|
"sort": request.GET.get("sort"),
|
2021-05-03 21:47:27 +00:00
|
|
|
"page_range": paginated.get_elided_page_range(
|
|
|
|
page.number, on_each_side=2, on_ends=1
|
2021-05-03 21:52:24 +00:00
|
|
|
),
|
2021-01-13 19:45:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 23:04:08 +00:00
|
|
|
return TemplateResponse(request, "shelf/shelf.html", data)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-01-27 17:31:01 +00:00
|
|
|
# pylint: disable=unused-argument
|
2021-01-19 00:38:04 +00:00
|
|
|
def post(self, request, username, shelf_identifier):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""edit a shelf"""
|
2021-09-27 22:55:55 +00:00
|
|
|
user = get_user_from_username(request.user, username)
|
|
|
|
shelf = get_object_or_404(user.shelf_set, identifier=shelf_identifier)
|
|
|
|
shelf.raise_not_editable(request.user)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-09-27 22:55:55 +00:00
|
|
|
# you can't change the name of the default shelves
|
2021-03-08 16:49:10 +00:00
|
|
|
if not shelf.editable and request.POST.get("name") != shelf.name:
|
2021-01-13 19:45:08 +00:00
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
form = forms.ShelfForm(request.POST, instance=shelf)
|
|
|
|
if not form.is_valid():
|
|
|
|
return redirect(shelf.local_path)
|
|
|
|
shelf = form.save()
|
|
|
|
return redirect(shelf.local_path)
|
|
|
|
|
|
|
|
|
2021-10-11 20:22:12 +00:00
|
|
|
def sort_books(books, sort):
|
|
|
|
"""Books in shelf sorting"""
|
|
|
|
sort_fields = [
|
|
|
|
"title",
|
|
|
|
"author",
|
|
|
|
"shelved_date",
|
|
|
|
"start_date",
|
|
|
|
"finish_date",
|
|
|
|
"rating",
|
|
|
|
]
|
|
|
|
|
|
|
|
if sort in sort_fields:
|
|
|
|
books = books.order_by(sort)
|
|
|
|
elif sort and sort[1:] in sort_fields:
|
|
|
|
books = books.order_by(F(sort[1:]).desc(nulls_last=True))
|
|
|
|
else:
|
|
|
|
books = books.order_by("-shelved_date")
|
|
|
|
return books
|