Merge reading status views into one view

This commit is contained in:
Mouse Reeve 2021-06-09 10:36:52 -07:00
parent 8d181a9848
commit 113ced2900
7 changed files with 60 additions and 95 deletions

View file

@ -7,7 +7,7 @@
{% block modal-form-open %}
<form name="finish-reading" action="{% url 'finish-reading' book.id %}" method="post">
<form name="finish-reading" action="{% url 'reading-status' 'finish' book.id %}" method="post">
{% endblock %}
{% block modal-body %}

View file

@ -8,7 +8,7 @@ Start "<em>{{ book_title }}</em>"
{% endblock %}
{% block modal-form-open %}
<form name="start-reading" action="{% url 'start-reading' book.id %}" method="post">
<form name="start-reading" action="{% url 'reading-status' 'start' book.id %}" method="post">
{% endblock %}
{% block modal-body %}

View file

@ -6,7 +6,7 @@
{% endblock %}
{% block modal-form-open %}
<form name="shelve" action="{% url 'to-read' book.id %}" method="post">
<form name="shelve" action="{% url 'reading-status' 'want' book.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="book" value="{{ active_shelf.book.id }}">
<input type="hidden" name="shelf" value="to-read">

View file

@ -56,7 +56,7 @@ class ReadingViews(TestCase):
)
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
views.StartReading.as_view()(request, self.book.id)
views.ReadingStatus.as_view()(request, "start", self.book.id)
self.assertEqual(shelf.books.get(), self.book)
@ -112,7 +112,7 @@ class ReadingViews(TestCase):
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
views.FinishReading.as_view()(request, self.book.id)
views.ReadingStatus.as_view()(request, "finish", self.book.id)
self.assertEqual(shelf.books.get(), self.book)

View file

@ -317,17 +317,9 @@ urlpatterns = [
re_path(r"^delete-progressupdate/?$", views.delete_progressupdate),
# shelve actions
re_path(
r"^to-read/(?P<book_id>\d+)/?$", views.WantToRead.as_view(), name="to-read"
),
re_path(
r"^start-reading/(?P<book_id>\d+)/?$",
views.StartReading.as_view(),
name="start-reading",
),
re_path(
r"^finish-reading/(?P<book_id>\d+)/?$",
views.FinishReading.as_view(),
name="finish-reading",
r"^reading-status/(?P<status>want|start|finish)/(?P<book_id>\d+)/?$",
views.ReadingStatus.as_view(),
name="reading-status",
),
# following
re_path(r"^follow/?$", views.follow, name="follow"),

View file

@ -24,8 +24,9 @@ from .landing import About, Home, Discover
from .list import Lists, List, Curate, UserLists
from .notifications import Notifications
from .outbox import Outbox
from .reading import edit_readthrough, create_readthrough, delete_readthrough
from .reading import WantToRead, StartReading, FinishReading, delete_progressupdate
from .reading import edit_readthrough, create_readthrough
from .reading import delete_readthrough, delete_progressupdate
from .reading import ReadingStatus
from .reports import Report, Reports, make_report, resolve_report, suspend_user
from .rss_feed import RssFeed
from .password import PasswordResetRequest, PasswordReset, ChangePassword

View file

@ -18,96 +18,68 @@ from .helpers import get_edition, handle_reading_status
@method_decorator(login_required, name="dispatch")
# pylint: disable=no-self-use
class WantToRead(View):
class ReadingStatus(View):
"""consider reading a book"""
def get(self, request, book_id):
def get(self, request, status, book_id):
"""modal page"""
book = get_edition(book_id)
return TemplateResponse(request, "reading_progress/want.html", {"book": book})
template = {
"want": "want.html",
"finish": "start.html",
"start": "finish.html",
}.get(status)
if not template:
return HttpResponseNotFound()
return TemplateResponse(request, f"reading_progress/{template}", {"book": book})
def post(self, request, book_id):
def post(self, request, status, book_id):
"""desire a book"""
identifier = {
"want": models.Shelf.TO_READ,
"start": models.Shelf.READING,
"finish": models.Shelf.READ_FINISHED,
}.get(status)
if not identifier:
return HttpResponseBadRequest()
desired_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.TO_READ, user=request.user
identifier=identifier, user=request.user
).first()
return handle_shelve(request, book_id, desired_shelf)
@method_decorator(login_required, name="dispatch")
# pylint: disable=no-self-use
class StartReading(View):
"""begin a book"""
def get(self, request, book_id):
"""modal page"""
book = get_edition(book_id)
return TemplateResponse(request, "reading_progress/start.html", {"book": book})
def post(self, request, book_id):
"""begin reading a book"""
desired_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.READING, user=request.user
).first()
return handle_shelve(request, book_id, desired_shelf)
@method_decorator(login_required, name="dispatch")
# pylint: disable=no-self-use
class FinishReading(View):
"""finish a book"""
def get(self, request, book_id):
"""modal page"""
book = get_edition(book_id)
return TemplateResponse(request, "reading_progress/finish.html", {"book": book})
def post(self, request, book_id):
"""a user completed a book, yay"""
desired_shelf = models.Shelf.objects.filter(
identifier=models.Shelf.READ_FINISHED, user=request.user
).first()
return handle_shelve(request, book_id, desired_shelf)
def handle_shelve(request, book_id, desired_shelf):
"""these are all basically the same"""
book = get_edition(book_id)
reshelve_book(request.user, book, desired_shelf)
if desired_shelf.identifier != models.Shelf.TO_READ:
# update or create a readthrough
readthrough = update_readthrough(request, book=book)
if readthrough:
readthrough.save()
# post about it (if you want)
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
handle_reading_status(request.user, desired_shelf, book, privacy)
return redirect(request.headers.get("Referer", "/"))
def reshelve_book(user, book, desired_shelf):
"""move a book to a new shelf"""
current_status_shelfbook = (
models.ShelfBook.objects.select_related("shelf")
.filter(
shelf__identifier__in=models.Shelf.READ_STATUS_IDENTIFIERS,
user=user,
book=book,
current_status_shelfbook = (
models.ShelfBook.objects.select_related("shelf")
.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 != desired_shelf.identifier:
current_status_shelfbook.delete()
else: # It already was on the shelf
return
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=user)
models.ShelfBook.objects.create(
book=book, shelf=desired_shelf, user=request.user
)
if desired_shelf.identifier != models.Shelf.TO_READ:
# update or create a readthrough
readthrough = update_readthrough(request, book=book)
if readthrough:
readthrough.save()
# post about it (if you want)
if request.POST.get("post-status"):
privacy = request.POST.get("privacy")
handle_reading_status(request.user, desired_shelf, book, privacy)
return redirect(request.headers.get("Referer", "/"))
@login_required