moviewyrm/bookwyrm/views/reading.py

191 lines
5.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" the good stuff! the books! """
2021-01-13 20:33:48 +00:00
import dateutil.parser
from dateutil.parser import ParserError
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
from django.views.decorators.http import require_POST
from bookwyrm import models
2021-01-13 21:05:16 +00:00
from .helpers import get_edition, handle_reading_status
from .shelf import handle_unshelve
2021-01-13 20:33:48 +00:00
# pylint: disable= no-self-use
@login_required
@require_POST
def start_reading(request, book_id):
2021-03-08 16:49:10 +00:00
""" begin reading a book """
2021-01-13 20:33:48 +00:00
book = get_edition(book_id)
2021-03-08 16:49:10 +00:00
shelf = models.Shelf.objects.filter(identifier="reading", user=request.user).first()
2021-01-13 20:33:48 +00:00
# create a readthrough
readthrough = update_readthrough(request, book=book)
2021-01-14 02:02:01 +00:00
if readthrough:
2021-01-13 20:33:48 +00:00
readthrough.save()
2021-01-27 17:29:34 +00:00
# create a progress update if we have a page
readthrough.create_update()
2021-01-13 20:33:48 +00:00
# shelve the book
2021-03-08 16:49:10 +00:00
if request.POST.get("reshelve", True):
2021-01-13 20:33:48 +00:00
try:
2021-03-08 16:49:10 +00:00
current_shelf = models.Shelf.objects.get(user=request.user, edition=book)
2021-01-13 20:33:48 +00:00
handle_unshelve(request.user, book, current_shelf)
except models.Shelf.DoesNotExist:
# this just means it isn't currently on the user's shelves
pass
2021-03-08 16:49:10 +00:00
models.ShelfBook.objects.create(book=book, shelf=shelf, user=request.user)
2021-01-13 20:33:48 +00:00
# post about it (if you want)
2021-03-08 16:49:10 +00:00
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
2021-01-13 20:33:48 +00:00
handle_reading_status(request.user, shelf, book, privacy)
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 20:33:48 +00:00
@login_required
@require_POST
def finish_reading(request, book_id):
2021-03-08 16:49:10 +00:00
""" a user completed a book, yay """
2021-01-13 20:33:48 +00:00
book = get_edition(book_id)
2021-03-08 16:49:10 +00:00
shelf = models.Shelf.objects.filter(identifier="read", user=request.user).first()
2021-01-13 20:33:48 +00:00
# update or create a readthrough
readthrough = update_readthrough(request, book=book)
2021-01-14 02:02:01 +00:00
if readthrough:
2021-01-13 20:33:48 +00:00
readthrough.save()
# shelve the book
2021-03-08 16:49:10 +00:00
if request.POST.get("reshelve", True):
2021-01-13 20:33:48 +00:00
try:
2021-03-08 16:49:10 +00:00
current_shelf = models.Shelf.objects.get(user=request.user, edition=book)
2021-01-13 20:33:48 +00:00
handle_unshelve(request.user, book, current_shelf)
except models.Shelf.DoesNotExist:
# this just means it isn't currently on the user's shelves
pass
2021-03-08 16:49:10 +00:00
models.ShelfBook.objects.create(book=book, shelf=shelf, user=request.user)
2021-01-13 20:33:48 +00:00
# post about it (if you want)
2021-03-08 16:49:10 +00:00
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
2021-01-13 20:33:48 +00:00
handle_reading_status(request.user, shelf, book, privacy)
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 20:33:48 +00:00
@login_required
@require_POST
def edit_readthrough(request):
2021-03-08 16:49:10 +00:00
""" can't use the form because the dates are too finnicky """
2021-01-13 20:33:48 +00:00
readthrough = update_readthrough(request, create=False)
if not readthrough:
return HttpResponseNotFound()
# don't let people edit other people's data
if request.user != readthrough.user:
return HttpResponseBadRequest()
readthrough.save()
# record the progress update individually
# use default now for date field
readthrough.create_update()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 20:33:48 +00:00
@login_required
@require_POST
def delete_readthrough(request):
2021-03-08 16:49:10 +00:00
""" remove a readthrough """
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
2021-01-13 20:33:48 +00:00
# don't let people edit other people's data
if request.user != readthrough.user:
return HttpResponseBadRequest()
readthrough.delete()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 20:33:48 +00:00
@login_required
@require_POST
def create_readthrough(request):
2021-03-08 16:49:10 +00:00
""" can't use the form because the dates are too finnicky """
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
2021-01-13 20:33:48 +00:00
readthrough = update_readthrough(request, create=True, book=book)
if not readthrough:
return redirect(book.local_path)
readthrough.save()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))
2021-01-13 20:33:48 +00:00
def update_readthrough(request, book=None, create=True):
2021-03-08 16:49:10 +00:00
""" updates but does not save dates on a readthrough """
2021-01-13 20:33:48 +00:00
try:
2021-03-08 16:49:10 +00:00
read_id = request.POST.get("id")
2021-01-13 20:33:48 +00:00
if not read_id:
raise models.ReadThrough.DoesNotExist
readthrough = models.ReadThrough.objects.get(id=read_id)
except models.ReadThrough.DoesNotExist:
if not create or not book:
return None
readthrough = models.ReadThrough(
user=request.user,
book=book,
)
2021-03-08 16:49:10 +00:00
start_date = request.POST.get("start_date")
2021-01-13 20:33:48 +00:00
if start_date:
try:
start_date = timezone.make_aware(dateutil.parser.parse(start_date))
readthrough.start_date = start_date
except ParserError:
pass
2021-03-08 16:49:10 +00:00
finish_date = request.POST.get("finish_date")
2021-01-13 20:33:48 +00:00
if finish_date:
try:
2021-03-08 16:49:10 +00:00
finish_date = timezone.make_aware(dateutil.parser.parse(finish_date))
2021-01-13 20:33:48 +00:00
readthrough.finish_date = finish_date
except ParserError:
pass
2021-03-08 16:49:10 +00:00
progress = request.POST.get("progress")
if progress:
try:
progress = int(progress)
readthrough.progress = progress
except ValueError:
pass
2021-03-08 16:49:10 +00:00
progress_mode = request.POST.get("progress_mode")
if progress_mode:
try:
progress_mode = models.ProgressMode(progress_mode)
readthrough.progress_mode = progress_mode
except ValueError:
pass
2021-01-13 20:33:48 +00:00
if not readthrough.start_date and not readthrough.finish_date:
return None
return readthrough
2021-03-08 16:49:10 +00:00
@login_required
@require_POST
def delete_progressupdate(request):
2021-03-08 16:49:10 +00:00
""" remove a progress update """
update = get_object_or_404(models.ProgressUpdate, id=request.POST.get("id"))
# don't let people edit other people's data
if request.user != update.user:
return HttpResponseBadRequest()
update.delete()
2021-03-08 16:49:10 +00:00
return redirect(request.headers.get("Referer", "/"))