From 1c7bf3c37e7c0e30ea9bc9355a86d69bd7032da3 Mon Sep 17 00:00:00 2001 From: TussockyJoker Date: Thu, 5 Mar 2026 22:00:42 +0000 Subject: [PATCH] can no longer have duplicate species in species database --- database.db | 0 database/db_commands.py | 37 ++++++++++-------------- database/db_init.py | 2 +- database/example_database.db | Bin 20480 -> 24576 bytes routes/species_portal/species_portal.py | 4 ++- 5 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 database.db diff --git a/database.db b/database.db new file mode 100644 index 0000000..e69de29 diff --git a/database/db_commands.py b/database/db_commands.py index 7b2a480..8f58f15 100644 --- a/database/db_commands.py +++ b/database/db_commands.py @@ -4,19 +4,22 @@ from database.db_connection import get_db def add_species(english, latin, body, category, extinction_risk, image_id): with get_db() as conn: - conn.execute(""" - INSERT INTO species ( - species_english, - species_latin, - body_text, - category, - extinction_risk, - created_at, - photoid + try: + conn.execute(""" + INSERT INTO species ( + species_english, + species_latin, + body_text, + category, + extinction_risk, + created_at, + photoid - ) - VALUES (?, ?, ?, ?, ?, ?, ?) - """, (english, latin.lower(), body, category, extinction_risk, datetime.now(), image_id)) + ) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, (english.title(), latin.lower(), body, category, extinction_risk, datetime.now(), image_id)) + except: + raise ValueError("This species already exists in the database, would you like to edit it?") def get_all_species(): @@ -48,16 +51,6 @@ WHERE ).fetchall() return [dict(row) for row in rows] - -def get_species_by_name(species_english): - with get_db() as conn: - row = conn.execute( - "SELECT * FROM species WHERE species_english = ?", - (species_english,) - ).fetchone() - return dict(row) if row else None - - def delete_species(species_id): with get_db() as conn: conn.execute( diff --git a/database/db_init.py b/database/db_init.py index 976d6df..c6ed40e 100644 --- a/database/db_init.py +++ b/database/db_init.py @@ -97,7 +97,7 @@ def init_database(): conn.execute(""" CREATE TABLE IF NOT EXISTS species ( species_id INTEGER PRIMARY KEY AUTOINCREMENT, - species_english TEXT, + species_english TEXT UNIQUE, species_latin TEXT, body_text TEXT, category TEXT, diff --git a/database/example_database.db b/database/example_database.db index 55caf13b812a66ac372c31f9383b3986cc5ff33c..9e2ab29674c68cba3a158a10d4d080e7b2ca7ba2 100644 GIT binary patch delta 434 zcmZut&r2IY6y9MlEivmejiT)yoHmsv*m&_IIf&S;2okAP+U8Js**tfr?#}AWt}!R| z7|3^=yCpVB@N*_GB zKFb2stvJ=f5WWtd3>04zDIxt76;40olgtZlzS$11Gmk=QkrAFIlTcek=wa68tj%Xw L3qvWQuEJ;cwH1KS delta 72 zcmZoTz}T>Wae}m9Bm)BjD-go~%S0VxprBrxCNIAx0~7CQ2Hw+r*Eb6)MDuPw&6~)` Tczv=if7E16zKYHN`57Gm4qXuv diff --git a/routes/species_portal/species_portal.py b/routes/species_portal/species_portal.py index b45ebb2..caddcff 100644 --- a/routes/species_portal/species_portal.py +++ b/routes/species_portal/species_portal.py @@ -14,7 +14,7 @@ def portal(): @page.route("/", methods=["POST"]) def add_species_to_database(): try: - eng_name = request.form["eng_name"].capitalize() + eng_name = request.form["eng_name"].title() latin_name = request.form["latin_name"].capitalize() main_text = request.form["main_text"] category = request.form["category"].capitalize() @@ -36,6 +36,8 @@ def add_species_to_database(): "species_portal/species_portal_failed.jinja", error_information=error_information ) + +# now add editing functionality @page.route("/delete/", methods=["POST"])