From cd2b7ae14c0e8881c204a6024108e53ed4968974 Mon Sep 17 00:00:00 2001 From: Moritz Kowalski Date: Mon, 19 May 2025 12:14:23 +0200 Subject: [PATCH] Fix bugs with empty tables --- auth/oidc.py | 2 +- db/models.py | 9 +-- main.py | 7 +- templates/base.html | 152 +++++++++++++++++++++++--------------------- 4 files changed, 89 insertions(+), 81 deletions(-) diff --git a/auth/oidc.py b/auth/oidc.py index 971158d..96bfc97 100644 --- a/auth/oidc.py +++ b/auth/oidc.py @@ -106,7 +106,7 @@ async def authorize(request: Request): if result: user_db_id = result[0] else: - print("Create User in DB") + print(f"User {profile['preferred_username']} not found in database, creating new user.") user_db_id = create_postpaid_user(profile["preferred_username"]) request.session["user_db_id"] = user_db_id diff --git a/db/models.py b/db/models.py index 2ee55a1..2cecafc 100644 --- a/db/models.py +++ b/db/models.py @@ -78,13 +78,14 @@ def create_postpaid_user(username: str): int: The ID of the newly created user. """ - t = text("INSERT INTO users_postpaid (username) VALUES (:username)") + print(f"create_postpaid_user: {username}") + t_insert = text("INSERT INTO users_postpaid (username) VALUES (:username)") with engine.connect() as connection: - t = text("SELECT * FROM users_postpaid WHERE username = :username") - if connection.execute(t, {"username": username}).fetchone(): + t_select = text("SELECT * FROM users_postpaid WHERE username = :username") + if connection.execute(t_select, {"username": username}).fetchone(): raise HTTPException(status_code=400, detail="User already exists") try: - res = connection.execute(t, {"username": username}) + res = connection.execute(t_insert, {"username": username}) if res.rowcount == 0: raise HTTPException(status_code=500, detail="Failed to create user") except Exception as e: diff --git a/main.py b/main.py index 8964792..2d090f0 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,7 @@ from auth import oidc ADMIN_GROUP = "Fachschaft Admins" +FS_GROUP = "Fachschaft" app = FastAPI() app.add_middleware(SessionMiddleware, secret_key="my_secret_key") @@ -59,6 +60,8 @@ def home(request: Request): user_db = get_postpaid_user(row[0]) if user_db: users.append(user_db) + if FS_GROUP in user_authentik["groups"]: + with engine.connect() as conn: t = text("SELECT id FROM users_prepaid") result = conn.execute(t).fetchall() if result: @@ -146,7 +149,7 @@ def drink(request: Request): """ user_authentik = request.session.get("user_authentik") - if not user_authentik or ADMIN_GROUP not in user_authentik["groups"]: + if not user_authentik or FS_GROUP not in user_authentik["groups"]: raise HTTPException(status_code=403, detail="Nicht erlaubt") user_db_id = request.session.get("user_db_id") @@ -252,7 +255,7 @@ def toggle_activated_user_prepaid(request: Request, username: str = Form(...)): @app.post("/add_money_prepaid_user") def add_money_prepaid_user(request: Request, username: str = Form(...), money: float = Form(...)): curr_user_auth = request.session.get("user_authentik") - if not curr_user_auth or ADMIN_GROUP not in curr_user_auth["groups"]: + if not curr_user_auth or FS_GROUP not in curr_user_auth["groups"]: raise HTTPException(status_code=403, detail="Nicht erlaubt") curr_user_db_id = request.session.get("user_db_id") if not curr_user_db_id: diff --git a/templates/base.html b/templates/base.html index 4a29990..688b7be 100644 --- a/templates/base.html +++ b/templates/base.html @@ -92,75 +92,79 @@

Füge bestehendem Prepaid-User Geld hinzu:

-
- - - - - -
+ + + + + + + {% else %} +

Es sind keine Prepaid-User vorhanden.

+ {% endif %} {% endif %} {% if 'Fachschaft Admins' in user.groups %}

Admin Interface

@@ -405,6 +409,7 @@

Prepaid Liste

Users in prepaid database:

+ {% if db_users_prepaid %} @@ -418,7 +423,6 @@ - {% if db_users_prepaid %} {% for prepaid_user_i in db_users_prepaid %} {% endfor %} - {% else %} - - - - {% endif %}
- No users in prepaid database -

(De-)Activate User

@@ -514,6 +511,13 @@ Toggle Activation + {% else %} + + + No users in prepaid database + + + {% endif %} {% endif %} {% endif %}