chore: update versioning and enhance training unit features
- Incremented APP_VERSION to 0.8.10 and DB_SCHEMA_VERSION to 20260505037. - Added changelog entry for version 0.8.10 detailing database and API changes. - Refactored training framework program handling to support cloning training units from framework slots. - Improved permission checks for training units based on framework slot associations. - Introduced new API endpoint for creating training units from framework slots.
This commit is contained in:
1 parent
b4495e39c1
commit
7e21b44604
5 files changed
+525
-85
No files matched your search
@@ -14,7 +14,10 @@ from db import get_db, get_cursor, r2d
|
||||
|
||||
from routers.training_planning import (
|
||||
_has_planning_role,
|
||||
_hydrate_training_unit_payload,
|
||||
_optional_positive_int,
|
||||
_insert_sections_from_legacy_exercises,
|
||||
_replace_unit_sections,
|
||||
_validate_variant_for_exercise,
|
||||
)
|
||||
|
||||
@@ -35,23 +38,6 @@ def _framework_access(cur, framework_id: int, profile_id: int, role: str) -> Dic
|
||||
return row
|
||||
|
||||
|
||||
def _fetch_slot_exercises(cur, slot_id: int) -> List[Dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT t.id, t.slot_id, t.exercise_id, t.exercise_variant_id, t.order_index,
|
||||
e.title AS exercise_title,
|
||||
ev.variant_name AS exercise_variant_name
|
||||
FROM training_framework_slot_exercises t
|
||||
LEFT JOIN exercises e ON e.id = t.exercise_id
|
||||
LEFT JOIN exercise_variants ev ON ev.id = t.exercise_variant_id
|
||||
WHERE t.slot_id = %s
|
||||
ORDER BY t.order_index
|
||||
""",
|
||||
(slot_id,),
|
||||
)
|
||||
return [r2d(x) for x in cur.fetchall()]
|
||||
|
||||
|
||||
def _training_type_ids(cur, framework_id: int) -> List[int]:
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -101,7 +87,22 @@ def _hydrate_framework(cur, row: Dict[str, Any]) -> Dict[str, Any]:
|
||||
)
|
||||
slots = [r2d(s) for s in cur.fetchall()]
|
||||
for s in slots:
|
||||
s["exercises"] = _fetch_slot_exercises(cur, s["id"])
|
||||
cur.execute(
|
||||
"SELECT id FROM training_units WHERE framework_slot_id = %s",
|
||||
(s["id"],),
|
||||
)
|
||||
row_b = cur.fetchone()
|
||||
if not row_b:
|
||||
s["blueprint_training_unit_id"] = None
|
||||
s["sections"] = []
|
||||
s["exercises"] = []
|
||||
continue
|
||||
uid = row_b["id"]
|
||||
s["blueprint_training_unit_id"] = uid
|
||||
unit_min: Dict[str, Any] = {"id": uid}
|
||||
_hydrate_training_unit_payload(cur, unit_min)
|
||||
s["sections"] = unit_min.get("sections", [])
|
||||
s["exercises"] = unit_min.get("exercises", [])
|
||||
row["slots"] = slots
|
||||
row["training_type_ids"] = _training_type_ids(cur, fid)
|
||||
row["target_group_ids"] = _target_group_ids(cur, fid)
|
||||
@@ -191,10 +192,26 @@ def _insert_goal_rows(cur, framework_id: int, goals_in: List[Any]) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _insert_slots_and_exercises(
|
||||
def _insert_default_blueprint_section(cur, blueprint_unit_id: int) -> None:
|
||||
"""Leerer Ablauf, falls noch keine Sektionen existieren."""
|
||||
cur.execute(
|
||||
"SELECT 1 FROM training_unit_sections WHERE training_unit_id = %s",
|
||||
(blueprint_unit_id,),
|
||||
)
|
||||
if cur.fetchone():
|
||||
return
|
||||
_replace_unit_sections(
|
||||
cur,
|
||||
blueprint_unit_id,
|
||||
[{"title": "Ablauf", "order_index": 0, "guidance_notes": None, "items": []}],
|
||||
)
|
||||
|
||||
|
||||
def _insert_slots_and_blueprints(
|
||||
cur,
|
||||
framework_id: int,
|
||||
slots_in: Optional[List[Any]],
|
||||
profile_id: int,
|
||||
) -> None:
|
||||
if slots_in is None:
|
||||
return
|
||||
@@ -221,25 +238,44 @@ def _insert_slots_and_exercises(
|
||||
),
|
||||
)
|
||||
sid = cur.fetchone()["id"]
|
||||
ex_items = slot.get("exercises") or []
|
||||
for ej, raw in enumerate(ex_items):
|
||||
eid = raw.get("exercise_id")
|
||||
if not eid:
|
||||
continue
|
||||
eid = int(eid)
|
||||
vid = _optional_positive_int(raw.get("exercise_variant_id"), "exercise_variant_id")
|
||||
_validate_variant_for_exercise(cur, eid, vid)
|
||||
oidx = raw.get("order_index")
|
||||
if oidx is None:
|
||||
oidx = ej
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO training_framework_slot_exercises (
|
||||
slot_id, exercise_id, exercise_variant_id, order_index
|
||||
) VALUES (%s, %s, %s, %s)
|
||||
""",
|
||||
(sid, eid, vid, int(oidx)),
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO training_units (
|
||||
group_id, planned_date,
|
||||
planned_time_start, planned_time_end, planned_focus,
|
||||
status, notes, trainer_notes,
|
||||
created_by, plan_template_id, framework_slot_id
|
||||
) VALUES (
|
||||
NULL, NULL,
|
||||
NULL, NULL, NULL,
|
||||
'planned', NULL, NULL,
|
||||
%s, NULL, %s
|
||||
)
|
||||
RETURNING id
|
||||
""",
|
||||
(profile_id, sid),
|
||||
)
|
||||
bid = cur.fetchone()["id"]
|
||||
|
||||
sections_in = slot.get("sections")
|
||||
exercises_in = slot.get("exercises")
|
||||
|
||||
if sections_in is not None:
|
||||
if len(sections_in) == 0:
|
||||
_insert_default_blueprint_section(cur, bid)
|
||||
else:
|
||||
_replace_unit_sections(cur, bid, sections_in)
|
||||
elif exercises_in is not None and len(exercises_in) > 0:
|
||||
for raw in exercises_in:
|
||||
eid = raw.get("exercise_id")
|
||||
if not eid:
|
||||
continue
|
||||
vid = _optional_positive_int(raw.get("exercise_variant_id"), "exercise_variant_id")
|
||||
_validate_variant_for_exercise(cur, int(eid), vid)
|
||||
_insert_sections_from_legacy_exercises(cur, bid, exercises_in)
|
||||
else:
|
||||
_insert_default_blueprint_section(cur, bid)
|
||||
|
||||
|
||||
@router.get("/training-framework-programs")
|
||||
@@ -334,7 +370,7 @@ def create_training_framework_program(data: dict, session=Depends(require_auth))
|
||||
)
|
||||
fid = cur.fetchone()["id"]
|
||||
_insert_goal_rows(cur, fid, goals_in)
|
||||
_insert_slots_and_exercises(cur, fid, slots_in)
|
||||
_insert_slots_and_blueprints(cur, fid, slots_in, profile_id)
|
||||
_replace_training_types(cur, fid, tt_ids)
|
||||
_replace_target_groups(cur, fid, tg_ids)
|
||||
conn.commit()
|
||||
@@ -431,7 +467,7 @@ def update_training_framework_program(framework_id: int, data: dict, session=Dep
|
||||
"DELETE FROM training_framework_slots WHERE framework_program_id = %s",
|
||||
(framework_id,),
|
||||
)
|
||||
_insert_slots_and_exercises(cur, framework_id, data.get("slots") or [])
|
||||
_insert_slots_and_blueprints(cur, framework_id, data.get("slots") or [], profile_id)
|
||||
|
||||
if header_fields or "goals" in data or "slots" in data or "training_type_ids" in data or "target_group_ids" in data:
|
||||
cur.execute(
|
||||
|
||||
Reference in new issue
Block a user