From b6a38df0eaedb9ac4bb4b602643ca1e97f889b49 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 5 Sep 2024 12:50:52 -0500 Subject: Improve error messaging for "Create Population" feature. --- uploader/population/views.py | 43 +++++++++++---------- .../templates/populations/create-population.html | 44 +++++++++++++++++----- 2 files changed, 57 insertions(+), 30 deletions(-) (limited to 'uploader') diff --git a/uploader/population/views.py b/uploader/population/views.py index fe318e1..84dffdb 100644 --- a/uploader/population/views.py +++ b/uploader/population/views.py @@ -90,46 +90,49 @@ def create_population(species_id: int): if request.method == "GET": error_values = request.args.get("error_values") - if bool(error_values): - base64.b64encode( - json.dumps(dict(request.form)).encode("utf8")) - error_values = json.loads( - base64.b64decode( - error_values.encode("utf8")).decode("utf8")) + if not bool(error_values): + error_values = base64.b64encode( + '{"errors":{}, "error_values": {}}'.encode("utf8") + ).decode("utf8") + + error_values = json.loads(base64.b64decode( + error_values.encode("utf8")).decode("utf8")) return render_template( "populations/create-population.html", species=species, activelink="create-population", **error_values) - error = False - if not bool(species): flash("You must select a species.", "alert-danger") - error = True + return redirect(url_for("species.populations.index")) + + errors = tuple() population_name = (request.form.get( "population_name") or "").strip() if not bool(population_name): - flash("You must provide a name for the population!", "alert-danger") - error = True + errors = errors + (("population_name", + "You must provide a name for the population!"),) if not valid_population_name(population_name): - flash("The population name can only contain letters, numbers, " - "hyphens and underscores.", - "alert-danger") - error = True + errors = errors + (( + "population_name", + "The population name can only contain letters, numbers, " + "hyphens and underscores."),) population_fullname = (request.form.get( "population_fullname") or "").strip() if not bool(population_fullname): - flash("You MUST provide a Full Name for the population.", - "alert-danger") - error = True + errors = errors + ( + ("population_fullname", "Full Name MUST be provided."),) - if error: + if bool(errors): values = base64.b64encode( - json.dumps(dict(request.form)).encode("utf8")) + json.dumps({ + "errors": dict(errors), + "error_values": dict(request.form) + }).encode("utf8")) return redirect(url_for("species.populations.create_population", species_id=species["SpeciesId"], error_values=values)) diff --git a/uploader/templates/populations/create-population.html b/uploader/templates/populations/create-population.html index a27a134..6a96148 100644 --- a/uploader/templates/populations/create-population.html +++ b/uploader/templates/populations/create-population.html @@ -40,15 +40,24 @@ Create Population -
+ {{flash_all_messages()}} + +
+ {%if errors.population_fullname%} + {{errors.population_fullname}} + {%endif%}

@@ -58,15 +67,22 @@

-
+
+ {%if errors.population_name%} + {{errors.population_name}} + {%endif%}

@@ -81,15 +97,23 @@

-
+
- + {%if errors.population_description%} + {{errors.population_description}} + {%endif%} +

This is a more detailed description for your population. This is -- cgit v1.2.3