From cd5f5e2e7a4cb91f00e3227922daab40c30b35bf Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 4 Sep 2024 12:32:27 -0500 Subject: Create populations (InbredSet groups) under Species. --- uploader/population/views.py | 55 ++++++++-- .../templates/populations/create-population.html | 118 +++++++++++++++++++++ .../templates/populations/view-population.html | 2 +- 3 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 uploader/templates/populations/create-population.html (limited to 'uploader') 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("//create-population/", methods=["GET", "POST"]) +@popbp.route("//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", diff --git a/uploader/templates/populations/create-population.html b/uploader/templates/populations/create-population.html new file mode 100644 index 0000000..a27a134 --- /dev/null +++ b/uploader/templates/populations/create-population.html @@ -0,0 +1,118 @@ +{%extends "populations/base.html"%} +{%from "flash_messages.html" import flash_all_messages%} +{%from "species/macro-select-species.html" import select_species_form%} + +{%block title%}Create Population{%endblock%} + +{%block pagetitle%}Create Population{%endblock%} + +{%block lvl3_breadcrumbs%} + +{%endblock%} + + +{%block contents%} +
+

The population is the next hierarchical node under Species. Data is grouped under a specific population, under a particular species.

+

+ This page enables you to create a new population, in the case that you + cannot find the population you want in the + + list of species populations + +

+
+ +
+
+ + Create Population + +
+ + + +

+ This is a descriptive name for your population — useful for + humans. +

+
+
+ +
+ + + +

+ This is a short representative, but constrained name for your + population. +

+

+ The field will only accept letters ('A-Za-z'), numbers (0-9), hyphens + and underscores. Any other character will cause the name to be + rejected. +

+
+
+ +
+ + + +

+ This is a more detailed description for your population. This is + useful to communicate with other researchers some details regarding + your population, and what its about. +

+

+ Put, here, anything that describes your population but does not go + cleanly under metadata. +

+
+
+ +
+ +
+ +
+
+{%endblock%} + +{%block sidebarcontents%} +

… maybe provide species details here, perhaps? …

+{%endblock%} diff --git a/uploader/templates/populations/view-population.html b/uploader/templates/populations/view-population.html index 2ab7ca3..7eb1362 100644 --- a/uploader/templates/populations/view-population.html +++ b/uploader/templates/populations/view-population.html @@ -30,7 +30,7 @@
{{population.FullName}}
Description
-
{{population.Description or "-"}}
+
{{population.Description or "-"}}
-- cgit v1.2.3