aboutsummaryrefslogtreecommitdiff
path: root/uploader/population/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/population/views.py')
-rw-r--r--uploader/population/views.py55
1 files changed, 45 insertions, 10 deletions
diff --git a/uploader/population/views.py b/uploader/population/views.py
index e21465c..fe318e1 100644
--- a/uploader/population/views.py
+++ b/uploader/population/views.py
@@ -1,4 +1,8 @@
"""Views dealing with populations/inbredsets"""
+import re
+import json
+import base64
+
from flask import (flash,
request,
url_for,
@@ -54,12 +58,30 @@ def list_species_populations(species_id: int):
activelink="list-populations")
-def valid_population_name(population_name) -> bool:
- """Check whether the given name is a valid population name."""
- raise NotImplementedError("Please implement this…")
+def valid_population_name(population_name: str) -> bool:
+ """
+ Check whether the given name is a valid population name.
+
+ Parameters
+ ----------
+ population_name: a string of characters.
+
+ Checks For
+ ----------
+ * The name MUST start with an alphabet [a-zA-Z]
+ * The name MUST end with an alphabet [a-zA-Z] or number [0-9]
+ * The name MUST be composed of alphabets [a-zA-Z], numbers [0-9],
+ underscores (_) and/or hyphens (-).
+
+ Returns
+ -------
+ Boolean indicating whether or not the name is valid.
+ """
+ pattern = re.compile(r"^[a-zA-Z]+[a-zA-Z0-9_-]*[a-zA-Z0-9]$")
+ return bool(pattern.match(population_name))
-@popbp.route("/<int:species_id>/create-population/", methods=["GET", "POST"])
+@popbp.route("/<int:species_id>/populations/create", methods=["GET", "POST"])
@require_login
def create_population(species_id: int):
"""Create a new population."""
@@ -67,10 +89,18 @@ def create_population(species_id: int):
species = species_by_id(conn, species_id)
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"))
return render_template(
"populations/create-population.html",
species=species,
- continue_to=request.args.get("continue_uri"))
+ activelink="create-population",
+ **error_values)
error = False
@@ -78,7 +108,8 @@ def create_population(species_id: int):
flash("You must select a species.", "alert-danger")
error = True
- population_name = request.form.get("population_name", "").strip()
+ 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
@@ -89,15 +120,19 @@ def create_population(species_id: int):
"alert-danger")
error = True
- population_fullname = request.form.get("inbredset_fullname", "").strip()
+ population_fullname = (request.form.get(
+ "population_fullname") or "").strip()
if not bool(population_fullname):
- flash("You MUST provide a Full Name for the population."
+ flash("You MUST provide a Full Name for the population.",
"alert-danger")
error = True
if error:
+ values = base64.b64encode(
+ json.dumps(dict(request.form)).encode("utf8"))
return redirect(url_for("species.populations.create_population",
- **dict(request.args)))
+ species_id=species["SpeciesId"],
+ error_values=values))
new_population = save_population(conn, {
"SpeciesId": species["SpeciesId"],
@@ -105,7 +140,7 @@ def create_population(species_id: int):
"InbredSetName": population_fullname,
"FullName": population_fullname,
"Family": request.form.get("inbredset_family") or None,
- "Description": request.form.get("description") or None
+ "Description": request.form.get("population_description") or None
})
return redirect(url_for("species.populations.view_population",