aboutsummaryrefslogtreecommitdiff
path: root/uploader/species/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/species/models.py')
-rw-r--r--uploader/species/models.py57
1 files changed, 53 insertions, 4 deletions
diff --git a/uploader/species/models.py b/uploader/species/models.py
index 4426b64..51f941c 100644
--- a/uploader/species/models.py
+++ b/uploader/species/models.py
@@ -28,7 +28,7 @@ def order_species_by_family(species: tuple[dict, ...]) -> list:
**ordered,
_key: ordered.get(_key, tuple()) + (current,)
}
- ordered = reduce(__order__, species, {})
+ ordered = reduce(__order__, species, {})# type: ignore[var-annotated]
return sorted(tuple(ordered.items()), key=lambda item: item[0][0])
@@ -47,7 +47,7 @@ def save_species(conn: mdb.Connection,
common_name: str,
scientific_name: str,
family: str,
- taxon_id: Optional[str] = None) -> int:
+ taxon_id: Optional[str] = None) -> dict:
"""
Save a new species to the database.
@@ -58,14 +58,14 @@ def save_species(conn: mdb.Connection,
common_name: The species' common name.
scientific_name; The species' scientific name.
"""
- genus, species = scientific_name.split(" ")
+ genus, species_name = scientific_name.split(" ")
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()})",
+ "menu_name": f"{common_name} ({genus[0]}. {species_name.lower()})",
"scientific_name": scientific_name,
"family": family,
"family_order": families[family],
@@ -91,6 +91,55 @@ def save_species(conn: mdb.Connection,
}
+def update_species(# pylint: disable=[too-many-arguments]
+ conn: mdb.Connection,
+ species_id: int,
+ common_name: str,
+ scientific_name: str,
+ family: str,
+ family_order: int,
+ species_order: int
+):
+ """Update a species' details.
+
+ Parameters
+ ----------
+ conn: A connection to the MariaDB database.
+ species_id: The species identifier
+
+ Key-Word Arguments
+ ------------------
+ common_name: A layman's name for the species
+ scientific_name: A binomial nomenclature name for the species
+ family: The grouping under which the species falls
+ family_order: The ordering for the "family" above
+ species_order: The ordering of this species in relation to others
+ """
+ with conn.cursor(cursorclass=DictCursor) as cursor:
+ genus, species_name = scientific_name.split(" ")
+ species = {
+ "species_id": species_id,
+ "common_name": common_name,
+ "common_name_lower": common_name.lower(),
+ "menu_name": f"{common_name} ({genus[0]}. {species_name.lower()})",
+ "scientific_name": scientific_name,
+ "family": family,
+ "family_order": family_order,
+ "species_order": species_order
+ }
+ cursor.execute(
+ "UPDATE Species SET "
+ "SpeciesName=%(common_name)s, "
+ "Name=%(common_name_lower)s, "
+ "MenuName=%(menu_name)s, "
+ "FullName=%(scientific_name)s, "
+ "Family=%(family)s, "
+ "FamilyOrderId=%(family_order)s, "
+ "OrderId=%(species_order)s "
+ "WHERE Id=%(species_id)s",
+ species)
+
+
def species_families(conn: mdb.Connection) -> dict:
"""Retrieve the families under which species are grouped."""
with conn.cursor(cursorclass=DictCursor) as cursor: