From 692a7fc3a5b4e6c655732c1b29274b66bea515d9 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Mon, 29 Apr 2024 10:31:06 +0200 Subject: Ensure unique applicants --- app.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'app.py') diff --git a/app.py b/app.py index 73926e3..cc16026 100644 --- a/app.py +++ b/app.py @@ -27,7 +27,7 @@ cursor.executescript(""" username VARCHAR(12) NOT NULL, preferredRole VARCHAR(6) NOT NULL, motivation TEXT NOT NULL, - userId INTEGER NOT NULL + userId INTEGER UNIQUE NOT NULL ); """) cursor.close() @@ -92,8 +92,16 @@ def join_submission(db: sqlite3.Connection): if user_id == None or not user_id.isdigit(): raise HTTPError(400, "Missing or invalid user id") - # FIXME: The user id is a 64-bit unsigned integer which may be larger than the INTEGER type of sqlite3. - db.execute(f"INSERT INTO applications(username, preferredRole, motivation, userId) VALUES (?, ?, ?, ?)", (name, preferred_role, motivation, user_id)) + try: + db.execute("INSERT INTO applications(username, preferredRole, motivation, userId) VALUES (?, ?, ?, ?)", (name, preferred_role, motivation, user_id)) + except sqlite3.IntegrityError as e: + print(e.sqlite_errorcode == sqlite3.SQLITE_CONSTRAINT_UNIQUE) + print(str(e)) + if e.sqlite_errorcode == sqlite3.SQLITE_CONSTRAINT_UNIQUE: + # The database (model) rejected the application because the unique constraint wasn't met! + raise HTTPError(400, "You've already submitted an application!") + else: + raise return template("join_success") -- cgit v1.2.3