diff options
author | Frederick Muriuki Muriithi | 2024-09-10 16:57:41 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-09-10 16:57:41 -0500 |
commit | 83b41616825d5abed8543a744d20cf6e4c6ffa64 (patch) | |
tree | 12a6a60687974d26b60da2650d13bcf5e5faa4af /uploader/species/models.py | |
parent | 3c8ec9af68d593e7e5eef52aca3d001e22c8e0f7 (diff) | |
download | gn-uploader-83b41616825d5abed8543a744d20cf6e4c6ffa64.tar.gz |
Require that user selects the family.
Diffstat (limited to 'uploader/species/models.py')
-rw-r--r-- | uploader/species/models.py | 36 |
1 files changed, 24 insertions, 12 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() + } |