Convert start and finish reading into view classes

This commit is contained in:
Mouse Reeve 2021-06-08 11:10:39 -07:00
parent 0e84f4bbac
commit 45146b4999
3 changed files with 84 additions and 75 deletions

View file

@ -25,7 +25,7 @@ from .list import Lists, List, Curate, UserLists
from .notifications import Notifications from .notifications import Notifications
from .outbox import Outbox from .outbox import Outbox
from .reading import edit_readthrough, create_readthrough, delete_readthrough from .reading import edit_readthrough, create_readthrough, delete_readthrough
from .reading import start_reading, finish_reading, delete_progressupdate from .reading import StartReading, FinishReading, delete_progressupdate
from .reports import Report, Reports, make_report, resolve_report, suspend_user from .reports import Report, Reports, make_report, resolve_report, suspend_user
from .rss_feed import RssFeed from .rss_feed import RssFeed
from .password import PasswordResetRequest, PasswordReset, ChangePassword from .password import PasswordResetRequest, PasswordReset, ChangePassword

View file

@ -7,6 +7,8 @@ from dateutil.parser import ParserError
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from bookwyrm import models from bookwyrm import models
@ -14,88 +16,95 @@ from .helpers import get_edition, handle_reading_status
from .shelf import handle_unshelve from .shelf import handle_unshelve
# pylint: disable= no-self-use @method_decorator(login_required, name="dispatch")
@login_required # pylint: disable=no-self-use
@require_POST class StartReading(View):
def start_reading(request, book_id): """begin a book"""
"""begin reading a book"""
book = get_edition(book_id)
reading_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.READING, user=request.user
).first()
# create a readthrough def post(self, request, book_id):
readthrough = update_readthrough(request, book=book) """begin reading a book"""
if readthrough: book = get_edition(book_id)
readthrough.save() reading_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.READING, user=request.user
).first()
# create a progress update if we have a page # create a readthrough
readthrough.create_update() readthrough = update_readthrough(request, book=book)
if readthrough:
readthrough.save()
current_status_shelfbook = ( # create a progress update if we have a page
models.ShelfBook.objects.select_related("shelf") readthrough.create_update()
.filter(
shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS, current_status_shelfbook = (
user=request.user, models.ShelfBook.objects.select_related("shelf")
book=book, .filter(
shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS,
user=request.user,
book=book,
)
.first()
) )
.first() if current_status_shelfbook is not None:
) if current_status_shelfbook.shelf.identifier != models.Shelf.READING:
if current_status_shelfbook is not None: handle_unshelve(book, current_status_shelfbook.shelf)
if current_status_shelfbook.shelf.identifier != models.Shelf.READING: else: # It already was on the shelf
handle_unshelve(book, current_status_shelfbook.shelf) return redirect(request.headers.get("Referer", "/"))
else: # It already was on the shelf
return redirect(request.headers.get("Referer", "/"))
models.ShelfBook.objects.create(book=book, shelf=reading_shelf, user=request.user) models.ShelfBook.objects.create(
book=book, shelf=reading_shelf, user=request.user
# post about it (if you want)
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
handle_reading_status(request.user, reading_shelf, book, privacy)
return redirect(request.headers.get("Referer", "/"))
@login_required
@require_POST
def finish_reading(request, book_id):
"""a user completed a book, yay"""
book = get_edition(book_id)
finished_read_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.READ_FINISHED, user=request.user
).first()
# update or create a readthrough
readthrough = update_readthrough(request, book=book)
if readthrough:
readthrough.save()
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 != models.Shelf.READ_FINISHED:
handle_unshelve(book, current_status_shelfbook.shelf)
else: # It already was on the shelf
return redirect(request.headers.get("Referer", "/"))
models.ShelfBook.objects.create( # post about it (if you want)
book=book, shelf=finished_read_shelf, user=request.user if request.POST.get("post-status"):
) privacy = request.POST.get("privacy")
handle_reading_status(request.user, reading_shelf, book, privacy)
# post about it (if you want) return redirect(request.headers.get("Referer", "/"))
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
handle_reading_status(request.user, finished_read_shelf, book, privacy)
return redirect(request.headers.get("Referer", "/"))
@method_decorator(login_required, name="dispatch")
# pylint: disable=no-self-use
class FinishReading(View):
"""finish a book"""
def post(self, request, book_id):
"""a user completed a book, yay"""
book = get_edition(book_id)
finished_read_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.READ_FINISHED, user=request.user
).first()
# update or create a readthrough
readthrough = update_readthrough(request, book=book)
if readthrough:
readthrough.save()
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 != models.Shelf.READ_FINISHED:
handle_unshelve(book, current_status_shelfbook.shelf)
else: # It already was on the shelf
return redirect(request.headers.get("Referer", "/"))
models.ShelfBook.objects.create(
book=book, shelf=finished_read_shelf, user=request.user
)
# post about it (if you want)
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
handle_reading_status(request.user, finished_read_shelf, book, privacy)
return redirect(request.headers.get("Referer", "/"))
@login_required @login_required

View file

@ -20,7 +20,7 @@ from .helpers import is_api_request, get_edition, get_user_from_username
from .helpers import handle_reading_status, privacy_filter from .helpers import handle_reading_status, privacy_filter
# pylint: disable= no-self-use # pylint: disable=no-self-use
class Shelf(View): class Shelf(View):
"""shelf page""" """shelf page"""