moviewyrm/bookwyrm/views/reading.py

215 lines
8 KiB
Python
Raw Permalink Normal View History

2021-03-08 16:49:10 +00:00
""" the good stuff! the books! """
2022-05-26 17:58:11 +00:00
import logging
2021-01-13 20:33:48 +00:00
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
2021-09-22 22:59:48 +00:00
from django.db import transaction
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
2021-01-13 20:33:48 +00:00
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
2021-01-13 20:33:48 +00:00
from django.views.decorators.http import require_POST
2022-01-11 17:50:04 +00:00
from bookwyrm import forms, models
from bookwyrm.views.shelf.shelf_actions import unshelve
from .status import CreateStatus
from .helpers import get_edition, handle_reading_status, is_api_request
from .helpers import load_date_in_user_tz_as_utc
2021-01-13 20:33:48 +00:00
2022-05-26 17:58:11 +00:00
logger = logging.getLogger(__name__)
2021-01-13 20:33:48 +00:00
2021-06-08 18:23:53 +00:00
# pylint: disable=no-self-use
2021-11-20 08:19:51 +00:00
# pylint: disable=too-many-return-statements
2022-01-11 17:50:04 +00:00
@method_decorator(login_required, name="dispatch")
class ReadingStatus(View):
2021-06-08 18:23:53 +00:00
"""consider reading a book"""
def get(self, request, status, book_id):
"""modal page"""
book = get_edition(book_id)
template = {
"want": "want.html",
2021-06-09 18:05:15 +00:00
"start": "start.html",
"finish": "finish.html",
2022-02-11 13:33:46 +00:00
"stop": "stop.html",
}.get(status)
if not template:
return HttpResponseNotFound()
2021-09-05 22:14:57 +00:00
# redirect if we're already on this shelf
return TemplateResponse(request, f"reading_progress/{template}", {"book": book})
2022-05-26 17:58:11 +00:00
@transaction.atomic
def post(self, request, status, book_id):
2021-09-22 22:59:48 +00:00
"""Change the state of a book by shelving it and adding reading dates"""
identifier = {
"want": models.Shelf.TO_READ,
"start": models.Shelf.READING,
"finish": models.Shelf.READ_FINISHED,
"stop": models.Shelf.STOPPED_READING,
}.get(status)
if not identifier:
2022-05-26 17:58:11 +00:00
logger.exception("Invalid reading status type: %s", status)
return HttpResponseBadRequest()
2022-01-09 00:38:52 +00:00
# invalidate related caches
cache.delete(f"active_shelf-{request.user.id}-{book_id}")
2021-09-27 23:08:52 +00:00
desired_shelf = get_object_or_404(
models.Shelf, identifier=identifier, user=request.user
)
2021-09-22 22:59:48 +00:00
book = (
models.Edition.viewer_aware_objects(request.user)
.prefetch_related("shelfbook_set__shelf")
.get(id=book_id)
)
2021-09-22 22:59:48 +00:00
# gets the first shelf that indicates a reading status, or None
shelves = [
s
for s in book.current_shelves
if s.shelf.identifier in models.Shelf.READ_STATUS_IDENTIFIERS
]
current_status_shelfbook = shelves[0] if shelves else None
2021-09-05 22:14:57 +00:00
2021-09-22 22:59:48 +00:00
# checking the referer prevents redirecting back to the modal page
2021-09-05 22:14:57 +00:00
referer = request.headers.get("Referer", "/")
referer = "/" if "reading-status" in referer else referer
if current_status_shelfbook is not None:
if current_status_shelfbook.shelf.identifier != desired_shelf.identifier:
current_status_shelfbook.delete()
else: # It already was on the shelf
2021-09-05 22:14:57 +00:00
return redirect(referer)
models.ShelfBook.objects.create(
book=book, shelf=desired_shelf, user=request.user
)
2021-01-13 20:33:48 +00:00
2021-09-22 22:59:48 +00:00
update_readthrough_on_shelve(
request.user,
book,
desired_shelf.identifier,
start_date=request.POST.get("start_date"),
finish_date=request.POST.get("finish_date"),
stopped_date=request.POST.get("stopped_date"),
2021-09-22 22:59:48 +00:00
)
# post about it (if you want)
if request.POST.get("post-status"):
# is it a comment?
if request.POST.get("content"):
return CreateStatus.as_view()(request, "comment")
privacy = request.POST.get("privacy")
handle_reading_status(request.user, desired_shelf, book, privacy)
# if the request includes a "shelf" value we are using the 'move' button
2021-11-20 05:06:16 +00:00
if bool(request.POST.get("shelf")):
# unshelve the existing shelf
this_shelf = request.POST.get("shelf")
if (
2021-11-20 05:06:16 +00:00
bool(current_status_shelfbook)
and int(this_shelf) != int(current_status_shelfbook.shelf.id)
and current_status_shelfbook.shelf.identifier
!= desired_shelf.identifier
):
2021-11-26 06:39:35 +00:00
return unshelve(request, book_id=book_id)
if is_api_request(request):
return HttpResponse()
2021-09-05 22:14:57 +00:00
return redirect(referer)
2021-01-13 20:33:48 +00:00
2022-01-11 17:50:04 +00:00
@method_decorator(login_required, name="dispatch")
2022-01-11 18:40:32 +00:00
class ReadThrough(View):
2022-01-11 17:50:04 +00:00
"""Add new read dates"""
2022-01-11 18:41:33 +00:00
2022-01-11 18:40:32 +00:00
def get(self, request, book_id, readthrough_id=None):
2022-01-11 17:50:04 +00:00
"""standalone form in case of errors"""
2022-01-11 18:40:32 +00:00
book = get_object_or_404(models.Edition, id=book_id)
form = forms.ReadThroughForm()
data = {"form": form, "book": book}
if readthrough_id:
data["readthrough"] = get_object_or_404(
models.ReadThrough, id=readthrough_id
)
return TemplateResponse(request, "readthrough/readthrough.html", data)
2022-01-11 17:50:04 +00:00
def post(self, request):
"""can't use the form normally because the dates are too finnicky"""
book_id = request.POST.get("book")
2022-01-11 17:50:04 +00:00
normalized_post = request.POST.copy()
normalized_post["start_date"] = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
normalized_post["finish_date"] = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
form = forms.ReadThroughForm(request.POST)
2022-01-11 17:50:04 +00:00
if not form.is_valid():
book = get_object_or_404(models.Edition, id=book_id)
data = {"form": form, "book": book}
2022-01-11 18:33:58 +00:00
if request.POST.get("id"):
data["readthrough"] = get_object_or_404(
models.ReadThrough, id=request.POST.get("id")
)
2022-01-11 18:41:33 +00:00
return TemplateResponse(request, "readthrough/readthrough.html", data)
2022-01-11 17:50:04 +00:00
form.save()
return redirect("book", book_id)
2022-01-11 17:50:04 +00:00
2021-09-22 22:59:48 +00:00
@transaction.atomic
# pylint: disable=too-many-arguments
2021-09-22 22:59:48 +00:00
def update_readthrough_on_shelve(
user, annotated_book, status, start_date=None, finish_date=None, stopped_date=None
2021-09-22 22:59:48 +00:00
):
"""update the current readthrough for a book when it is re-shelved"""
# there *should* only be one of current active readthrough, but it's a list
active_readthrough = next(iter(annotated_book.active_readthroughs), None)
# deactivate all existing active readthroughs
for readthrough in annotated_book.active_readthroughs:
readthrough.is_active = False
readthrough.save()
# if the state is want-to-read, deactivating existing readthroughs is all we need
if status == models.Shelf.TO_READ:
return
# if we're starting a book, we need a fresh clean active readthrough
if status == models.Shelf.READING or not active_readthrough:
active_readthrough = models.ReadThrough.objects.create(
user=user, book=annotated_book
)
# santiize and set dates
active_readthrough.start_date = load_date_in_user_tz_as_utc(start_date, user)
# if the stop or finish date is set, the readthrough will be set as inactive
2021-09-22 22:59:48 +00:00
active_readthrough.finish_date = load_date_in_user_tz_as_utc(finish_date, user)
active_readthrough.stopped_date = load_date_in_user_tz_as_utc(stopped_date, user)
2021-09-22 22:59:48 +00:00
active_readthrough.save()
2021-01-13 20:33:48 +00:00
@login_required
@require_POST
def delete_readthrough(request):
2021-04-26 16:15:42 +00:00
"""remove a readthrough"""
2021-03-08 16:49:10 +00:00
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
readthrough.raise_not_deletable(request.user)
2021-01-13 20:33:48 +00:00
readthrough.delete()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 20:33:48 +00:00
2022-01-11 18:41:33 +00:00
@login_required
@require_POST
def delete_progressupdate(request):
2021-04-26 16:15:42 +00:00
"""remove a progress update"""
2021-03-08 16:49:10 +00:00
update = get_object_or_404(models.ProgressUpdate, id=request.POST.get("id"))
update.raise_not_deletable(request.user)
update.delete()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))