feat: implement exercise promotion logic for training units
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Successful in 6s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 45s
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Successful in 6s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 45s
- Added functionality to promote private exercises used in training units to club visibility, allowing better access for trainers and members. - Introduced helper functions to retrieve distinct exercise IDs and group club IDs for scheduled units. - Updated the create, update, and quick create training unit methods to include exercise promotion logic, enhancing exercise management within clubs.
This commit is contained in:
1 parent
c6a7d668c5
commit
f9d518fb78
5 files changed
+502
-20
No files matched your search
@@ -653,6 +653,109 @@ def _replace_unit_sections(cur, unit_id: int, sections_in: List[Any]):
|
||||
_insert_section_items(cur, sid, sec.get("items"))
|
||||
|
||||
|
||||
def _distinct_exercise_ids_in_unit(cur, unit_id: int) -> List[int]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT DISTINCT tusi.exercise_id
|
||||
FROM training_unit_section_items tusi
|
||||
INNER JOIN training_unit_sections tus ON tusi.section_id = tus.id
|
||||
WHERE tus.training_unit_id = %s
|
||||
AND tusi.item_type = 'exercise'
|
||||
AND tusi.exercise_id IS NOT NULL
|
||||
""",
|
||||
(unit_id,),
|
||||
)
|
||||
rows = cur.fetchall() or []
|
||||
out: List[int] = []
|
||||
for r in rows:
|
||||
try:
|
||||
out.append(int(r["exercise_id"]))
|
||||
except (TypeError, ValueError, KeyError):
|
||||
continue
|
||||
return out
|
||||
|
||||
|
||||
def _group_club_id_for_scheduled_unit(cur, unit_id: int) -> Optional[int]:
|
||||
"""Nur echte Gruppentermine (keine Rahmen-Blueprints ohne Gruppe)."""
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT tg.club_id
|
||||
FROM training_units tu
|
||||
INNER JOIN training_groups tg ON tu.group_id = tg.id
|
||||
WHERE tu.id = %s AND tu.framework_slot_id IS NULL
|
||||
""",
|
||||
(unit_id,),
|
||||
)
|
||||
r = cur.fetchone()
|
||||
if not r or r.get("club_id") is None:
|
||||
return None
|
||||
return int(r["club_id"])
|
||||
|
||||
|
||||
def _caller_may_promote_exercise_to_club(
|
||||
cur,
|
||||
exercise_created_by: Optional[int],
|
||||
profile_id: int,
|
||||
role: str,
|
||||
target_club_id: int,
|
||||
) -> bool:
|
||||
if is_platform_admin(role):
|
||||
return True
|
||||
if exercise_created_by is not None and int(exercise_created_by) == profile_id:
|
||||
return True
|
||||
if can_manage_club_org(cur, profile_id, target_club_id, role):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _promote_private_exercises_used_in_unit(cur, unit_id: int, profile_id: int, role: str) -> None:
|
||||
"""
|
||||
Private Übungen in der Einheit auf visibility=club (Verein der Trainingsgruppe) setzen,
|
||||
damit andere Trainer und Mitglieder sie in der Durchführung sehen.
|
||||
"""
|
||||
target_club_id = _group_club_id_for_scheduled_unit(cur, unit_id)
|
||||
if not target_club_id:
|
||||
return
|
||||
if not (
|
||||
is_platform_admin(role)
|
||||
or _profile_active_in_club(cur, target_club_id, profile_id)
|
||||
or can_manage_club_org(cur, profile_id, target_club_id, role)
|
||||
):
|
||||
return
|
||||
|
||||
for eid in _distinct_exercise_ids_in_unit(cur, unit_id):
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, created_by, visibility, club_id, COALESCE(status, '') AS status
|
||||
FROM exercises WHERE id = %s
|
||||
""",
|
||||
(eid,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
continue
|
||||
if str(row.get("status") or "").strip().lower() == "archived":
|
||||
continue
|
||||
vis = (row.get("visibility") or "private").strip().lower()
|
||||
if vis == "official":
|
||||
continue
|
||||
if vis == "club":
|
||||
continue
|
||||
if vis != "private":
|
||||
continue
|
||||
cb = row.get("created_by")
|
||||
if not _caller_may_promote_exercise_to_club(cur, cb, profile_id, role, target_club_id):
|
||||
continue
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE exercises
|
||||
SET visibility = 'club', club_id = %s, updated_at = NOW()
|
||||
WHERE id = %s AND LOWER(COALESCE(visibility, 'private')) = 'private'
|
||||
""",
|
||||
(target_club_id, eid),
|
||||
)
|
||||
|
||||
|
||||
def _insert_sections_from_legacy_exercises(cur, unit_id: int, exercises_in: List[Any]):
|
||||
if not exercises_in:
|
||||
return
|
||||
@@ -1281,6 +1384,8 @@ def create_training_unit(data: dict, tenant: TenantContext = Depends(get_tenant_
|
||||
elif exercises_in is not None:
|
||||
_insert_sections_from_legacy_exercises(cur, unit_id, exercises_in)
|
||||
|
||||
_promote_private_exercises_used_in_unit(cur, unit_id, profile_id, role)
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(unit_id, tenant)
|
||||
@@ -1461,6 +1566,9 @@ def update_training_unit(unit_id: int, data: dict, tenant: TenantContext = Depen
|
||||
cur.execute("DELETE FROM training_unit_sections WHERE training_unit_id = %s", (unit_id,))
|
||||
_insert_sections_from_legacy_exercises(cur, unit_id, data["exercises"] or [])
|
||||
|
||||
if content_handled or "sections" in data or "exercises" in data:
|
||||
_promote_private_exercises_used_in_unit(cur, unit_id, profile_id, role)
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(unit_id, tenant)
|
||||
@@ -1572,6 +1680,8 @@ def create_training_unit_from_framework_slot(data: dict, tenant: TenantContext =
|
||||
slot_id,
|
||||
)
|
||||
|
||||
_promote_private_exercises_used_in_unit(cur, new_id, profile_id, role)
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(new_id, tenant)
|
||||
@@ -1644,6 +1754,7 @@ def quick_create_training_unit(data: dict, tenant: TenantContext = Depends(get_t
|
||||
|
||||
if tpl_id_safe:
|
||||
_instantiate_from_template(cur, unit_id, tpl_id_safe)
|
||||
_promote_private_exercises_used_in_unit(cur, unit_id, profile_id, role)
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
Reference in new issue
Block a user