2021-09-27 22:55:55 +00:00
|
|
|
""" shelf views """
|
2021-03-31 17:23:20 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2021-09-27 22:55:55 +00:00
|
|
|
from django.db import IntegrityError, transaction
|
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 django.views.decorators.http import require_POST
|
|
|
|
|
|
|
|
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-09-27 22:55:55 +00:00
|
|
|
from .helpers import is_api_request, get_user_from_username
|
2021-06-18 21:12:56 +00:00
|
|
|
from .helpers import privacy_filter
|
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-09-27 18:27:46 +00:00
|
|
|
shelves = privacy_filter(request.user, user.shelf_set).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-05-23 02:54:50 +00:00
|
|
|
reviews = models.Review.objects.filter(
|
|
|
|
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-05-23 02:54:50 +00:00
|
|
|
if not is_self:
|
|
|
|
reviews = privacy_filter(request.user, reviews)
|
|
|
|
|
|
|
|
books = books.annotate(
|
2021-07-14 04:25:17 +00:00
|
|
|
rating=Subquery(reviews.values("rating")[:1]),
|
|
|
|
shelved_date=F("shelfbook__shelved_date"),
|
2021-05-23 02:54:50 +00:00
|
|
|
).prefetch_related("authors")
|
2021-05-11 20:54:38 +00:00
|
|
|
|
2021-03-29 18:48:19 +00:00
|
|
|
paginated = Paginator(
|
2021-05-23 02:54:50 +00:00
|
|
|
books.order_by("-shelfbook__updated_date"),
|
2021-03-29 18:48:19 +00:00
|
|
|
PAGE_LENGTH,
|
2021-03-08 16:49:10 +00:00
|
|
|
)
|
2021-01-13 19:45:08 +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-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)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def create_shelf(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""user generated shelves"""
|
2021-01-13 19:45:08 +00:00
|
|
|
form = forms.ShelfForm(request.POST)
|
|
|
|
if not form.is_valid():
|
2021-03-08 16:49:10 +00:00
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
shelf = form.save()
|
2021-03-31 22:03:07 +00:00
|
|
|
return redirect(shelf.local_path)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def delete_shelf(request, shelf_id):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""user generated shelves"""
|
2021-01-13 19:45:08 +00:00
|
|
|
shelf = get_object_or_404(models.Shelf, id=shelf_id)
|
2021-09-29 00:02:18 +00:00
|
|
|
shelf.raise_not_deletable(request.user)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
shelf.delete()
|
2021-03-31 22:03:07 +00:00
|
|
|
return redirect("user-shelves", request.user.localname)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
2021-09-27 22:55:55 +00:00
|
|
|
@transaction.atomic
|
2021-01-13 19:45:08 +00:00
|
|
|
def shelve(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""put a book on a user's shelf"""
|
2021-09-27 22:55:55 +00:00
|
|
|
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
|
|
|
desired_shelf = get_object_or_404(
|
|
|
|
request.user.shelf_set, identifier=request.POST.get("shelf")
|
|
|
|
)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
2021-09-27 22:55:55 +00:00
|
|
|
# first we need to remove from the specified shelf
|
2021-03-25 23:57:20 +00:00
|
|
|
change_from_current_identifier = request.POST.get("change-shelf-from")
|
2021-09-28 00:58:21 +00:00
|
|
|
if change_from_current_identifier:
|
2021-09-27 23:08:52 +00:00
|
|
|
# find the shelfbook obj and delete it
|
2021-09-27 22:55:55 +00:00
|
|
|
get_object_or_404(
|
|
|
|
models.ShelfBook,
|
|
|
|
book=book,
|
|
|
|
user=request.user,
|
2021-09-27 23:08:52 +00:00
|
|
|
shelf__identifier=change_from_current_identifier,
|
2021-09-27 22:55:55 +00:00
|
|
|
).delete()
|
2021-03-25 23:57:20 +00:00
|
|
|
|
|
|
|
# A book can be on multiple shelves, but only on one read status shelf at a time
|
|
|
|
if desired_shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS:
|
2021-09-27 22:55:55 +00:00
|
|
|
# figure out where state shelf it's currently on (if any)
|
2021-03-25 23:57:20 +00:00
|
|
|
current_read_status_shelfbook = (
|
|
|
|
models.ShelfBook.objects.select_related("shelf")
|
|
|
|
.filter(
|
|
|
|
shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS,
|
|
|
|
user=request.user,
|
|
|
|
book=book,
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if current_read_status_shelfbook is not None:
|
|
|
|
if (
|
|
|
|
current_read_status_shelfbook.shelf.identifier
|
|
|
|
!= desired_shelf.identifier
|
|
|
|
):
|
2021-09-27 22:55:55 +00:00
|
|
|
current_read_status_shelfbook.delete()
|
2021-03-25 23:57:20 +00:00
|
|
|
else: # It is already on the shelf
|
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
|
|
|
|
2021-09-27 22:55:55 +00:00
|
|
|
# create the new shelf-book entry
|
2021-03-25 23:57:20 +00:00
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=book, shelf=desired_shelf, user=request.user
|
|
|
|
)
|
|
|
|
else:
|
2021-09-27 22:55:55 +00:00
|
|
|
# we're putting it on a custom shelf
|
2021-01-13 19:45:08 +00:00
|
|
|
try:
|
2021-03-25 23:57:20 +00:00
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=book, shelf=desired_shelf, user=request.user
|
|
|
|
)
|
2021-03-31 17:23:20 +00:00
|
|
|
# The book is already on this shelf.
|
|
|
|
# Might be good to alert, or reject the action?
|
2021-03-25 23:57:20 +00:00
|
|
|
except IntegrityError:
|
2021-01-13 19:45:08 +00:00
|
|
|
pass
|
2021-03-25 23:57:20 +00:00
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def unshelve(request):
|
2021-09-27 22:55:55 +00:00
|
|
|
"""put a on a user's shelf"""
|
|
|
|
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
|
|
|
shelf_book = get_object_or_404(
|
|
|
|
models.ShelfBook, book=book, shelf__id=request.POST["shelf"]
|
|
|
|
)
|
|
|
|
shelf_book.raise_not_deletable(request.user)
|
|
|
|
|
|
|
|
shelf_book.delete()
|
2021-03-08 16:49:10 +00:00
|
|
|
return redirect(request.headers.get("Referer", "/"))
|