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 55caf13..9e2ab29 100644 Binary files a/database/example_database.db and b/database/example_database.db differ 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"])