moviewyrm/bookwyrm/views/shelf.py

201 lines
6.7 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" shelf views"""
from django.db import IntegrityError
2021-01-13 19:45:08 +00:00
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
2021-01-13 19:45:08 +00:00
from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.http import require_POST
from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.settings import PAGE_LENGTH
2021-01-13 19:45:08 +00:00
from .helpers import is_api_request, get_edition, get_user_from_username
from .helpers import handle_reading_status
# pylint: disable= no-self-use
class Shelf(View):
2021-03-08 16:49:10 +00:00
""" shelf page """
2021-01-13 19:45:08 +00:00
def get(self, request, username, shelf_identifier):
2021-03-08 16:49:10 +00:00
""" display a shelf """
2021-01-13 19:45:08 +00:00
try:
2021-02-23 20:41:37 +00:00
user = get_user_from_username(request.user, username)
2021-01-13 19:45:08 +00:00
except models.User.DoesNotExist:
return HttpResponseNotFound()
try:
page = int(request.GET.get("page", 1))
except ValueError:
page = 1
2021-01-13 19:45:08 +00:00
if shelf_identifier:
shelf = user.shelf_set.get(identifier=shelf_identifier)
else:
shelf = user.shelf_set.first()
is_self = request.user == user
shelves = user.shelf_set
if not is_self:
follower = user.followers.filter(id=request.user.id).exists()
# make sure the user has permission to view the shelf
2021-03-08 16:49:10 +00:00
if shelf.privacy == "direct" or (
shelf.privacy == "followers" and not follower
):
2021-01-13 19:45:08 +00:00
return HttpResponseNotFound()
# only show other shelves that should be visible
if follower:
2021-03-08 16:49:10 +00:00
shelves = shelves.filter(privacy__in=["public", "followers"])
2021-01-13 19:45:08 +00:00
else:
2021-03-08 16:49:10 +00:00
shelves = shelves.filter(privacy="public")
2021-01-13 19:45:08 +00:00
if is_api_request(request):
return ActivitypubResponse(shelf.to_activity(**request.GET))
paginated = Paginator(
2021-03-08 16:49:10 +00:00
models.ShelfBook.objects.filter(user=user, shelf=shelf)
.order_by("-updated_date")
.all(),
PAGE_LENGTH,
2021-03-08 16:49:10 +00:00
)
2021-01-13 19:45:08 +00:00
data = {
2021-03-08 16:49:10 +00:00
"user": user,
"is_self": is_self,
"shelves": shelves.all(),
"shelf": shelf,
"books": paginated.page(page),
2021-01-13 19:45:08 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "user/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-03-08 16:49:10 +00:00
""" edit a shelf """
2021-01-19 00:38:04 +00:00
try:
shelf = request.user.shelf_set.get(identifier=shelf_identifier)
except models.Shelf.DoesNotExist:
return HttpResponseNotFound()
2021-01-13 19:45:08 +00:00
if request.user != shelf.user:
return HttpResponseBadRequest()
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)
def user_shelves_page(request, username):
2021-03-08 16:49:10 +00:00
""" default shelf """
2021-01-13 19:45:08 +00:00
return Shelf.as_view()(request, username, None)
@login_required
@require_POST
def create_shelf(request):
2021-03-08 16:49:10 +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-08 16:49:10 +00:00
return redirect("/user/%s/shelf/%s" % (request.user.localname, shelf.identifier))
2021-01-13 19:45:08 +00:00
@login_required
@require_POST
def delete_shelf(request, shelf_id):
2021-03-08 16:49:10 +00:00
""" user generated shelves """
2021-01-13 19:45:08 +00:00
shelf = get_object_or_404(models.Shelf, id=shelf_id)
if request.user != shelf.user or not shelf.editable:
return HttpResponseBadRequest()
shelf.delete()
2021-03-08 16:49:10 +00:00
return redirect("/user/%s/shelves" % request.user.localname)
2021-01-13 19:45:08 +00:00
@login_required
@require_POST
def shelve(request):
""" put a book on a user's shelf """
2021-03-08 16:49:10 +00:00
book = get_edition(request.POST.get("book"))
2021-01-13 19:45:08 +00:00
desired_shelf = models.Shelf.objects.filter(
2021-03-08 16:49:10 +00:00
identifier=request.POST.get("shelf"), user=request.user
2021-01-13 19:45:08 +00:00
).first()
if not desired_shelf:
return HttpResponseNotFound()
2021-01-13 19:45:08 +00:00
change_from_current_identifier = request.POST.get("change-shelf-from")
if change_from_current_identifier is not None:
current_shelf = models.Shelf.objects.get(
user=request.user, identifier=change_from_current_identifier
)
handle_unshelve(book, current_shelf)
# 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:
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
):
handle_unshelve(book, current_read_status_shelfbook.shelf)
else: # It is already on the shelf
return redirect(request.headers.get("Referer", "/"))
models.ShelfBook.objects.create(
book=book, shelf=desired_shelf, user=request.user
)
if desired_shelf.identifier == models.Shelf.TO_READ and request.POST.get(
"post-status"
):
privacy = request.POST.get("privacy") or desired_shelf.privacy
handle_reading_status(request.user, desired_shelf, book, privacy=privacy)
else:
2021-01-13 19:45:08 +00:00
try:
models.ShelfBook.objects.create(
book=book, shelf=desired_shelf, user=request.user
)
# The book is already on this shelf. Might be good to alert, or reject the action?
except IntegrityError:
2021-01-13 19:45:08 +00:00
pass
return redirect(request.headers.get("Referer", "/"))
2021-01-13 19:45:08 +00:00
@login_required
@require_POST
def unshelve(request):
2021-03-08 16:49:10 +00:00
""" put a on a user's shelf """
book = models.Edition.objects.get(id=request.POST["book"])
current_shelf = models.Shelf.objects.get(id=request.POST["shelf"])
2021-01-13 19:45:08 +00:00
handle_unshelve(book, current_shelf)
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 19:45:08 +00:00
2021-03-08 16:49:10 +00:00
# pylint: disable=unused-argument
def handle_unshelve(book, shelf):
2021-03-08 16:49:10 +00:00
""" unshelve a book """
2021-01-13 19:45:08 +00:00
row = models.ShelfBook.objects.get(book=book, shelf=shelf)
row.delete()