feat: enhance tenant context integration and update access layer endpoints
- Implemented `library_content_visibility_sql` for managing visibility of exercises, training planning, and framework programs based on tenant context. - Updated access layer documentation to reflect changes in endpoint visibility and governance requirements. - Bumped application version to 0.8.23 in both backend and frontend files. - Enhanced changelog to document the new version and changes made in this release.
This commit is contained in:
1 parent
4b6fd49940
commit
c919e02441
7 files changed
+186
-122
No files matched your search
@@ -9,7 +9,7 @@ from typing import Any, Dict, List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
from tenant_context import TenantContext, get_tenant_context, library_content_visibility_sql
|
||||
from club_tenancy import (
|
||||
assert_valid_governance_visibility,
|
||||
exercise_visible_to_profile,
|
||||
@@ -560,9 +560,9 @@ def _template_access(cur, tid: int, profile_id: int, role: str) -> Dict[str, Any
|
||||
|
||||
|
||||
@router.get("/training-plan-templates")
|
||||
def list_training_plan_templates(session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def list_training_plan_templates(tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
if is_platform_admin(role):
|
||||
@@ -576,35 +576,30 @@ def list_training_plan_templates(session=Depends(require_auth)):
|
||||
"""
|
||||
)
|
||||
else:
|
||||
vis_clause, vis_params = library_content_visibility_sql(
|
||||
alias="t",
|
||||
profile_id=profile_id,
|
||||
role=role,
|
||||
effective_club_id=tenant.effective_club_id,
|
||||
)
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT t.*,
|
||||
(SELECT COUNT(*) FROM training_plan_template_sections s WHERE s.template_id = t.id)
|
||||
AS sections_count
|
||||
FROM training_plan_templates t
|
||||
WHERE (
|
||||
t.visibility = 'official'
|
||||
OR (t.visibility = 'private' AND t.created_by = %s)
|
||||
OR (
|
||||
t.visibility = 'club'
|
||||
AND t.club_id IS NOT NULL
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM club_members cm
|
||||
WHERE cm.profile_id = %s AND cm.club_id = t.club_id AND cm.status = 'active'
|
||||
)
|
||||
)
|
||||
)
|
||||
WHERE ({vis_clause})
|
||||
ORDER BY t.updated_at DESC NULLS LAST, t.name
|
||||
""",
|
||||
(profile_id, profile_id),
|
||||
vis_params,
|
||||
)
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
|
||||
|
||||
@router.get("/training-plan-templates/{template_id}")
|
||||
def get_training_plan_template(template_id: int, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def get_training_plan_template(template_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
row = _template_access(cur, template_id, profile_id, role)
|
||||
@@ -622,9 +617,9 @@ def get_training_plan_template(template_id: int, session=Depends(require_auth)):
|
||||
|
||||
|
||||
@router.post("/training-plan-templates")
|
||||
def create_training_plan_template(data: dict, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def create_training_plan_template(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
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 Vorlagen anlegen")
|
||||
name = (data.get("name") or "").strip()
|
||||
@@ -635,6 +630,8 @@ def create_training_plan_template(data: dict, session=Depends(require_auth)):
|
||||
club_id = data.get("club_id")
|
||||
if club_id in ("", []):
|
||||
club_id = None
|
||||
if visibility == "club" and club_id is None:
|
||||
club_id = tenant.effective_club_id
|
||||
sections_in = data.get("sections") or []
|
||||
|
||||
with get_db() as conn:
|
||||
@@ -662,13 +659,13 @@ def create_training_plan_template(data: dict, session=Depends(require_auth)):
|
||||
(tid, order_ix, title, sec.get("guidance_text")),
|
||||
)
|
||||
conn.commit()
|
||||
return get_training_plan_template(tid, session)
|
||||
return get_training_plan_template(tid, tenant)
|
||||
|
||||
|
||||
@router.put("/training-plan-templates/{template_id}")
|
||||
def update_training_plan_template(template_id: int, data: dict, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def update_training_plan_template(template_id: int, data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
row_prev = _fetch_training_plan_template_row(cur, template_id)
|
||||
@@ -731,13 +728,13 @@ def update_training_plan_template(template_id: int, data: dict, session=Depends(
|
||||
(template_id, order_ix, title, sec.get("guidance_text")),
|
||||
)
|
||||
conn.commit()
|
||||
return get_training_plan_template(template_id, session)
|
||||
return get_training_plan_template(template_id, tenant)
|
||||
|
||||
|
||||
@router.delete("/training-plan-templates/{template_id}")
|
||||
def delete_training_plan_template(template_id: int, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def delete_training_plan_template(template_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
row_del = _fetch_training_plan_template_row(cur, template_id)
|
||||
@@ -760,10 +757,10 @@ def list_training_units(
|
||||
assigned_to_me: bool = Query(default=False),
|
||||
sort: str = Query(default="desc"),
|
||||
limit: Optional[int] = Query(default=None),
|
||||
session=Depends(require_auth),
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
gid = _optional_positive_int(group_id, "group_id") if group_id else None
|
||||
cid = _optional_positive_int(club_id, "club_id") if club_id else None
|
||||
@@ -873,9 +870,9 @@ def list_training_units(
|
||||
|
||||
|
||||
@router.get("/training-units/{unit_id}")
|
||||
def get_training_unit(unit_id: int, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def get_training_unit(unit_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -937,9 +934,9 @@ def get_training_unit(unit_id: int, session=Depends(require_auth)):
|
||||
|
||||
|
||||
@router.post("/training-units")
|
||||
def create_training_unit(data: dict, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def create_training_unit(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
group_id = data.get("group_id")
|
||||
planned_date = data.get("planned_date")
|
||||
@@ -996,13 +993,13 @@ def create_training_unit(data: dict, session=Depends(require_auth)):
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(unit_id, session)
|
||||
return get_training_unit(unit_id, tenant)
|
||||
|
||||
|
||||
@router.put("/training-units/{unit_id}")
|
||||
def update_training_unit(unit_id: int, data: dict, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def update_training_unit(unit_id: int, data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -1143,13 +1140,13 @@ def update_training_unit(unit_id: int, data: dict, session=Depends(require_auth)
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(unit_id, session)
|
||||
return get_training_unit(unit_id, tenant)
|
||||
|
||||
|
||||
@router.delete("/training-units/{unit_id}")
|
||||
def delete_training_unit(unit_id: int, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
def delete_training_unit(unit_id: int, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
@@ -1179,10 +1176,10 @@ def delete_training_unit(unit_id: int, session=Depends(require_auth)):
|
||||
|
||||
|
||||
@router.post("/training-units/from-framework-slot")
|
||||
def create_training_unit_from_framework_slot(data: dict, session=Depends(require_auth)):
|
||||
def create_training_unit_from_framework_slot(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
"""Geplante Einheit aus Rahmen-Slot-Blueprint kopieren (Lineage über origin_framework_slot_id)."""
|
||||
profile_id = session["profile_id"]
|
||||
role = session.get("role")
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
if not _has_planning_role(role):
|
||||
raise HTTPException(status_code=403, detail="Nur Planungsberechtigte dürfen Trainingseinheiten erstellen")
|
||||
@@ -1243,12 +1240,12 @@ def create_training_unit_from_framework_slot(data: dict, session=Depends(require
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(new_id, session)
|
||||
return get_training_unit(new_id, tenant)
|
||||
|
||||
|
||||
@router.post("/training-units/quick-create")
|
||||
def quick_create_training_unit(data: dict, session=Depends(require_auth)):
|
||||
profile_id = session["profile_id"]
|
||||
def quick_create_training_unit(data: dict, tenant: TenantContext = Depends(get_tenant_context)):
|
||||
profile_id = tenant.profile_id
|
||||
|
||||
group_id = data.get("group_id")
|
||||
planned_date = data.get("planned_date")
|
||||
@@ -1274,7 +1271,7 @@ def quick_create_training_unit(data: dict, session=Depends(require_auth)):
|
||||
if not group:
|
||||
raise HTTPException(status_code=404, detail="Trainingsgruppe nicht gefunden")
|
||||
|
||||
role = session.get("role")
|
||||
role = tenant.global_role
|
||||
co_trainers = group["co_trainer_ids"] or []
|
||||
|
||||
if not _has_planning_role(role):
|
||||
@@ -1316,5 +1313,5 @@ def quick_create_training_unit(data: dict, session=Depends(require_auth)):
|
||||
|
||||
conn.commit()
|
||||
|
||||
return get_training_unit(unit_id, session)
|
||||
return get_training_unit(unit_id, tenant)
|
||||
|
||||
Reference in new issue
Block a user