From 83b41616825d5abed8543a744d20cf6e4c6ffa64 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 10 Sep 2024 16:57:41 -0500 Subject: Require that user selects the family. --- uploader/species/models.py | 36 ++++++++++++++++++++++++------------ uploader/species/views.py | 35 ++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 21 deletions(-) (limited to 'uploader/species') diff --git a/uploader/species/models.py b/uploader/species/models.py index cea3549..4426b64 100644 --- a/uploader/species/models.py +++ b/uploader/species/models.py @@ -46,6 +46,7 @@ def species_by_id(conn: mdb.Connection, speciesid) -> dict: def save_species(conn: mdb.Connection, common_name: str, scientific_name: str, + family: str, taxon_id: Optional[str] = None) -> int: """ Save a new species to the database. @@ -58,20 +59,27 @@ def save_species(conn: mdb.Connection, scientific_name; The species' scientific name. """ genus, species = scientific_name.split(" ") - species = { - "common_name": common_name, - "common_name_lower": common_name.lower(), - "menu_name": f"{common_name} ({genus[0]}. {species.lower()})", - "scientific_name": scientific_name, - "taxon_id": taxon_id - } + families = species_families(conn) with conn.cursor() as cursor: + cursor.execute("SELECT MAX(OrderId) FROM Species") + species = { + "common_name": common_name, + "common_name_lower": common_name.lower(), + "menu_name": f"{common_name} ({genus[0]}. {species.lower()})", + "scientific_name": scientific_name, + "family": family, + "family_order": families[family], + "taxon_id": taxon_id, + "species_order": cursor.fetchone()[0] + 5 + } cursor.execute( "INSERT INTO Species(" - "SpeciesName, Name, MenuName, FullName, TaxonomyId" + "SpeciesName, Name, MenuName, FullName, Family, FamilyOrderId, " + "TaxonomyId, OrderId" ") VALUES (" "%(common_name)s, %(common_name_lower)s, %(menu_name)s, " - "%(scientific_name)s, %(taxon_id)s" + "%(scientific_name)s, %(family)s, %(family_order)s, %(taxon_id)s, " + "%(species_order)s" ")", species) species_id = cursor.lastrowid @@ -83,9 +91,13 @@ def save_species(conn: mdb.Connection, } -def species_families(conn: mdb.Connection) -> tuple: +def species_families(conn: mdb.Connection) -> dict: """Retrieve the families under which species are grouped.""" with conn.cursor(cursorclass=DictCursor) as cursor: cursor.execute( - "SELECT DISTINCT(Family) FROM Species WHERE Family IS NOT NULL") - return tuple(fam["Family"] for fam in cursor.fetchall()) + "SELECT DISTINCT(Family), FamilyOrderId FROM Species " + "WHERE Family IS NOT NULL") + return { + fam["Family"]: fam["FamilyOrderId"] + for fam in cursor.fetchall() + } diff --git a/uploader/species/views.py b/uploader/species/views.py index d994e41..f39ca98 100644 --- a/uploader/species/views.py +++ b/uploader/species/views.py @@ -8,6 +8,7 @@ from flask import (flash, current_app as app) from uploader.population import popbp +from uploader.datautils import order_by_family from uploader.ui import make_template_renderer from uploader.db_utils import database_connection from uploader.oauth2.client import oauth2_get, oauth2_post @@ -49,12 +50,13 @@ def create_species(): # We can use uniprot's API to fetch the details with something like # https://rest.uniprot.org/taxonomy/ e.g. # https://rest.uniprot.org/taxonomy/6239 - if request.method == "GET": - return render_template("species/create-species.html", - activelink="create-species") - with (database_connection(app.config["SQL_URI"]) as conn, conn.cursor() as cursor): + if request.method == "GET": + return render_template("species/create-species.html", + families=species_families(conn), + activelink="create-species") + error = False taxon_id = request.form.get("species_taxonomy_id", "").strip() or None @@ -82,6 +84,11 @@ def create_species(): "alert-danger") error = True + family = request.form.get("species_family", "").strip() + if not bool(family): + flash("The species' family MUST be selected.", "alert-danger") + error = True + if bool(taxon_id): cursor.execute( "SELECT * FROM Species WHERE TaxonomyId=%s", (taxon_id,)) @@ -97,7 +104,8 @@ def create_species(): scientific_name=scientific_name, taxon_id=taxon_id)) - species = save_species(conn, common_name, scientific_name, taxon_id) + species = save_species( + conn, common_name, scientific_name, family, taxon_id) flash("Species saved successfully!", "alert-success") return redirect(url_for("species.view_species", species_id=species["species_id"])) @@ -144,7 +152,11 @@ def edit_species_extra(token: dict, species_id: int):# pylint: disable=[unused-a with database_connection(app.config["SQL_URI"]) as conn: species = species_by_id(conn, species_id) + all_the_species = all_species(conn) families = species_families(conn) + family_order = tuple( + item[0] for item in order_by_family(all_the_species) + if item[0][1] is not None) if bool(species) and request.method == "GET": return oauth2_get("auth/user/resources").then( __system_resource_uuid__ @@ -153,10 +165,15 @@ def edit_species_extra(token: dict, species_id: int):# pylint: disable=[unused-a "auth/resource/authorisation", json={"resource-ids": [resource_id]}) ).then(__check_privileges__).then( - lambda authorisations: render_template("species/edit-species.html", - species=species, - families=families, - activelink="edit-species") + lambda authorisations: render_template( + "species/edit-species.html", + species=species, + families=families, + family_order=family_order, + max_order_id = max( + row["OrderId"] for row in all_the_species + if row["OrderId"] is not None), + activelink="edit-species") ).either(__failure__, lambda res: res) if bool(species) and request.method == "POST": -- cgit v1.2.3