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
@@ -17,6 +17,7 @@ from pydantic import BaseModel, Field, model_validator
|
||||
from db import get_db, get_cursor, r2d
|
||||
from club_tenancy import (
|
||||
assert_valid_governance_visibility,
|
||||
can_manage_club_org,
|
||||
club_admin_shares_club_with_creator,
|
||||
has_club_role,
|
||||
is_platform_admin,
|
||||
@@ -778,6 +779,10 @@ def bulk_patch_exercises_metadata(
|
||||
Übungen vollständig durch diese Liste ersetzt (leeres Array entfernt alle).
|
||||
|
||||
Berechtigt: Ersteller der jeweiligen Übung oder Plattform-Admin (admin/superadmin).
|
||||
Zusätzlich: Vereinsorga (club_admin) darf **nur** bei reiner Sichtbarkeitsänderung auf ``club``
|
||||
für den eigenen Verein (`club_id` / aktiver Verein) fremde Übungen freigeben — analog
|
||||
Trainingseinheit-Speichern.
|
||||
|
||||
Governance wie bei Einzel-PUT (official nur Plattform-Admin; club mit Mitgliedschaft bzw. Admin).
|
||||
"""
|
||||
profile_id = tenant.profile_id
|
||||
@@ -861,14 +866,6 @@ def bulk_patch_exercises_metadata(
|
||||
owner = rowd.get("created_by")
|
||||
if owner is not None:
|
||||
owner = int(owner)
|
||||
if owner != profile_id and not is_platform_admin(role):
|
||||
failed.append(
|
||||
{
|
||||
"id": ex_id,
|
||||
"detail": "Keine Berechtigung (nur Ersteller oder Plattform-Admin)",
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
ex_vis = (rowd.get("visibility") or "private").strip().lower()
|
||||
ex_cid_raw = rowd.get("club_id")
|
||||
@@ -882,18 +879,45 @@ def bulk_patch_exercises_metadata(
|
||||
if patch_visibility and body.club_id is not None:
|
||||
next_club = int(body.club_id)
|
||||
|
||||
if patch_visibility and next_vis == "club" and next_club is None:
|
||||
eff = tenant.effective_club_id
|
||||
next_club = int(eff) if eff is not None else None
|
||||
|
||||
if patch_visibility and next_vis == "club" and next_club is None:
|
||||
failed.append(
|
||||
{
|
||||
"id": ex_id,
|
||||
"detail": "Vereins-Übung: club_id angeben oder aktiven Verein wählen (X-Active-Club-Id).",
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
other_meta_patches = (
|
||||
patch_status
|
||||
or patch_focus_areas
|
||||
or patch_style_dirs
|
||||
or patch_training_types
|
||||
or patch_target_groups
|
||||
)
|
||||
is_owner_or_platform = owner == profile_id or is_platform_admin(role)
|
||||
if not is_owner_or_platform:
|
||||
org_club_promo_only = (
|
||||
patch_visibility
|
||||
and not other_meta_patches
|
||||
and next_vis == "club"
|
||||
and next_club is not None
|
||||
and can_manage_club_org(cur, profile_id, int(next_club), role)
|
||||
)
|
||||
if not org_club_promo_only:
|
||||
failed.append(
|
||||
{
|
||||
"id": ex_id,
|
||||
"detail": "Keine Berechtigung (Ersteller, Plattform-Admin oder Vereinsorga bei reiner Vereinsfreigabe).",
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
if patch_visibility:
|
||||
if next_vis == "club":
|
||||
if next_club is None:
|
||||
next_club = tenant.effective_club_id
|
||||
if next_club is None:
|
||||
failed.append(
|
||||
{
|
||||
"id": ex_id,
|
||||
"detail": "Vereins-Übung: club_id angeben oder aktiven Verein wählen (X-Active-Club-Id).",
|
||||
}
|
||||
)
|
||||
continue
|
||||
gov_club = next_club if next_vis == "club" else None
|
||||
try:
|
||||
assert_valid_governance_visibility(cur, profile_id, role, next_vis, gov_club)
|
||||
|
||||
@@ -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