diff --git a/__pycache__/app.cpython-313.pyc b/__pycache__/app.cpython-313.pyc index 2e4e59d..b64110b 100644 Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ diff --git a/app.py b/app.py index 99dffc9..b9bebf6 100644 --- a/app.py +++ b/app.py @@ -382,6 +382,18 @@ def admin_required(f): return f(*args, **kwargs) return decorated_function +# Hilfsfunktion zur Fehlerbehandlung in API-Endpunkten +def handle_api_exception(func): + """Decorator für API-Endpunkte zur einheitlichen Fehlerbehandlung""" + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as e: + # Log und API-Fehler zurückgeben + return ErrorHandler.handle_exception(e, is_api_request=True) + return wrapper + @login_manager.user_loader def load_user(id): # Verwende session.get() anstelle von query.get() für SQLAlchemy 2.0 Kompatibilität diff --git a/logs/app.log b/logs/app.log index 03626be..2eea1df 100644 --- a/logs/app.log +++ b/logs/app.log @@ -25,3 +25,9 @@ 2025-05-10 23:17:04,727 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] 2025-05-10 23:17:06,698 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] 2025-05-10 23:17:06,698 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] +2025-05-10 23:23:26,898 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] +2025-05-10 23:23:28,862 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] +2025-05-10 23:23:28,862 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] +2025-05-10 23:24:45,296 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] +2025-05-10 23:24:47,176 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] +2025-05-10 23:24:47,176 INFO: Anwendung gestartet [in C:\Users\TTOMCZA.EMEA\Dev\website\app.py:76] diff --git a/utils/__pycache__/db_test.cpython-313.pyc b/utils/__pycache__/db_test.cpython-313.pyc index 39ed7c3..57337ac 100644 Binary files a/utils/__pycache__/db_test.cpython-313.pyc and b/utils/__pycache__/db_test.cpython-313.pyc differ diff --git a/utils/db_test.py b/utils/db_test.py index f3653fa..f4f5bb3 100644 --- a/utils/db_test.py +++ b/utils/db_test.py @@ -9,9 +9,19 @@ import sqlite3 parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, parent_dir) -from app import app, db_path +# Vermeidung zirkulärer Importe - importiere nur die Modelle und DB-Objekt from models import db, User, Thought, MindMapNode, Category +def get_db_path(): + """Ermittelt den Pfad zur Datenbankdatei""" + db_dir = os.path.join(parent_dir, 'database') + if not os.path.exists(db_dir): + os.makedirs(db_dir) + return os.path.join(db_dir, 'systades.db') + +# Datenbank-Pfad +db_path = get_db_path() + def test_database_connection(): """Test if the database exists and can be connected to.""" try: @@ -37,52 +47,52 @@ def test_database_connection(): def test_models(): """Test if all models are properly defined and can be queried.""" - with app.app_context(): - try: - print("\nTesting User model...") - user_count = User.query.count() - print(f" Found {user_count} users") - - print("\nTesting Category model...") - category_count = Category.query.count() - print(f" Found {category_count} categories") - - print("\nTesting MindMapNode model...") - node_count = MindMapNode.query.count() - print(f" Found {node_count} mindmap nodes") - - print("\nTesting Thought model...") - thought_count = Thought.query.count() - print(f" Found {thought_count} thoughts") - - if user_count == 0: - print("\nWARNING: No users found in the database. You might need to create an admin user.") - - return True - except Exception as e: - print(f"Error testing models: {e}") - return False + # Import app here to avoid circular import + from flask import current_app + try: + print("\nTesting User model...") + user_count = User.query.count() + print(f" Found {user_count} users") + + print("\nTesting Category model...") + category_count = Category.query.count() + print(f" Found {category_count} categories") + + print("\nTesting MindMapNode model...") + node_count = MindMapNode.query.count() + print(f" Found {node_count} mindmap nodes") + + print("\nTesting Thought model...") + thought_count = Thought.query.count() + print(f" Found {thought_count} thoughts") + + if user_count == 0: + print("\nWARNING: No users found in the database. You might need to create an admin user.") + + return True + except Exception as e: + print(f"Error testing models: {e}") + return False def print_database_stats(): """Print database statistics.""" - with app.app_context(): - try: - stats = [] - stats.append(("Users", User.query.count())) - stats.append(("Categories", Category.query.count())) - stats.append(("Mindmap Nodes", MindMapNode.query.count())) - stats.append(("Thoughts", Thought.query.count())) - - print("\nDatabase Statistics:") - print("-" * 40) - for name, count in stats: - print(f"{name:<20} : {count}") - print("-" * 40) - - return True - except Exception as e: - print(f"Error generating database statistics: {e}") - return False + try: + stats = [] + stats.append(("Users", User.query.count())) + stats.append(("Categories", Category.query.count())) + stats.append(("Mindmap Nodes", MindMapNode.query.count())) + stats.append(("Thoughts", Thought.query.count())) + + print("\nDatabase Statistics:") + print("-" * 40) + for name, count in stats: + print(f"{name:<20} : {count}") + print("-" * 40) + + return True + except Exception as e: + print(f"Error generating database statistics: {e}") + return False def run_all_tests(): """Run all database tests.""" @@ -97,15 +107,18 @@ def run_all_tests(): if not test_database_connection(): success = False - # Test models - print("\n2. Testing database models...") - if not test_models(): - success = False - - # Print statistics - print("\n3. Database statistics:") - if not print_database_stats(): - success = False + # Import app here to avoid circular import + from app import app + with app.app_context(): + # Test models + print("\n2. Testing database models...") + if not test_models(): + success = False + + # Print statistics + print("\n3. Database statistics:") + if not print_database_stats(): + success = False print("\n" + "=" * 60) if success: @@ -117,4 +130,7 @@ def run_all_tests(): return success if __name__ == "__main__": - run_all_tests() \ No newline at end of file + # Import app here to avoid circular import + from app import app + with app.app_context(): + run_all_tests() \ No newline at end of file