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
|
|
|
|
from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
|
|
|
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-01-13 21:05:16 +00:00
|
|
|
from .helpers import get_edition, handle_reading_status
|
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()
|
|
|
|
return TemplateResponse(request, f"reading_progress/{template}", {"book": book})
|
|
|
|
|
|
|
|
def post(self, request, status, book_id):
|
2021-06-08 18:23:53 +00:00
|
|
|
"""desire a book"""
|
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-06-08 18:23:53 +00:00
|
|
|
desired_shelf = models.Shelf.objects.filter(
|
2021-06-09 17:36:52 +00:00
|
|
|
identifier=identifier, user=request.user
|
2021-06-08 18:10:39 +00:00
|
|
|
).first()
|
|
|
|
|
2021-06-08 18:55:18 +00:00
|
|
|
book = get_edition(book_id)
|
2021-06-08 18:43:30 +00:00
|
|
|
|
2021-06-09 17:36:52 +00:00
|
|
|
current_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_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
|
|
|
|
return redirect(request.headers.get("Referer", "/"))
|
|
|
|
|
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=book, shelf=desired_shelf, user=request.user
|
|
|
|
)
|
2021-01-13 20:33:48 +00:00
|
|
|
|
2021-06-09 17:36:52 +00:00
|
|
|
if desired_shelf.identifier != models.Shelf.TO_READ:
|
|
|
|
# update or create a readthrough
|
|
|
|
readthrough = update_readthrough(request, book=book)
|
|
|
|
if readthrough:
|
|
|
|
readthrough.save()
|
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-06-08 18:43:30 +00:00
|
|
|
|
2021-06-09 17:36:52 +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-04-26 16:15:42 +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()
|
|
|
|
|
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-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-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-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
|
|
|
|
|
|
|
|
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-03-28 22:51:40 +00:00
|
|
|
user_tz = dateutil.tz.gettz(user.preferred_timezone)
|
|
|
|
start_date = dateutil.parser.parse(date_str, ignoretz=True)
|
|
|
|
return start_date.replace(tzinfo=user_tz).astimezone(dateutil.tz.UTC)
|
|
|
|
|
|
|
|
|
2021-01-13 20:33:48 +00:00
|
|
|
def update_readthrough(request, book=None, create=True):
|
2021-04-26 16:15:42 +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:
|
2021-03-28 22:51:40 +00:00
|
|
|
readthrough.start_date = load_date_in_user_tz_as_utc(
|
|
|
|
start_date, request.user
|
|
|
|
)
|
2021-01-13 20:33:48 +00:00
|
|
|
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-28 22:51:40 +00:00
|
|
|
readthrough.finish_date = load_date_in_user_tz_as_utc(
|
|
|
|
finish_date, request.user
|
|
|
|
)
|
2021-01-13 20:33:48 +00:00
|
|
|
except ParserError:
|
|
|
|
pass
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
progress = request.POST.get("progress")
|
2021-01-17 20:59:53 +00:00
|
|
|
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")
|
2021-01-17 20:59:53 +00:00
|
|
|
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-01-17 20:59:53 +00:00
|
|
|
|
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-01-17 20:59:53 +00:00
|
|
|
|
|
|
|
# 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", "/"))
|