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/views.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'uploader/species/views.py') 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