2021-03-08 16:49:10 +00:00
|
|
|
""" the good stuff! the books! """
|
2021-03-28 22:51:40 +00:00
|
|
|
from datetime import datetime
|
2021-01-13 20:33:48 +00:00
|
|
|
import dateutil.parser
|
2021-03-28 22:51:40 +00:00
|
|
|
import dateutil.tz
|
2021-01-13 20:33:48 +00:00
|
|
|
from dateutil.parser import ParserError
|
|
|
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-09-22 22:59:48 +00:00
|
|
|
from django.db import transaction
|
2021-09-10 15:00:31 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
|
2021-01-13 20:33:48 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-06-08 18:55:18 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2021-06-08 18:10:39 +00:00
|
|
|
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
|
|
|
|
|
2021-08-16 20:04:34 +00:00
|
|
|
from bookwyrm import forms, models
|
2021-09-10 15:00:31 +00:00
|
|
|
from .helpers import get_edition, handle_reading_status, is_api_request
|
2021-01-13 20:33:48 +00:00
|
|
|
|
|
|
|
|
2021-06-08 18:23:53 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
# pylint: disable=no-self-use
|
2021-06-09 17:36:52 +00:00
|
|
|
class ReadingStatus(View):
|
2021-06-08 18:23:53 +00:00
|
|
|
"""consider reading a book"""
|
|
|
|
|
2021-06-09 17:36:52 +00:00
|
|
|
def get(self, request, status, book_id):
|
2021-06-08 18:55:18 +00:00
|
|
|
"""modal page"""
|
|
|
|
book = get_edition(book_id)
|
2021-06-09 17:36:52 +00:00
|
|
|
template = {
|
|
|
|
"want": "want.html",
|
2021-06-09 18:05:15 +00:00
|
|
|
"start": "start.html",
|
|
|
|
"finish": "finish.html",
|
2021-06-09 17:36:52 +00:00
|
|
|
}.get(status)
|
|
|
|
if not template:
|
|
|
|
return HttpResponseNotFound()
|
2021-09-05 22:14:57 +00:00
|
|
|
# redirect if we're already on this shelf
|
2021-06-09 17:36:52 +00:00
|
|
|
return TemplateResponse(request, f"reading_progress/{template}", {"book": book})
|
|
|
|
|
|
|
|
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"""
|
2021-06-09 17:36:52 +00:00
|
|
|
identifier = {
|
|
|
|
"want": models.Shelf.TO_READ,
|
|
|
|
"start": models.Shelf.READING,
|
|
|
|
"finish": models.Shelf.READ_FINISHED,
|
|
|
|
}.get(status)
|
|
|
|
if not identifier:
|
|
|
|
return HttpResponseBadRequest()
|
2021-06-08 18:10:39 +00:00
|
|
|
|
2021-09-27 23:08:52 +00:00
|
|
|
desired_shelf = get_object_or_404(
|
|
|
|
models.Shelf, identifier=identifier, user=request.user
|
2021-09-27 23:04:40 +00:00
|
|
|
)
|
2021-06-08 18:10:39 +00:00
|
|
|
|
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-06-08 18:43:30 +00:00
|
|
|
|
2021-09-22 22:59:48 +00:00
|
|
|
# gets the first shelf that indicates a reading status, or None
|
2021-09-22 23:16:53 +00:00
|
|
|
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", "/")
|
2021-09-10 15:00:31 +00:00
|
|
|
referer = "/" if "reading-status" in referer else referer
|
2021-06-09 17:36:52 +00:00
|
|
|
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)
|
2021-06-09 17:36:52 +00:00
|
|
|
|
|
|
|
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"),
|
|
|
|
)
|
2021-06-08 18:10:39 +00:00
|
|
|
|
2021-06-09 17:36:52 +00:00
|
|
|
# post about it (if you want)
|
|
|
|
if request.POST.get("post-status"):
|
2021-08-16 20:04:34 +00:00
|
|
|
# is it a comment?
|
|
|
|
if request.POST.get("content"):
|
|
|
|
form = forms.CommentForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
else:
|
|
|
|
# uh oh
|
2021-08-16 22:00:35 +00:00
|
|
|
raise Exception(form.errors)
|
2021-08-16 20:04:34 +00:00
|
|
|
else:
|
|
|
|
privacy = request.POST.get("privacy")
|
|
|
|
handle_reading_status(request.user, desired_shelf, book, privacy)
|
2021-09-10 15:00:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-09-22 22:59:48 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def update_readthrough_on_shelve(
|
|
|
|
user, annotated_book, status, start_date=None, finish_date=None
|
|
|
|
):
|
|
|
|
"""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 finish date is set, the readthrough will be automatically set as inactive
|
|
|
|
active_readthrough.finish_date = load_date_in_user_tz_as_utc(finish_date, user)
|
|
|
|
|
|
|
|
active_readthrough.save()
|
|
|
|
|
|
|
|
|
2021-01-13 20:33:48 +00:00
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def edit_readthrough(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""can't use the form because the dates are too finnicky"""
|
2021-09-22 22:59:48 +00:00
|
|
|
readthrough = get_object_or_404(models.ReadThrough, id=request.POST.get("id"))
|
2021-09-27 23:04:40 +00:00
|
|
|
readthrough.raise_not_editable(request.user)
|
2021-09-22 22:59:48 +00:00
|
|
|
|
|
|
|
readthrough.start_date = load_date_in_user_tz_as_utc(
|
|
|
|
request.POST.get("start_date"), request.user
|
|
|
|
)
|
|
|
|
readthrough.finish_date = load_date_in_user_tz_as_utc(
|
|
|
|
request.POST.get("finish_date"), request.user
|
|
|
|
)
|
|
|
|
|
|
|
|
progress = request.POST.get("progress")
|
|
|
|
try:
|
|
|
|
progress = int(progress)
|
|
|
|
readthrough.progress = progress
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
progress_mode = request.POST.get("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
|
|
|
readthrough.save()
|
|
|
|
|
2021-01-17 20:59:53 +00:00
|
|
|
# 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-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"))
|
2021-09-27 23:04:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def create_readthrough(request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""can't use the form because the dates are too finnicky"""
|
2021-03-08 16:49:10 +00:00
|
|
|
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
|
2021-09-22 22:59:48 +00:00
|
|
|
|
2021-09-22 23:41:43 +00:00
|
|
|
start_date = load_date_in_user_tz_as_utc(
|
|
|
|
request.POST.get("start_date"), request.user
|
2021-09-22 22:59:48 +00:00
|
|
|
)
|
2021-09-22 23:41:43 +00:00
|
|
|
finish_date = load_date_in_user_tz_as_utc(
|
|
|
|
request.POST.get("finish_date"), request.user
|
2021-09-22 22:59:48 +00:00
|
|
|
)
|
|
|
|
models.ReadThrough.objects.create(
|
|
|
|
user=request.user,
|
|
|
|
book=book,
|
|
|
|
start_date=start_date,
|
|
|
|
finish_date=finish_date,
|
|
|
|
)
|
|
|
|
return redirect("book", book.id)
|
2021-01-13 20:33:48 +00:00
|
|
|
|
|
|
|
|
2021-03-28 22:51:40 +00:00
|
|
|
def load_date_in_user_tz_as_utc(date_str: str, user: models.User) -> datetime:
|
2021-04-26 16:15:42 +00:00
|
|
|
"""ensures that data is stored consistently in the UTC timezone"""
|
2021-09-22 22:59:48 +00:00
|
|
|
if not date_str:
|
|
|
|
return None
|
2021-03-28 22:51:40 +00:00
|
|
|
user_tz = dateutil.tz.gettz(user.preferred_timezone)
|
2021-09-22 22:59:48 +00:00
|
|
|
date = dateutil.parser.parse(date_str, ignoretz=True)
|
2021-01-13 20:33:48 +00:00
|
|
|
try:
|
2021-09-22 22:59:48 +00:00
|
|
|
return date.replace(tzinfo=user_tz).astimezone(dateutil.tz.UTC)
|
|
|
|
except ParserError:
|
2021-01-13 20:33:48 +00:00
|
|
|
return None
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-17 20:59:53 +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"))
|
2021-09-27 23:04:40 +00:00
|
|
|
update.raise_not_deletable(request.user)
|
2021-01-17 20:59:53 +00:00
|
|
|
|
|
|
|
update.delete()
|
2021-03-08 16:49:10 +00:00
|
|
|
return redirect(request.headers.get("Referer", "/"))
|