From cdaf0fe8e3df19b01e9d3c41f7935c1dc1ac9fa9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 11 Jan 2022 10:22:01 -0800 Subject: [PATCH] Show error message when start and finish date is invalid --- bookwyrm/forms.py | 10 ++++ .../readthrough/add_readthrough_modal.html | 46 ++++++++++++++++++- .../templates/readthrough/readthrough.html | 15 ++++++ bookwyrm/views/reading.py | 15 +++--- 4 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 bookwyrm/templates/readthrough/readthrough.html diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index 37cb2e87..83f0d053 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -480,6 +480,16 @@ class SortListForm(forms.Form): ) class ReadThroughForm(CustomForm): + def clean(self): + """make sure the email isn't in use by a registered user""" + cleaned_data = super().clean() + start_date = cleaned_data.get("start_date") + finish_date = cleaned_data.get("finish_date") + if start_date > finish_date: + self.add_error( + "finish_date", _("Reading finish date cannot be before start date.") + ) + class Meta: model = models.ReadThrough fields = ["user", "book", "start_date", "finish_date"] diff --git a/bookwyrm/templates/readthrough/add_readthrough_modal.html b/bookwyrm/templates/readthrough/add_readthrough_modal.html index 55d2198c..e177850e 100644 --- a/bookwyrm/templates/readthrough/add_readthrough_modal.html +++ b/bookwyrm/templates/readthrough/add_readthrough_modal.html @@ -13,7 +13,51 @@ Add read dates for "{{ title }}" {% endblock %} {% block modal-body %} -{% include 'readthrough/readthrough_form.html' with readthrough=None %} +{% csrf_token %} + + + + +
+ + {% firstof form.start_date.value readthrough.start_date|date:"Y-m-d" as value %} + + {% include 'snippets/form_errors.html' with errors_list=form.start_date.errors id="desc_start_date" %} +
+ +{# Only show progress for editing existing readthroughs #} +{% if readthrough.id and not readthrough.finish_date %} +{% join "id_progress" readthrough.id as field_id %} + +{% include "snippets/progress_field.html" with id=field_id %} +{% endif %} + +
+ + {% firstof form.finish_date.value readthrough.finish_date|date:"Y-m-d" as value %} + + {% include 'snippets/form_errors.html' with errors_list=form.finish_date.errors id="desc_finish_date" %} +
{% endblock %} {% block modal-footer %} diff --git a/bookwyrm/templates/readthrough/readthrough.html b/bookwyrm/templates/readthrough/readthrough.html new file mode 100644 index 00000000..efd77683 --- /dev/null +++ b/bookwyrm/templates/readthrough/readthrough.html @@ -0,0 +1,15 @@ +{% extends 'layout.html' %} +{% load i18n %} +{% load utilities %} + +{% block title %} +{% blocktrans trimmed with title=book|book_title %} +Add read dates for "{{ title }}" +{% endblocktrans %} +{% endblock %} + +{% block content %} + +{% include "readthrough/add_readthrough_modal.html" with book=book active=True static=True %} + +{% endblock %} diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index 7c10c7fc..7c2b6d0b 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -124,9 +124,8 @@ class CreateReadThrough(View): def post(self, request): """can't use the form normally because the dates are too finnicky""" - print("hi") + book_id = request.POST.get("book") 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 @@ -134,13 +133,15 @@ class CreateReadThrough(View): normalized_post["finish_date"] = load_date_in_user_tz_as_utc( request.POST.get("finish_date"), request.user ) - form = forms.ReadThroughForm(normalized_post) + form = forms.ReadThroughForm(request.POST) if not form.is_valid(): - print(form.errors) - # TODO error handling - return "" + book = get_object_or_404(models.Edition, id=book_id) + data = {"form": form, "book": book} + return TemplateResponse( + request, "readthrough/readthrough.html", data + ) form.save() - return redirect("book", request.POST.get("book")) + return redirect("book", book_id)