Code Refactoring – ScenarioRoutes.py¶
Date: 2025-11-20
Author: Claude Code (Automated Refactoring)
Commit: b4bd184
Summary¶
The monolithic ScenarioRoutes.py (729 lines, 11 routes) was refactored into a package with four clearly separated modules. All endpoints, auth decorators, and DB access remain unchanged; maintainability improves significantly.
New Structure¶
| Module | Purpose | Key routes |
|---|---|---|
scenario_crud.py |
Scenario CRUD (list, details, create, update, delete) | /admin/scenarios, /admin/create_scenario, /admin/delete_scenario/<id> |
scenario_management.py |
Thread/evaluator distribution (round‑robin) | /admin/add_threads_to_scenario, /admin/add_viewers_to_scenario |
scenario_resources.py |
Reference data for UI | /admin/get_function_types, /admin/get_users, /admin/get_threads_from_function_type/<id> |
scenario_stats.py |
Progress and status calculations | /admin/scenario_progress_stats/<id> |
__init__.py registers the modules in the package.
Quality Gains¶
- Single responsibility: CRUD, management, resources, and stats are separated.
- Maintainability: Smaller files, clearer boundaries, easier debugging.
- Testability: Modules can be checked independently (syntax checks passed).
- Documentation: Module and route docstrings added.
Repository Changes¶
D app/routes/ScenarioRoutes.py # monolithic (backup kept separately)
A app/routes/scenarios/__init__.py
A app/routes/scenarios/scenario_crud.py
A app/routes/scenarios/scenario_management.py
A app/routes/scenarios/scenario_resources.py
A app/routes/scenarios/scenario_stats.py
M app/routes/__init__.py # new imports
Compatibility & Security¶
- All 11 routes keep URL, methods, decorators (
@admin_required), and responses. - Authentik auth (with optional legacy compatibility) unchanged; user context (
g.keycloak_user) preserved. - No DB migration required; query and transaction logic unchanged.
Recommendations¶
- Apply similar splitting to other large files (e.g.,
routes_socketio.py,MailRatingRoutes.py,routes.py,UserPromptRoutes.py). - Add unit tests per module to secure the separation long‑term.