"feat: Refactor database connection in app.py"
This commit is contained in:
@@ -138,31 +138,38 @@ def load_user(user_id):
|
|||||||
Robuster User-Loader mit Error-Handling für Schema-Probleme.
|
Robuster User-Loader mit Error-Handling für Schema-Probleme.
|
||||||
"""
|
"""
|
||||||
try:
|
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()
|
db_session = get_db_session()
|
||||||
|
|
||||||
# Robuste Abfrage mit Error-Handling
|
# Robuste Abfrage mit Error-Handling
|
||||||
try:
|
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()
|
db_session.close()
|
||||||
return user
|
return user
|
||||||
except Exception as db_error:
|
except Exception as db_error:
|
||||||
# Schema-Problem - versuche manuelle Abfrage
|
# 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
|
# Manuelle Abfrage nur mit Basis-Feldern
|
||||||
try:
|
try:
|
||||||
result = db_session.execute(
|
result = db_session.execute(
|
||||||
text("SELECT id, email, password_hash, name, role, active FROM users WHERE id = :user_id"),
|
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()
|
).fetchone()
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
# Manuell User-Objekt erstellen
|
# Manuell User-Objekt erstellen
|
||||||
user = User()
|
user = User()
|
||||||
user.id = result[0]
|
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.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.role = result[4] if len(result) > 4 else "user"
|
||||||
user.active = result[5] if len(result) > 5 else True
|
user.active = result[5] if len(result) > 5 else True
|
||||||
|
|
||||||
@@ -4457,9 +4464,12 @@ def get_notifications():
|
|||||||
try:
|
try:
|
||||||
db_session = get_db_session()
|
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
|
# Benachrichtigungen für den aktuellen Benutzer laden
|
||||||
notifications = db_session.query(Notification).filter(
|
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()
|
).order_by(Notification.created_at.desc()).limit(50).all()
|
||||||
|
|
||||||
notifications_data = [notification.to_dict() for notification in notifications]
|
notifications_data = [notification.to_dict() for notification in notifications]
|
||||||
@@ -4485,9 +4495,12 @@ def mark_notification_read(notification_id):
|
|||||||
try:
|
try:
|
||||||
db_session = get_db_session()
|
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 = db_session.query(Notification).filter(
|
||||||
Notification.id == notification_id,
|
Notification.id == notification_id,
|
||||||
Notification.user_id == current_user.id
|
Notification.user_id == user_id
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
if not notification:
|
if not notification:
|
||||||
@@ -4520,9 +4533,12 @@ def mark_all_notifications_read():
|
|||||||
try:
|
try:
|
||||||
db_session = get_db_session()
|
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
|
# Alle ungelesenen Benachrichtigungen des Benutzers finden und als gelesen markieren
|
||||||
updated_count = db_session.query(Notification).filter(
|
updated_count = db_session.query(Notification).filter(
|
||||||
Notification.user_id == current_user.id,
|
Notification.user_id == user_id,
|
||||||
Notification.read == False
|
Notification.read == False
|
||||||
).update({"read": True})
|
).update({"read": True})
|
||||||
|
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user