From a1fa1ebf6e390d9705da13eb23e6b3fd3802d1f1 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Wed, 1 May 2024 21:19:47 +0200 Subject: Add application management page --- app.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'app.py') diff --git a/app.py b/app.py index 022681d..dc8c076 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,7 @@ import os import sqlite3 from bottle.ext import sqlite from beaker.middleware import SessionMiddleware +import functools load_dotenv() @@ -31,17 +32,29 @@ connection = sqlite3.connect(DB_PATH) cursor = connection.cursor() cursor.executescript(""" CREATE TABLE IF NOT EXISTS applications ( + id INTEGER PRIMARY KEY, username VARCHAR(12) NOT NULL, + userId INTEGER UNIQUE NOT NULL, preferredRole VARCHAR(6) NOT NULL, motivation TEXT NOT NULL, - userId INTEGER UNIQUE NOT NULL + applicationTime INT NOT NULL -- unix timestamp ); + INSERT OR IGNORE + INTO applications(username, userId, preferredRole, motivation, applicationTime) + VALUES + ('DillerBlaster69', 0, 'dps', 'fake motivation', strftime('%s','now')), + ('DillerDiller', 1, 'healer', 'fake motivation', strftime('%s','now')), + ('diller123', 2, 'tank', 'fake motivation', strftime('%s','now')), + ('susamongus', 3, 'dps', 'fake motivation #4', strftime('%s','now')); CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY, + username VARCHAR(12) NOT NULL, userId INTEGER UNIQUE NOT NULL, - role VARCHAR(6) NOT NULL + preferredRole VARCHAR(6) NOT NULL, + joinTime INT NOT NULL -- unix timestamp ); - INSERT OR IGNORE INTO users(userId, role) VALUES (1165955606, 'dps'); + INSERT OR IGNORE INTO users(userId, preferredRole, joinTime) VALUES (1165955606, 'dps', strftime('%s','now')); """) cursor.close() connection.close() @@ -164,7 +177,11 @@ def join_submission(db: sqlite3.Connection): raise HTTPError(400, "Missing or invalid user id") try: - db.execute("INSERT INTO applications(username, preferredRole, motivation, userId) VALUES (?, ?, ?, ?)", (name, preferred_role, motivation, user_id)) + db.execute(""" + INSERT + INTO applications(username, userId, preferredRole, motivation, applicationTime) + VALUES (?, ?, ?, strftime('%s','now'), ?) + """, (name, user_id, preferred_role, motivation)) except sqlite3.IntegrityError as e: print(e.sqlite_errorcode == sqlite3.SQLITE_CONSTRAINT_UNIQUE) print(str(e)) @@ -176,6 +193,48 @@ def join_submission(db: sqlite3.Connection): return template("join_success") +def require_authentication(fn): + """Decorator that ensures the client is logged in""" + @functools.wraps(fn) + def wrapped(db: sqlite3.Connection, *args, **kwargs): + # Ensure authentication + session = request.environ.get("beaker.session") + print(session) + user_id = session.get("user_id", None) + if user_id is None: + raise HTTPError(403, "Must be logged in! (missing cookie)") + user = db.execute("SELECT * FROM users WHERE userId = ?", [user_id]).fetchone() + if user is None: + raise HTTPError(403, "Must be logged in! (unknown user)") + + # Wrapped function may or may not want this + kwargs["db"] = db + + return fn(*args, **kwargs) + return wrapped + +@require_authentication +@app.route("/manage.html") +def manage(db: sqlite3.Connection): + applications = db.execute("SELECT * FROM applications").fetchall(); + return template("manage", applications=applications) + + +@require_authentication +@app.route("/manage//", method="POST") +def approve_application(action: str, user_id: int, db: sqlite3.Connection): + if action == "accept": + db.execute(""" + INSERT INTO users(username, userId, preferredRole, joinTime) + SELECT username, userId, preferredRole, strftime('%s','now') + FROM applications + WHERE userId = ?; + """, [user_id]) + print(user_id) + db.execute("DELETE FROM applications WHERE userId = ?", [user_id]) + + return f"Application {action}ed!" + @app.route("//") def server_static(type, filename): return static_file(filename, root=f"./static/{type}/") -- cgit v1.2.3