"feat: Refactor database connection in app.py"

This commit is contained in:
Till Tomczak 2025-05-29 19:51:38 +02:00
parent e4e1bc4266
commit 9a30cfa005
2 changed files with 24 additions and 8 deletions

View File

@ -138,31 +138,38 @@ def load_user(user_id):
Robuster User-Loader mit Error-Handling für Schema-Probleme.
"""
try:
# user_id von Flask-Login ist immer ein String - zu Integer konvertieren
try:
user_id_int = int(user_id)
except (ValueError, TypeError):
app_logger.error(f"Ungültige User-ID: {user_id}")
return None
db_session = get_db_session()
# Robuste Abfrage mit Error-Handling
try:
user = db_session.query(User).filter(User.id == user_id).first()
user = db_session.query(User).filter(User.id == user_id_int).first()
db_session.close()
return user
except Exception as db_error:
# Schema-Problem - versuche manuelle Abfrage
app_logger.warning(f"Schema-Problem beim User-Load für ID {user_id}: {str(db_error)}")
app_logger.warning(f"Schema-Problem beim User-Load für ID {user_id_int}: {str(db_error)}")
# Manuelle Abfrage nur mit Basis-Feldern
try:
result = db_session.execute(
text("SELECT id, email, password_hash, name, role, active FROM users WHERE id = :user_id"),
{"user_id": user_id}
{"user_id": user_id_int}
).fetchone()
if result:
# Manuell User-Objekt erstellen
user = User()
user.id = result[0]
user.email = result[1] if len(result) > 1 else f"user_{user_id}@system.local"
user.email = result[1] if len(result) > 1 else f"user_{user_id_int}@system.local"
user.password_hash = result[2] if len(result) > 2 else ""
user.name = result[3] if len(result) > 3 else f"User {user_id}"
user.name = result[3] if len(result) > 3 else f"User {user_id_int}"
user.role = result[4] if len(result) > 4 else "user"
user.active = result[5] if len(result) > 5 else True
@ -4457,9 +4464,12 @@ def get_notifications():
try:
db_session = get_db_session()
# Sicherstellen, dass current_user.id als Integer behandelt wird
user_id = int(current_user.id)
# Benachrichtigungen für den aktuellen Benutzer laden
notifications = db_session.query(Notification).filter(
Notification.user_id == current_user.id
Notification.user_id == user_id
).order_by(Notification.created_at.desc()).limit(50).all()
notifications_data = [notification.to_dict() for notification in notifications]
@ -4485,9 +4495,12 @@ def mark_notification_read(notification_id):
try:
db_session = get_db_session()
# Sicherstellen, dass current_user.id als Integer behandelt wird
user_id = int(current_user.id)
notification = db_session.query(Notification).filter(
Notification.id == notification_id,
Notification.user_id == current_user.id
Notification.user_id == user_id
).first()
if not notification:
@ -4520,9 +4533,12 @@ def mark_all_notifications_read():
try:
db_session = get_db_session()
# Sicherstellen, dass current_user.id als Integer behandelt wird
user_id = int(current_user.id)
# Alle ungelesenen Benachrichtigungen des Benutzers finden und als gelesen markieren
updated_count = db_session.query(Notification).filter(
Notification.user_id == current_user.id,
Notification.user_id == user_id,
Notification.read == False
).update({"read": True})

Binary file not shown.