Update OpenAI API key and enhance app functionality: Replace the OpenAI API key in the .env file for improved access. Refactor app.py to include error handling for missing API keys and implement dark mode functionality with session management. Update README.md to reflect the use of Tailwind CSS via CDN and document the Content Security Policy (CSP) adjustments. Enhance mindmap data loading with a new API endpoint for refreshing data, ensuring better user experience during database connection issues. Update styles and templates for improved UI consistency and responsiveness.
This commit is contained in:
79
utils/db_check.py
Normal file
79
utils/db_check.py
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy import text
|
||||
import time
|
||||
|
||||
def check_db_connection(db):
|
||||
"""
|
||||
Überprüft die Datenbankverbindung und versucht ggf. die Verbindung wiederherzustellen
|
||||
|
||||
Args:
|
||||
db: SQLAlchemy-Instanz
|
||||
|
||||
Returns:
|
||||
bool: True, wenn die Verbindung erfolgreich ist, sonst False
|
||||
"""
|
||||
max_retries = 3
|
||||
retry_count = 0
|
||||
|
||||
while retry_count < max_retries:
|
||||
try:
|
||||
# Führe eine einfache Abfrage durch, um die Verbindung zu testen
|
||||
with current_app.app_context():
|
||||
db.session.execute(text('SELECT 1'))
|
||||
return True
|
||||
except SQLAlchemyError as e:
|
||||
retry_count += 1
|
||||
print(f"DB-Verbindungsfehler (Versuch {retry_count}/{max_retries}): {str(e)}")
|
||||
|
||||
if retry_count < max_retries:
|
||||
# Warte vor dem nächsten Versuch
|
||||
time.sleep(1)
|
||||
|
||||
# Versuche die Verbindung zurückzusetzen
|
||||
try:
|
||||
db.session.rollback()
|
||||
except:
|
||||
pass
|
||||
|
||||
return False
|
||||
|
||||
def initialize_db_if_needed(db, initialize_function=None):
|
||||
"""
|
||||
Initialisiert die Datenbank, falls erforderlich
|
||||
|
||||
Args:
|
||||
db: SQLAlchemy-Instanz
|
||||
initialize_function: Funktion, die aufgerufen wird, um die Datenbank zu initialisieren
|
||||
|
||||
Returns:
|
||||
bool: True, wenn die Datenbank bereit ist, sonst False
|
||||
"""
|
||||
# Prüfe die Verbindung
|
||||
if not check_db_connection(db):
|
||||
return False
|
||||
|
||||
# Prüfe, ob die Tabellen existieren
|
||||
try:
|
||||
with current_app.app_context():
|
||||
# Führe eine Testabfrage auf einer Tabelle durch
|
||||
db.session.execute(text('SELECT COUNT(*) FROM user'))
|
||||
except SQLAlchemyError:
|
||||
# Tabellen existieren nicht, erstelle sie
|
||||
try:
|
||||
with current_app.app_context():
|
||||
db.create_all()
|
||||
|
||||
# Rufe die Initialisierungsfunktion auf, falls vorhanden
|
||||
if initialize_function and callable(initialize_function):
|
||||
initialize_function()
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Fehler bei DB-Initialisierung: {str(e)}")
|
||||
return False
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user