aboutsummaryrefslogtreecommitdiffstats
path: root/db.py
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-02-29 09:35:55 +0200
committerJan Tuomi <jan@jantuomi.fi>2024-12-21 19:43:13 +0200
commit65b11d9d029558cacc7b4270dd7eaa2f1f63439e (patch)
treecfaff12ae1b2daba749336e8aecd878035488e54 /db.py
parent513d520722fe86635965aec2fb5c6666b51c4be2 (diff)
Fix issue where an empty vote submission would not visually show
Diffstat (limited to 'db.py')
-rw-r--r--db.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/db.py b/db.py
index 694f53d..42680cb 100644
--- a/db.py
+++ b/db.py
@@ -1,4 +1,5 @@
import os
+from typing import Literal
from dataclasses import dataclass
import datetime
import psycopg2
@@ -53,6 +54,7 @@ class Vote:
poll_id: str
choice_id: str
voter_name: str
+ value: int # 0 or 1
@dataclass
class Choice:
@@ -112,6 +114,7 @@ def tuple_to_vote(vote_t: tuple) -> Vote:
poll_id=vote_t[1],
choice_id=vote_t[2],
voter_name=vote_t[3],
+ value=vote_t[4],
)
def get_poll(id: str):
@@ -178,13 +181,14 @@ def create_poll(title: str,
conn.rollback()
raise e
-def vote_poll(poll_id: str, voter_name: str, choice_ids: list[str]):
+def vote_poll(poll_id: str, voter_name: str, selections: dict[str, int]) -> None:
with db.cursor() as (conn, cur):
try:
- for choice_id in choice_ids:
- cur.execute("INSERT INTO votes (poll_id, voter_name, choice_id)"
- "VALUES (%s, %s, %s)",
- (poll_id, voter_name, choice_id))
+ for choice_id in selections:
+ value = selections[choice_id]
+ cur.execute("INSERT INTO votes (poll_id, voter_name, choice_id, value)"
+ "VALUES (%s, %s, %s, %s)",
+ (poll_id, voter_name, choice_id, value))
conn.commit()
except Exception as e: