Converts create readthrough to modal

This commit is contained in:
Mouse Reeve 2022-01-11 09:50:04 -08:00
parent 4ba375892a
commit 0d2c6e63d1
7 changed files with 75 additions and 42 deletions

View file

@ -478,3 +478,8 @@ class SortListForm(forms.Form):
("descending", _("Descending")), ("descending", _("Descending")),
), ),
) )
class ReadThroughForm(CustomForm):
class Meta:
model = models.ReadThrough
fields = ["user", "book", "start_date", "finish_date"]

View file

@ -0,0 +1,28 @@
{% extends "components/modal.html" %}
{% load i18n %}
{% load utilities %}
{% block modal-title %}
{% blocktrans trimmed with title=book|book_title %}
Add read dates for "<em>{{ title }}</em>"
{% endblocktrans %}
{% endblock %}
{% block modal-form-open %}
<form name="add-readthrough" action="/create-readthrough" method="post">
{% endblock %}
{% block modal-body %}
{% include 'snippets/readthrough_form.html' with readthrough=None %}
{% endblock %}
{% block modal-footer %}
<button class="button is-primary" type="submit">{% trans "Add" %}</button>
{% if not static %}
<button type="button" class="button" data-modal-close>{% trans "Cancel" %}</button>
{% endif %}
{% endblock %}
{% block modal-form-close %}
</form>
{% endblock %}

View file

@ -237,24 +237,16 @@
<h2 class="title is-5">{% trans "Your reading activity" %}</h2> <h2 class="title is-5">{% trans "Your reading activity" %}</h2>
</div> </div>
<div class="column is-narrow"> <div class="column is-narrow">
{% trans "Add read dates" as button_text %} <button class="button is-small" data-modal-open="add-readthrough">
{% include 'snippets/toggle/open_button.html' with text=button_text icon_with_text="plus" class="is-small" controls_text="add_readthrough" focus="add_readthrough_focus_" %} <span class="icon icon-plus m-mobile-0" aria-hidden="true"></span>
<span class="is-sr-only-mobile">
{% trans "Add read dates" %}
</span>
</button>
</div> </div>
</header> </header>
<section class="is-hidden box" id="add_readthrough"> {% include "book/add_readthrough_modal.html" with id="add-readthrough" %}
<form name="add-readthrough" action="/create-readthrough" method="post">
{% include 'snippets/readthrough_form.html' with readthrough=None %}
<div class="field is-grouped">
<div class="control">
<button class="button is-primary" type="submit">{% trans "Create" %}</button>
</div>
<div class="control">
{% trans "Cancel" as button_text %}
{% include 'snippets/toggle/close_button.html' with text=button_text controls_text="add_readthrough" %}
</div>
</div>
</form>
</section>
{% if not readthroughs.exists %} {% if not readthroughs.exists %}
<p>{% trans "You don't have any reading activity for this book." %}</p> <p>{% trans "You don't have any reading activity for this book." %}</p>
{% endif %} {% endif %}

View file

@ -4,6 +4,7 @@
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}"> <input type="hidden" name="id" value="{{ readthrough.id }}">
<input type="hidden" name="book" value="{{ book.id }}"> <input type="hidden" name="book" value="{{ book.id }}">
<input type="hidden" name="user" value="{{ request.user.id }}">
<div class="field"> <div class="field">
<label class="label" tabindex="0" id="add_readthrough_focus_{{ readthrough.id }}" for="id_start_date_{{ readthrough.id }}"> <label class="label" tabindex="0" id="add_readthrough_focus_{{ readthrough.id }}" for="id_start_date_{{ readthrough.id }}">
{% trans "Started reading" %} {% trans "Started reading" %}

View file

@ -457,7 +457,7 @@ urlpatterns = [
# reading progress # reading progress
re_path(r"^edit-readthrough/?$", views.edit_readthrough, name="edit-readthrough"), re_path(r"^edit-readthrough/?$", views.edit_readthrough, name="edit-readthrough"),
re_path(r"^delete-readthrough/?$", views.delete_readthrough), re_path(r"^delete-readthrough/?$", views.delete_readthrough),
re_path(r"^create-readthrough/?$", views.create_readthrough), re_path(r"^create-readthrough/?$", views.CreateReadThrough.as_view(), name="create-readthrough"),
re_path(r"^delete-progressupdate/?$", views.delete_progressupdate), re_path(r"^delete-progressupdate/?$", views.delete_progressupdate),
# shelve actions # shelve actions
re_path( re_path(

View file

@ -88,7 +88,7 @@ from .list import Lists, SavedLists, List, Curate, UserLists
from .list import save_list, unsave_list, delete_list, unsafe_embed_list from .list import save_list, unsave_list, delete_list, unsafe_embed_list
from .notifications import Notifications from .notifications import Notifications
from .outbox import Outbox from .outbox import Outbox
from .reading import create_readthrough, delete_readthrough, delete_progressupdate from .reading import CreateReadThrough, delete_readthrough, delete_progressupdate
from .reading import ReadingStatus from .reading import ReadingStatus
from .rss_feed import RssFeed from .rss_feed import RssFeed
from .search import Search from .search import Search

View file

@ -9,16 +9,16 @@ from django.utils.decorators import method_decorator
from django.views import View 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 forms, models
from bookwyrm.views.shelf.shelf_actions import unshelve from bookwyrm.views.shelf.shelf_actions import unshelve
from .status import CreateStatus from .status import CreateStatus
from .helpers import get_edition, handle_reading_status, is_api_request from .helpers import get_edition, handle_reading_status, is_api_request
from .helpers import load_date_in_user_tz_as_utc from .helpers import load_date_in_user_tz_as_utc
@method_decorator(login_required, name="dispatch")
# pylint: disable=no-self-use # pylint: disable=no-self-use
# pylint: disable=too-many-return-statements # pylint: disable=too-many-return-statements
@method_decorator(login_required, name="dispatch")
class ReadingStatus(View): class ReadingStatus(View):
"""consider reading a book""" """consider reading a book"""
@ -115,6 +115,35 @@ class ReadingStatus(View):
return redirect(referer) return redirect(referer)
@method_decorator(login_required, name="dispatch")
class CreateReadThrough(View):
"""Add new read dates"""
def get(self, request):
"""standalone form in case of errors"""
def post(self, request):
"""can't use the form normally because the dates are too finnicky"""
print("hi")
normalized_post = request.POST.copy()
print(normalized_post)
normalized_post["start_date"] = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
normalized_post["finish_date"] = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
form = forms.ReadThroughForm(normalized_post)
if not form.is_valid():
print(form.errors)
# TODO error handling
return ""
form.save()
return redirect("book", request.POST.get("book"))
@transaction.atomic @transaction.atomic
def update_readthrough_on_shelve( def update_readthrough_on_shelve(
user, annotated_book, status, start_date=None, finish_date=None user, annotated_book, status, start_date=None, finish_date=None
@ -155,28 +184,6 @@ def delete_readthrough(request):
readthrough.delete() readthrough.delete()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
@login_required
@require_POST
def create_readthrough(request):
"""can't use the form because the dates are too finnicky"""
book = get_object_or_404(models.Edition, id=request.POST.get("book"))
start_date = load_date_in_user_tz_as_utc(
request.POST.get("start_date"), request.user
)
finish_date = load_date_in_user_tz_as_utc(
request.POST.get("finish_date"), request.user
)
models.ReadThrough.objects.create(
user=request.user,
book=book,
start_date=start_date,
finish_date=finish_date,
)
return redirect("book", book.id)
@login_required @login_required
@require_POST @require_POST
def delete_progressupdate(request): def delete_progressupdate(request):