From 72ed46d53cbee7b907d22de67abb91dd6c837b70 Mon Sep 17 00:00:00 2001 From: jamulix Date: Wed, 29 Oct 2025 10:18:58 +0100 Subject: [PATCH] Fix: Lazy initialization of services to prevent empty lessons list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: - Blueprints were imported BEFORE lessons were loaded - routes/training.py imported 'lessons' when it was still empty [] - This caused "Lektion nicht gefunden" / "Unbekannte Lektion" Solution: - Changed to import entire 'helpers' module instead of 'lessons' - Implemented lazy initialization with get_services() - Services are created on first route access AFTER lessons are loaded - Updated all route handlers to use get_services() This fixes the timing issue between blueprint imports and lesson loading. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- routes/training.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/routes/training.py b/routes/training.py index 2edc59d..afeaa52 100644 --- a/routes/training.py +++ b/routes/training.py @@ -6,7 +6,7 @@ Handles all training-related routes import logging from flask import Blueprint, render_template, request, jsonify from models import db -from services.helpers import lessons +from services import helpers from services.lesson_service import LessonService from services.training_service import TrainingService @@ -14,15 +14,25 @@ logger = logging.getLogger(__name__) training_bp = Blueprint('training', __name__) -# Initialize services -lesson_service = LessonService(lessons) -training_service = TrainingService(lesson_service) +# Initialize services (will be populated when lessons are loaded) +lesson_service = None +training_service = None + + +def get_services(): + """Lazy initialization of services after lessons are loaded""" + global lesson_service, training_service + if lesson_service is None: + lesson_service = LessonService(helpers.lessons) + training_service = TrainingService(lesson_service) + return lesson_service, training_service @training_bp.route('/train') def train(): """Hauptseite des Tipptrainers""" try: + _, training_service = get_services() context = training_service.get_training_context() return render_template('index.html', **context) @@ -72,6 +82,7 @@ def update_progress(): try: # Update progress and get statistics + _, training_service = get_services() stats = training_service.update_training_progress( user_input=user_input, current_text=current_text, @@ -99,6 +110,7 @@ def update_progress(): def next_text(): """Wechselt zum nächsten Text""" try: + _, training_service = get_services() result = training_service.switch_to_next_lesson() return jsonify(result) @@ -117,6 +129,7 @@ def set_lesson(): if lesson_index is None or not isinstance(lesson_index, int): return jsonify({'error': 'Ungültiger Lektionsindex'}), 400 + _, training_service = get_services() success, result = training_service.switch_to_lesson(lesson_index) if not success: @@ -154,7 +167,7 @@ def save_lesson_statistics(): return jsonify({'error': 'Ungültige Datentypen'}), 400 # Bereichsvalidierung - if lesson_index < 0 or lesson_index >= len(lessons): + if lesson_index < 0 or lesson_index >= len(helpers.lessons): return jsonify({'error': 'Ungültiger Lektionsindex'}), 400 if chars_per_minute < 0 or error_rate < 0 or wpm < 0 or training_duration < 0: @@ -188,6 +201,7 @@ def save_lesson_statistics(): def reset_lesson(): """Setzt den Fortschritt der aktuellen Lektion zurück""" try: + _, training_service = get_services() success, result = training_service.reset_current_lesson() if not success: