Merge pull request #1168 from bookwyrm-social/reading-progress-view

Merge reading status views into one view
This commit is contained in:
Mouse Reeve 2021-06-09 11:48:06 -07:00 committed by GitHub
commit fd753d47ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 101 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

@ -9,7 +9,7 @@
{% if shelf.identifier == 'reading' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %}
{% trans "Start reading" as button_text %}
{% url 'start-reading' book.id as fallback_url %}
{% url 'reading-status' 'start' book.id as fallback_url %}
{% include 'snippets/toggle/toggle_button.html' with class=class text=button_text controls_text="start-reading" controls_uid=button_uuid focus="modal-title-start-reading" disabled=is_current fallback_url=fallback_url %}
{% endif %}{% elif shelf.identifier == 'read' and active_shelf.shelf.identifier == 'read' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %}
@ -17,13 +17,13 @@
{% endif %}{% elif shelf.identifier == 'read' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %}
{% trans "Finish reading" as button_text %}
{% url 'finish-reading' book.id as fallback_url %}
{% url 'reading-status' 'finish' book.id as fallback_url %}
{% include 'snippets/toggle/toggle_button.html' with class=class text=button_text controls_text="finish-reading" controls_uid=button_uuid focus="modal-title-finish-reading" disabled=is_current fallback_url=fallback_url %}
{% endif %}{% elif shelf.identifier == 'to-read' %}{% if not dropdown or active_shelf.shelf.identifier|next_shelf != shelf.identifier %}
{% trans "Want to read" as button_text %}
{% url 'to-read' book.id as fallback_url %}
{% url 'reading-status' 'want' book.id as fallback_url %}
{% include 'snippets/toggle/toggle_button.html' with class=class text=button_text controls_text="want-to-read" controls_uid=button_uuid focus="modal-title-want-to-read" disabled=is_current fallback_url=fallback_url %}
{% endif %}{% elif shelf.editable %}

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)
@ -86,7 +86,7 @@ class ReadingViews(TestCase):
request = self.factory.post("")
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.assertFalse(to_read_shelf.books.exists())
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

@ -33,7 +33,7 @@ class ReadThrough(TestCase):
self.assertEqual(self.edition.readthrough_set.count(), 0)
self.client.post(
"/start-reading/{}".format(self.edition.id),
"/reading-status/start/{}".format(self.edition.id),
{
"start_date": "2020-11-27",
},
@ -54,7 +54,7 @@ class ReadThrough(TestCase):
self.assertEqual(self.edition.readthrough_set.count(), 0)
self.client.post(
"/start-reading/{}".format(self.edition.id),
"/reading-status/start/{}".format(self.edition.id),
{
"start_date": "2020-11-27",
},

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",
"start": "start.html",
"finish": "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