Show error message when start and finish date is invalid

This commit is contained in:
Mouse Reeve 2022-01-11 10:22:01 -08:00
parent 9521c477b2
commit cdaf0fe8e3
4 changed files with 78 additions and 8 deletions

View file

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

View file

@ -13,7 +13,51 @@ Add read dates for "<em>{{ title }}</em>"
{% endblock %}
{% block modal-body %}
{% include 'readthrough/readthrough_form.html' with readthrough=None %}
{% csrf_token %}
<input type="hidden" name="id" value="{{ readthrough.id }}">
<input type="hidden" name="book" value="{{ book.id }}">
<input type="hidden" name="user" value="{{ request.user.id }}">
<div class="field">
<label class="label" tabindex="0" id="add_readthrough_focus_{{ readthrough.id }}" for="id_start_date_{{ readthrough.id }}">
{% trans "Started reading" %}
</label>
{% firstof form.start_date.value readthrough.start_date|date:"Y-m-d" as value %}
<input
type="date"
name="start_date"
class="input"
id="id_start_date_{{ readthrough.id }}"
value="{{ value }}"
aria-describedby="desc_start_date"
>
{% include 'snippets/form_errors.html' with errors_list=form.start_date.errors id="desc_start_date" %}
</div>
{# Only show progress for editing existing readthroughs #}
{% if readthrough.id and not readthrough.finish_date %}
{% join "id_progress" readthrough.id as field_id %}
<label class="label" for="{{ field_id }}">
{% trans "Progress" %}
</label>
{% include "snippets/progress_field.html" with id=field_id %}
{% endif %}
<div class="field">
<label class="label" for="id_finish_date_{{ readthrough.id }}">
{% trans "Finished reading" %}
</label>
{% firstof form.finish_date.value readthrough.finish_date|date:"Y-m-d" as value %}
<input
type="date"
name="finish_date"
class="input"
id="id_finish_date_{{ readthrough.id }}"
value="{{ value }}"
aria-describedby="desc_finish_date"
>
{% include 'snippets/form_errors.html' with errors_list=form.finish_date.errors id="desc_finish_date" %}
</div>
{% endblock %}
{% block modal-footer %}

View file

@ -0,0 +1,15 @@
{% extends 'layout.html' %}
{% load i18n %}
{% load utilities %}
{% block title %}
{% blocktrans trimmed with title=book|book_title %}
Add read dates for "<em>{{ title }}</em>"
{% endblocktrans %}
{% endblock %}
{% block content %}
{% include "readthrough/add_readthrough_modal.html" with book=book active=True static=True %}
{% endblock %}

View file

@ -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)