feat(training-modules): implement training module functionality and UI integration
Deploy Development / deploy (push) Successful in 39s
Test Suite / pytest-backend (push) Successful in 36s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 12s
Test Suite / playwright-tests (push) Successful in 56s
Deploy Development / deploy (push) Successful in 39s
Test Suite / pytest-backend (push) Successful in 36s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 12s
Test Suite / playwright-tests (push) Successful in 56s
- Added new API endpoints for managing training modules, including listing, creating, updating, and deleting modules. - Implemented the ability to apply training modules to training units, allowing users to copy module content into specific sections. - Enhanced the frontend with new pages for managing training modules and integrated modal functionality for applying modules within the training planning page. - Updated version to 0.8.97 and adjusted database schema version accordingly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
59d53d6154
commit
c1243651bb
13 files changed
+2114
-10
No files matched your search
@@ -17,6 +17,7 @@ from club_tenancy import (
|
||||
is_platform_admin,
|
||||
library_content_visible_to_profile,
|
||||
)
|
||||
from routers.training_modules import load_training_module_for_apply
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["training_planning"])
|
||||
|
||||
@@ -568,6 +569,93 @@ def _hydrate_training_unit_payload(cur, unit: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return unit
|
||||
|
||||
|
||||
def _resolve_training_unit_section_id(cur, unit_id: int, section_order_index: int) -> int:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id FROM training_unit_sections
|
||||
WHERE training_unit_id = %s AND order_index = %s
|
||||
""",
|
||||
(unit_id, section_order_index),
|
||||
)
|
||||
r = cur.fetchone()
|
||||
if not r:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Abschnitt für diese Reihenfolge nicht gefunden"
|
||||
)
|
||||
return int(r["id"])
|
||||
|
||||
|
||||
def _append_copied_module_items_to_section(
|
||||
cur,
|
||||
section_id: int,
|
||||
module_items: List[Dict[str, Any]],
|
||||
source_training_module_id: int,
|
||||
) -> None:
|
||||
"""Hängt kopierte Modul‑Items ans Ende eines Abschnitts (section_order_index in API)."""
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT COALESCE(MAX(order_index), -1) AS mo
|
||||
FROM training_unit_section_items
|
||||
WHERE section_id = %s
|
||||
""",
|
||||
(section_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
start = int(row["mo"]) + 1 if row and row["mo"] is not None else 0
|
||||
|
||||
for i, mi in enumerate(module_items):
|
||||
oi = start + i
|
||||
itype = mi.get("item_type")
|
||||
if itype == "note":
|
||||
body = mi.get("note_body")
|
||||
if body is None:
|
||||
body = ""
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO training_unit_section_items (
|
||||
section_id, order_index, item_type,
|
||||
exercise_id, exercise_variant_id,
|
||||
planned_duration_min, actual_duration_min,
|
||||
notes, modifications, note_body, source_training_module_id
|
||||
) VALUES (%s, %s, 'note',
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, %s, %s)
|
||||
""",
|
||||
(section_id, oi, body, source_training_module_id),
|
||||
)
|
||||
continue
|
||||
|
||||
eid = mi.get("exercise_id")
|
||||
if not eid:
|
||||
continue
|
||||
eid = int(eid)
|
||||
vid = mi.get("exercise_variant_id")
|
||||
if vid is not None:
|
||||
vid = int(vid)
|
||||
else:
|
||||
vid = None
|
||||
_validate_variant_for_exercise(cur, eid, vid)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO training_unit_section_items (
|
||||
section_id, order_index, item_type,
|
||||
exercise_id, exercise_variant_id,
|
||||
planned_duration_min, actual_duration_min,
|
||||
notes, modifications, note_body, source_training_module_id
|
||||
) VALUES (%s, %s, 'exercise',
|
||||
%s, %s, %s, NULL, %s, NULL, NULL, %s)
|
||||
""",
|
||||
(
|
||||
section_id,
|
||||
oi,
|
||||
eid,
|
||||
vid,
|
||||
mi.get("planned_duration_min"),
|
||||
mi.get("notes"),
|
||||
source_training_module_id,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _insert_section_items(cur, section_id: int, items_in: Optional[List[Any]], start_order: int = 0):
|
||||
if items_in is None:
|
||||
items_in = []
|
||||
@@ -1443,6 +1531,46 @@ def get_training_unit(unit_id: int, tenant: TenantContext = Depends(get_tenant_c
|
||||
return unit
|
||||
|
||||
|
||||
@router.post("/training-units/{unit_id}/apply-training-module")
|
||||
def apply_training_module_to_training_unit(
|
||||
unit_id: int, data: dict, tenant: TenantContext = Depends(get_tenant_context)
|
||||
):
|
||||
"""Kopiert die Positionen eines Trainingsmoduls ans Ende eines Abschnitts (lokal bearbeitbar)."""
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
if not _has_planning_role(role):
|
||||
raise HTTPException(status_code=403, detail="Nur Trainer dürfen Module übernehmen")
|
||||
|
||||
module_id_raw = data.get("module_id")
|
||||
if module_id_raw is None or module_id_raw == "":
|
||||
raise HTTPException(status_code=400, detail="module_id ist Pflicht")
|
||||
try:
|
||||
module_id = int(module_id_raw)
|
||||
except (TypeError, ValueError):
|
||||
raise HTTPException(status_code=400, detail="module_id ungültig")
|
||||
|
||||
soy = data.get("section_order_index")
|
||||
try:
|
||||
section_order_index = int(soy)
|
||||
except (TypeError, ValueError):
|
||||
raise HTTPException(status_code=400, detail="section_order_index ist Pflicht (Ganzzahl)")
|
||||
if section_order_index < 0:
|
||||
raise HTTPException(status_code=400, detail="section_order_index ungültig")
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
unit_row = _training_unit_guard_row(cur, unit_id)
|
||||
_assert_training_unit_permission(cur, unit_row, profile_id, role)
|
||||
|
||||
section_id = _resolve_training_unit_section_id(cur, unit_id, section_order_index)
|
||||
mod_items, src_mid = load_training_module_for_apply(cur, module_id, profile_id, role)
|
||||
_append_copied_module_items_to_section(cur, section_id, mod_items, src_mid)
|
||||
_promote_private_exercises_used_in_unit(cur, unit_id, profile_id, role)
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(unit_id, tenant)
|
||||
|
||||
|
||||
@router.post("/training-units")
|
||||
def create_training_unit(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
|
||||
Reference in new issue
Block a user