aboutsummaryrefslogtreecommitdiff
path: root/uploader
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-09-12 12:57:50 -0500
committerFrederick Muriuki Muriithi2024-09-12 12:57:50 -0500
commitf6096e430e2bb914225823a1953c109bad89d755 (patch)
treecabaadb1985b4c078c3612242c9a342a89010d25 /uploader
parent1a4804305c764cb71afa472561b132278113d842 (diff)
downloadgn-uploader-f6096e430e2bb914225823a1953c109bad89d755.tar.gz
Save the updated species details.
Diffstat (limited to 'uploader')
-rw-r--r--uploader/species/models.py47
-rw-r--r--uploader/species/views.py17
-rw-r--r--uploader/templates/species/edit-species.html3
3 files changed, 62 insertions, 5 deletions
diff --git a/uploader/species/models.py b/uploader/species/models.py
index 4426b64..1abed6d 100644
--- a/uploader/species/models.py
+++ b/uploader/species/models.py
@@ -91,6 +91,53 @@ def save_species(conn: mdb.Connection,
}
+def update_species(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 = 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.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:
diff --git a/uploader/species/views.py b/uploader/species/views.py
index f39ca98..55b0dd3 100644
--- a/uploader/species/views.py
+++ b/uploader/species/views.py
@@ -14,7 +14,11 @@ from uploader.db_utils import database_connection
from uploader.oauth2.client import oauth2_get, oauth2_post
from uploader.authorisation import require_login, require_token
-from .models import all_species, save_species, species_by_id, species_families
+from .models import (all_species,
+ save_species,
+ species_by_id,
+ update_species,
+ species_families)
speciesbp = Blueprint("species", __name__)
@@ -177,9 +181,14 @@ def edit_species_extra(token: dict, species_id: int):# pylint: disable=[unused-a
).either(__failure__, lambda res: res)
if bool(species) and request.method == "POST":
- flash("We would have edited the species, but the feature is not "
- "currently implemented …",
- "alert-danger")
+ update_species(conn,
+ species_id,
+ request.form["species_name"],
+ request.form["species_fullname"],
+ request.form["species_family"],
+ int(request.form["species_familyorderid"]),
+ int(request.form["species_orderid"]))
+ flash("Updated species successfully.", "alert-success")
return redirect(url_for("species.edit_species_extra",
species_id=species_id))
diff --git a/uploader/templates/species/edit-species.html b/uploader/templates/species/edit-species.html
index 6827751..5a26455 100644
--- a/uploader/templates/species/edit-species.html
+++ b/uploader/templates/species/edit-species.html
@@ -107,6 +107,7 @@
id="txt-species-familyorderid"
name="species_familyorderid"
value="{{species.FamilyOrderId}}"
+ required="required"
class="form-control" />
<small class="form-text text-muted">
This is a number that determines the order of the "Family" groupings
@@ -121,7 +122,7 @@
<input type="number"
id="txt-species-orderid"
name="species_orderid"
- value="{{species.OrderId or max_order_id}}"
+ value="{{species.OrderId or (max_order_id + 5)}}"
class="form-control" />
<small class="form-text text-muted">
This integer value determines the order of the species in relation to