Persist book.subjects and add_author when form validation fails.

This does not resolve the problem of cover image uploads being dropped because this is a broader problem and is covered separately in #2760
(we should investigate the plugin ` django-file-resubmit`)
This commit is contained in:
Hugh Rundle 2023-04-16 17:58:03 +10:00
parent 7272ca2564
commit fb3cb229e9

View file

@ -102,11 +102,13 @@ class CreateBook(View):
"authors": authors,
}
ensure_transient_values_persist(request, data)
if not form.is_valid():
ensure_transient_values_persist(request, data, form)
return TemplateResponse(request, "book/edit/edit_book.html", data)
# we have to call this twice because it requires form.cleaned_data
# which only exists after we validate the form
ensure_transient_values_persist(request, data, form)
data = add_authors(request, data)
# check if this is an edition of an existing work
@ -139,8 +141,11 @@ class CreateBook(View):
return redirect(f"/book/{book.id}")
def ensure_transient_values_persist(request, data):
def ensure_transient_values_persist(request, data, form):
"""ensure that values of transient form fields persist when re-rendering the form"""
data["book"] = data.get("book") or {}
data["book"]["subjects"] = form.cleaned_data["subjects"]
data["add_author"] = request.POST.getlist("add_author")
data["cover_url"] = request.POST.get("cover-url")