diff options
author | Frederick Muriuki Muriithi | 2024-09-03 13:50:30 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-09-03 13:51:31 -0500 |
commit | cc39af629928d7f707bb36befb28f5f3386ddf3a (patch) | |
tree | 19fae39acd735c25e86b18f1fbf7b8b92353f2d6 | |
parent | 7d26a65c825acbe9922b332ef5543e92222e7076 (diff) | |
download | gn-uploader-cc39af629928d7f707bb36befb28f5f3386ddf3a.tar.gz |
Provide generic way to select species.
-rw-r--r-- | uploader/species/models.py | 20 | ||||
-rw-r--r-- | uploader/templates/species/macro-select-species.html | 38 |
2 files changed, 57 insertions, 1 deletions
diff --git a/uploader/species/models.py b/uploader/species/models.py index 481f8be..53e7de0 100644 --- a/uploader/species/models.py +++ b/uploader/species/models.py @@ -1,5 +1,7 @@ """Database functions for species.""" +import math from typing import Optional +from functools import reduce import MySQLdb as mdb from MySQLdb.cursors import DictCursor @@ -9,11 +11,27 @@ def all_species(conn: mdb.Connection) -> tuple: with conn.cursor(cursorclass=DictCursor) as cursor: cursor.execute( "SELECT Id AS SpeciesId, SpeciesName, LOWER(Name) AS Name, " - "MenuName, FullName, TaxonomyId FROM Species") + "MenuName, FullName, TaxonomyId, Family, FamilyOrderId, OrderId " + "FROM Species ORDER BY FamilyOrderId ASC, OrderID ASC") return tuple(cursor.fetchall()) return tuple() +def order_species_by_family(species: tuple[dict, ...]) -> list: + """Order the species by their family""" + def __family_order_id__(item): + orderid = item["FamilyOrderId"] + return math.inf if orderid is None else orderid + def __order__(ordered, current): + _key = (__family_order_id__(current), current["Family"]) + return { + **ordered, + _key: ordered.get(_key, tuple()) + (current,) + } + ordered = reduce(__order__, species, {}) + return sorted(tuple(ordered.items()), key=lambda item: item[0][0]) + + def species_by_id(conn: mdb.Connection, speciesid) -> dict: "Retrieve the species from the database by id." with conn.cursor(cursorclass=DictCursor) as cursor: diff --git a/uploader/templates/species/macro-select-species.html b/uploader/templates/species/macro-select-species.html new file mode 100644 index 0000000..3dbfc95 --- /dev/null +++ b/uploader/templates/species/macro-select-species.html @@ -0,0 +1,38 @@ +{%macro select_species_form(form_action, species)%} +{%if species | length > 0%} +<form method="GET" action="{{form_action}}"> + <legend>Select Species</legend> + + <div class="form-group"> + <label for="select-species" class="form-label">Select Species</label> + <select id="select-species" + name="species_id" + class="form-control" + required="required"> + <option value="">Select Species</option> + {%for group in species%} + {{group}} + <optgroup {%if group[0][1] is not none%} + label="{{group[0][1].capitalize()}}" + {%else%} + label="Undefined" + {%endif%}> + {%for aspecies in group[1]%} + <option value="{{aspecies.SpeciesId}}">{{aspecies.MenuName}}</option> + {%endfor%} + </optgroup> + {%endfor%} + </select> + </div> + + <div class="form-group"> + <input type="submit" value="Select" class="btn btn-primary" /> + </div> +</form> +{%else%} +<p class="text-danger"> + <span class="glyphicon glyphicon-exclamation-mark"></span> + We could not find species to select from! +</p> +{%endif%} +{%endmacro%} |