aboutsummaryrefslogtreecommitdiffstats
path: root/db.py
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-03-01 12:22:47 +0200
committerJan Tuomi <jan@jantuomi.fi>2024-12-21 19:43:13 +0200
commit4059c85229e8ab2a180e13bcca23c0e30687eedc (patch)
tree68338cf7a941269e4286229f40917ecf351ebd89 /db.py
parent4ad8fae0f1641f6c023d6de5e1407056b703a332 (diff)
Improve styles, error handling
Diffstat (limited to 'db.py')
-rw-r--r--db.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/db.py b/db.py
index e94c2a7..2ee4cc0 100644
--- a/db.py
+++ b/db.py
@@ -250,13 +250,17 @@ def update_poll_info(
author_name,
author_email,
is_whole_day,
- ) -> None:
+ ) -> str | None:
+ """Returns the id of the updated poll or None if not found."""
with db.cursor() as (conn, cur):
try:
cur.execute("UPDATE polls SET title = %s, description = %s, author_name = %s, author_email = %s, whole_day = %s "
- "WHERE manage_code = %s",
+ "WHERE manage_code = %s "
+ "RETURNING id",
(title, description, author_name, author_email, is_whole_day, code))
+ changed = cur.fetchone()
conn.commit()
+ return changed[0] if changed else None
except Exception as e:
conn.rollback()
raise e
@@ -273,7 +277,8 @@ def add_choice_to_poll(
with db.cursor() as (conn, cur):
try:
cur.execute("INSERT INTO choices (poll_id, start_datetime, end_datetime)"
- "VALUES (%s, %s, %s)",
+ "VALUES (%s, %s, %s) "
+ "RETURNING id",
(poll.id, start_datetime, end_datetime))
conn.commit()
except Exception as e: