diff options
author | Linnnus <[email protected]> | 2024-04-29 10:31:06 +0200 |
---|---|---|
committer | Linnnus <[email protected]> | 2024-04-29 10:31:06 +0200 |
commit | 692a7fc3a5b4e6c655732c1b29274b66bea515d9 (patch) | |
tree | 4924df0312b5f4fb78796b080c35039ec5870732 | |
parent | 4ea85a09f7ab6932472ecfcf43c82515928df477 (diff) |
Ensure unique applicants
-rw-r--r-- | app.py | 14 |
1 files changed, 11 insertions, 3 deletions
@@ -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") |