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,10 +16,12 @@ 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"""
def post(self, request, book_id):
"""begin reading a book""" """begin reading a book"""
book = get_edition(book_id) book = get_edition(book_id)
reading_shelf = models.Shelf.objects.filter( reading_shelf = models.Shelf.objects.filter(
@ -47,7 +51,9 @@ def start_reading(request, book_id):
else: # It already was on the shelf else: # It already was on the shelf
return redirect(request.headers.get("Referer", "/")) 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) # post about it (if you want)
if request.POST.get("post-status"): if request.POST.get("post-status"):
@ -57,9 +63,12 @@ def start_reading(request, book_id):
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
@login_required @method_decorator(login_required, name="dispatch")
@require_POST # pylint: disable=no-self-use
def finish_reading(request, book_id): class FinishReading(View):
"""finish a book"""
def post(self, request, book_id):
"""a user completed a book, yay""" """a user completed a book, yay"""
book = get_edition(book_id) book = get_edition(book_id)
finished_read_shelf = models.Shelf.objects.filter( finished_read_shelf = models.Shelf.objects.filter(

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"""