Fix bugs with empty tables
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
7
main.py
7
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:
|
||||
|
||||
@@ -92,75 +92,79 @@
|
||||
</button>
|
||||
</form>
|
||||
<p>Füge bestehendem Prepaid-User Geld hinzu:</p>
|
||||
<form
|
||||
method="post"
|
||||
action="/add_money_prepaid_user"
|
||||
style="
|
||||
display: flex;
|
||||
gap: 1em;
|
||||
align-items: center;
|
||||
margin-bottom: 1em;
|
||||
background: var(--hellgrau);
|
||||
padding: 1em;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
|
||||
max-width: 600px;
|
||||
"
|
||||
>
|
||||
<label
|
||||
for="addmoney-username"
|
||||
style="margin: 0 0.5em 0 0; font-weight: bold"
|
||||
>Username:</label
|
||||
>
|
||||
<select
|
||||
id="addmoney-username"
|
||||
name="username"
|
||||
required
|
||||
{% if db_users_prepaid %}
|
||||
<form
|
||||
method="post"
|
||||
action="/add_money_prepaid_user"
|
||||
style="
|
||||
padding: 0.5em;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
gap: 1em;
|
||||
align-items: center;
|
||||
margin-bottom: 1em;
|
||||
background: var(--hellgrau);
|
||||
padding: 1em;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
|
||||
max-width: 600px;
|
||||
"
|
||||
>
|
||||
{% for db_user in db_users_prepaid %}
|
||||
<option value="{{ db_user.username }}">
|
||||
{{ db_user.username }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<label
|
||||
for="addmoney-money"
|
||||
style="margin: 0 0.5em 0 0; font-weight: bold"
|
||||
>Amount (€):</label
|
||||
>
|
||||
<input
|
||||
id="addmoney-money"
|
||||
type="number"
|
||||
name="money"
|
||||
placeholder="Money"
|
||||
step="0.01"
|
||||
required
|
||||
style="
|
||||
padding: 0.5em;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
width: 100px;
|
||||
"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
style="
|
||||
padding: 0.5em 1em;
|
||||
background: rgb(0, 97, 143);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
"
|
||||
>
|
||||
Add Money
|
||||
</button>
|
||||
</form>
|
||||
<label
|
||||
for="addmoney-username"
|
||||
style="margin: 0 0.5em 0 0; font-weight: bold"
|
||||
>Username:</label
|
||||
>
|
||||
<select
|
||||
id="addmoney-username"
|
||||
name="username"
|
||||
required
|
||||
style="
|
||||
padding: 0.5em;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
"
|
||||
>
|
||||
{% for db_user in db_users_prepaid %}
|
||||
<option value="{{ db_user.username }}">
|
||||
{{ db_user.username }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<label
|
||||
for="addmoney-money"
|
||||
style="margin: 0 0.5em 0 0; font-weight: bold"
|
||||
>Amount (€):</label
|
||||
>
|
||||
<input
|
||||
id="addmoney-money"
|
||||
type="number"
|
||||
name="money"
|
||||
placeholder="Money"
|
||||
step="0.01"
|
||||
required
|
||||
style="
|
||||
padding: 0.5em;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
width: 100px;
|
||||
"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
style="
|
||||
padding: 0.5em 1em;
|
||||
background: rgb(0, 97, 143);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
"
|
||||
>
|
||||
Add Money
|
||||
</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<p>Es sind keine Prepaid-User vorhanden.</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if 'Fachschaft Admins' in user.groups %}
|
||||
<h2>Admin Interface</h2>
|
||||
@@ -405,6 +409,7 @@
|
||||
</form>
|
||||
<h3>Prepaid Liste</h3>
|
||||
<p>Users in prepaid database:</p>
|
||||
{% if db_users_prepaid %}
|
||||
<table>
|
||||
<thread>
|
||||
<tr>
|
||||
@@ -418,7 +423,6 @@
|
||||
</tr>
|
||||
</thread>
|
||||
<tbody>
|
||||
{% if db_users_prepaid %}
|
||||
{% for prepaid_user_i in db_users_prepaid %}
|
||||
<tr
|
||||
{%
|
||||
@@ -454,13 +458,6 @@
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="7" style="text-align: center">
|
||||
No users in prepaid database
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
</table>
|
||||
<p>(De-)Activate User</p>
|
||||
@@ -514,6 +511,13 @@
|
||||
Toggle Activation
|
||||
</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="7" style="text-align: center">
|
||||
No users in prepaid database
|
||||
</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
{% endif %} {% endif %}
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user