feat: add club visibility queue for training exercises
Deploy Development / deploy (push) Successful in 34s
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 24s
Deploy Development / deploy (push) Successful in 34s
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 24s
- Implemented a new API endpoint to retrieve exercises in training units that are not yet visible to the target club, enhancing visibility management for trainers. - Introduced a new widget in the Dashboard to display these exercises, improving user access to relevant training information. - Added helper functions to determine exercise visibility based on club membership and status, streamlining the exercise promotion process.
This commit is contained in:
1 parent
f9d518fb78
commit
fe703ca414
4 files changed
+541
No files matched your search
@@ -4,6 +4,7 @@ und wiederverwendbare Trainingsvorlagen (Sektions-Gliederung).
|
||||
|
||||
Governance: Sichtbarkeit wie Übungen (private / club / official); Schreiben nur Ersteller oder Plattform-Admin.
|
||||
"""
|
||||
from datetime import date, timedelta
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
@@ -692,6 +693,26 @@ def _group_club_id_for_scheduled_unit(cur, unit_id: int) -> Optional[int]:
|
||||
return int(r["club_id"])
|
||||
|
||||
|
||||
def _exercise_needs_club_visibility_for_target(ex: Dict[str, Any], target_club_id: int) -> bool:
|
||||
"""Übung für Mitglieder des Ziel-Vereins in der Durchführung sichtbar machen (Dashboard/Queue)."""
|
||||
if str(ex.get("status") or "").strip().lower() == "archived":
|
||||
return False
|
||||
vis = (ex.get("visibility") or "private").strip().lower()
|
||||
if vis == "official":
|
||||
return False
|
||||
if vis == "private":
|
||||
return True
|
||||
if vis == "club":
|
||||
raw = ex.get("club_id")
|
||||
if raw is None:
|
||||
return True
|
||||
try:
|
||||
return int(raw) != int(target_club_id)
|
||||
except (TypeError, ValueError):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _caller_may_promote_exercise_to_club(
|
||||
cur,
|
||||
exercise_created_by: Optional[int],
|
||||
@@ -1209,6 +1230,163 @@ def list_training_units(
|
||||
return [r2d(r) for r in rows]
|
||||
|
||||
|
||||
@router.get("/training-units/exercises-club-visibility-queue")
|
||||
def exercises_club_visibility_queue(
|
||||
start_date: Optional[str] = Query(default=None),
|
||||
end_date: Optional[str] = Query(default=None),
|
||||
assigned_to_me: bool = Query(default=True),
|
||||
limit_units: int = Query(default=80, ge=1, le=150),
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""
|
||||
Übungen in deinen Trainingseinheiten (Zeitfenster), die für den jeweiligen Verein der Gruppe
|
||||
noch nicht vereinsweit sichtbar sind — für Dashboard & Freigabe-Workflow.
|
||||
"""
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
if start_date is None:
|
||||
start_date = (date.today() - timedelta(days=45)).isoformat()
|
||||
if end_date is None:
|
||||
end_date = (date.today() + timedelta(days=365)).isoformat()
|
||||
|
||||
units = list_training_units(
|
||||
group_id=None,
|
||||
club_id=None,
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
status=None,
|
||||
assigned_to_me=assigned_to_me,
|
||||
debrief_pending=False,
|
||||
sort="asc",
|
||||
limit=limit_units,
|
||||
tenant=tenant,
|
||||
)
|
||||
unit_ids = [int(u["id"]) for u in units if u.get("id") is not None]
|
||||
if not unit_ids:
|
||||
return {"items": []}
|
||||
|
||||
placeholders = ",".join(["%s"] * len(unit_ids))
|
||||
items: List[Dict[str, Any]] = []
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT DISTINCT tu.id AS unit_id,
|
||||
tu.planned_date,
|
||||
tg.name AS group_name,
|
||||
tg.club_id AS target_club_id,
|
||||
c.name AS target_club_name,
|
||||
tusi.exercise_id AS exercise_id
|
||||
FROM training_units tu
|
||||
INNER JOIN training_groups tg ON tu.group_id = tg.id
|
||||
LEFT JOIN clubs c ON c.id = tg.club_id
|
||||
INNER JOIN training_unit_sections tus ON tus.training_unit_id = tu.id
|
||||
INNER JOIN training_unit_section_items tusi ON tusi.section_id = tus.id
|
||||
WHERE tu.id IN ({placeholders})
|
||||
AND tu.framework_slot_id IS NULL
|
||||
AND tusi.item_type = 'exercise'
|
||||
AND tusi.exercise_id IS NOT NULL
|
||||
""",
|
||||
tuple(unit_ids),
|
||||
)
|
||||
pairs = [r2d(r) for r in cur.fetchall()]
|
||||
if not pairs:
|
||||
return {"items": []}
|
||||
|
||||
ex_ids = sorted(
|
||||
{int(p["exercise_id"]) for p in pairs if p.get("exercise_id") is not None}
|
||||
)
|
||||
if not ex_ids:
|
||||
return {"items": []}
|
||||
|
||||
exercises_map: Dict[int, Dict[str, Any]] = {}
|
||||
ph = ",".join(["%s"] * len(ex_ids))
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT id, title, visibility, club_id, created_by, status
|
||||
FROM exercises
|
||||
WHERE id IN ({ph})
|
||||
""",
|
||||
tuple(ex_ids),
|
||||
)
|
||||
for r in cur.fetchall():
|
||||
d = r2d(r)
|
||||
exercises_map[int(d["id"])] = d
|
||||
|
||||
agg: Dict[tuple, Dict[str, Any]] = {}
|
||||
for p in pairs:
|
||||
try:
|
||||
ex_id = int(p["exercise_id"])
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
tc_raw = p.get("target_club_id")
|
||||
if tc_raw is None:
|
||||
continue
|
||||
tc = int(tc_raw)
|
||||
key = (ex_id, tc)
|
||||
if key not in agg:
|
||||
agg[key] = {
|
||||
"exercise_id": ex_id,
|
||||
"target_club_id": tc,
|
||||
"target_club_name": (p.get("target_club_name") or "").strip(),
|
||||
"units": [],
|
||||
}
|
||||
uid = p.get("unit_id")
|
||||
if uid is None:
|
||||
continue
|
||||
agg[key]["units"].append(
|
||||
{
|
||||
"id": int(uid),
|
||||
"planned_date": str(p["planned_date"]) if p.get("planned_date") is not None else "",
|
||||
"group_name": (p.get("group_name") or "").strip(),
|
||||
}
|
||||
)
|
||||
|
||||
for _key, blob in agg.items():
|
||||
ex_id = blob["exercise_id"]
|
||||
tc = blob["target_club_id"]
|
||||
ex = exercises_map.get(ex_id)
|
||||
if not ex:
|
||||
continue
|
||||
if not _exercise_needs_club_visibility_for_target(ex, tc):
|
||||
continue
|
||||
uniq_units = {u["id"]: u for u in blob["units"]}.values()
|
||||
ulist = sorted(
|
||||
uniq_units,
|
||||
key=lambda x: (x.get("planned_date") or "", x.get("id")),
|
||||
)
|
||||
cb = ex.get("created_by")
|
||||
cb_int = int(cb) if cb is not None else None
|
||||
can_promote = _caller_may_promote_exercise_to_club(cur, cb_int, profile_id, role, tc)
|
||||
vis = (ex.get("visibility") or "private").strip().lower()
|
||||
st = (ex.get("status") or "draft").strip().lower()
|
||||
ecid = ex.get("club_id")
|
||||
items.append(
|
||||
{
|
||||
"exercise_id": ex_id,
|
||||
"title": (ex.get("title") or f"Übung #{ex_id}").strip() or f"Übung #{ex_id}",
|
||||
"visibility": vis,
|
||||
"status": st,
|
||||
"club_id": int(ecid) if ecid is not None else None,
|
||||
"created_by": cb_int,
|
||||
"target_club_id": tc,
|
||||
"target_club_name": blob.get("target_club_name") or "",
|
||||
"can_promote": can_promote,
|
||||
"units": ulist,
|
||||
}
|
||||
)
|
||||
|
||||
items.sort(
|
||||
key=lambda x: (
|
||||
(x["units"][0].get("planned_date") if x["units"] else ""),
|
||||
x["title"],
|
||||
)
|
||||
)
|
||||
return {"items": items}
|
||||
|
||||
|
||||
@router.get("/training-units/{unit_id}")
|
||||
def get_training_unit(unit_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
|
||||
Reference in new issue
Block a user