diff options
-rw-r--r-- | uploader/species/models.py | 36 | ||||
-rw-r--r-- | uploader/species/views.py | 35 | ||||
-rw-r--r-- | uploader/templates/species/create-species.html | 16 |
3 files changed, 66 insertions, 21 deletions
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/<taxonID> 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": diff --git a/uploader/templates/species/create-species.html b/uploader/templates/species/create-species.html index 1bc3b61..0d0bedf 100644 --- a/uploader/templates/species/create-species.html +++ b/uploader/templates/species/create-species.html @@ -69,6 +69,22 @@ </div> <div class="form-group"> + <label for="select-species-family" class="form-label">Family</label> + <select id="select-species-family" + name="species_family" + required="required" + class="form-control"> + <option value="">Please select a grouping</option> + {%for family in families%} + <option value="{{family}}">{{family}}</option> + {%endfor%} + </select> + <small class="form-text text-muted"> + This is a generic grouping for the species that determines under which + grouping the species appears in the GeneNetwork menus</small> + </div> + + <div class="form-group"> <input type="submit" value="create new species" class="btn btn-primary" /> |