feat: implement bulk metadata update for exercises
- Introduced a new PATCH endpoint `/api/exercises/bulk-metadata` to allow bulk updates of visibility and status for exercises, supporting up to 500 IDs. - Enhanced the ExercisesListPage to include a bulk update modal for managing exercise visibility and status. - Updated frontend API utility to handle bulk patch requests. - Bumped application version to 0.8.29 and updated changelog to reflect these changes.
This commit is contained in:
1 parent
e0ecfe927f
commit
dc310b38eb
6 files changed
+473
-7
No files matched your search
@@ -9,7 +9,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends, Query, UploadFile, File, Form
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
@@ -213,6 +213,25 @@ class ExerciseVariantsReorder(BaseModel):
|
||||
variant_ids: list[int]
|
||||
|
||||
|
||||
_VALID_EXERCISE_STATUS_BULK = frozenset({"draft", "in_review", "approved", "archived"})
|
||||
_MAX_BULK_METADATA_IDS = 500
|
||||
|
||||
|
||||
class ExerciseBulkMetadataPatch(BaseModel):
|
||||
"""Massenänderung von Sichtbarkeit und/oder Status (z. B. Private → Verein)."""
|
||||
|
||||
exercise_ids: list[int] = Field(..., min_length=1, max_length=_MAX_BULK_METADATA_IDS)
|
||||
visibility: Optional[str] = Field(None, pattern="^(private|club|official)$")
|
||||
status: Optional[str] = None
|
||||
club_id: Optional[int] = Field(default=None, ge=1)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def at_least_visibility_or_status(self):
|
||||
if self.visibility is None and self.status is None:
|
||||
raise ValueError("Mindestens eines der Felder visibility oder status angeben")
|
||||
return self
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Helper Functions
|
||||
# ============================================================================
|
||||
@@ -552,6 +571,127 @@ def _merge_str_any(multi: list[str], single: Optional[str]) -> list[str]:
|
||||
return out
|
||||
|
||||
|
||||
@router.patch("/exercises/bulk-metadata")
|
||||
def bulk_patch_exercises_metadata(
|
||||
body: ExerciseBulkMetadataPatch,
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""
|
||||
Ändert Sichtbarkeit und/oder Status für viele Übungen auf einmal.
|
||||
Berechtigt: Ersteller der jeweiligen Übung oder Plattform-Admin (admin/superadmin).
|
||||
Governance wie bei Einzel-PUT (official nur Plattform-Admin; club mit Mitgliedschaft bzw. Admin).
|
||||
"""
|
||||
profile_id = tenant.profile_id
|
||||
role = tenant.global_role
|
||||
|
||||
unique_ids = sorted({int(x) for x in body.exercise_ids if x is not None and int(x) > 0})
|
||||
if not unique_ids:
|
||||
raise HTTPException(status_code=400, detail="Keine gültigen Übungs-IDs")
|
||||
if len(unique_ids) > _MAX_BULK_METADATA_IDS:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Maximal {_MAX_BULK_METADATA_IDS} Übungen pro Anfrage",
|
||||
)
|
||||
|
||||
status_val: Optional[str] = None
|
||||
if body.status is not None:
|
||||
st = str(body.status).strip().lower()
|
||||
if st not in _VALID_EXERCISE_STATUS_BULK:
|
||||
raise HTTPException(status_code=400, detail="Ungültiger Status")
|
||||
status_val = st
|
||||
|
||||
patch_visibility = body.visibility is not None
|
||||
patch_status = status_val is not None
|
||||
|
||||
updated: List[int] = []
|
||||
failed: List[Dict[str, Any]] = []
|
||||
|
||||
def _fail_msg(he: HTTPException) -> str:
|
||||
d = he.detail
|
||||
return d if isinstance(d, str) else str(d)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
for ex_id in unique_ids:
|
||||
cur.execute(
|
||||
"SELECT id, created_by, visibility, club_id FROM exercises WHERE id = %s",
|
||||
(ex_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
failed.append({"id": ex_id, "detail": "Übung nicht gefunden"})
|
||||
continue
|
||||
rowd = r2d(row)
|
||||
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")
|
||||
ex_cid = int(ex_cid_raw) if ex_cid_raw is not None else None
|
||||
|
||||
next_vis = ex_vis
|
||||
if patch_visibility:
|
||||
next_vis = str(body.visibility).strip().lower()
|
||||
|
||||
next_club = ex_cid
|
||||
if patch_visibility and body.club_id is not None:
|
||||
next_club = int(body.club_id)
|
||||
|
||||
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)
|
||||
except HTTPException as he:
|
||||
failed.append({"id": ex_id, "detail": _fail_msg(he)})
|
||||
continue
|
||||
|
||||
sets: List[str] = []
|
||||
vals: List[Any] = []
|
||||
if patch_visibility:
|
||||
sets.extend(["visibility = %s", "club_id = %s"])
|
||||
cid_out = next_club if next_vis == "club" else None
|
||||
vals.extend([next_vis, cid_out])
|
||||
if patch_status:
|
||||
sets.append("status = %s")
|
||||
vals.append(status_val)
|
||||
|
||||
sets.append("updated_at = NOW()")
|
||||
vals.append(ex_id)
|
||||
cur.execute(
|
||||
f"UPDATE exercises SET {', '.join(sets)} WHERE id = %s",
|
||||
tuple(vals),
|
||||
)
|
||||
updated.append(ex_id)
|
||||
conn.commit()
|
||||
|
||||
return {
|
||||
"updated": updated,
|
||||
"failed": failed,
|
||||
"updated_count": len(updated),
|
||||
"failed_count": len(failed),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/exercises")
|
||||
def list_exercises(
|
||||
focus_area_ids: list[int] = Query(default=[], description="ODER: mind. einer dieser Fokusbereiche"),
|
||||
|
||||
+10
-2
@@ -1,6 +1,6 @@
|
||||
# Shinkan Jinkendo Version Information
|
||||
|
||||
APP_VERSION = "0.8.28"
|
||||
APP_VERSION = "0.8.29"
|
||||
BUILD_DATE = "2026-05-05"
|
||||
DB_SCHEMA_VERSION = "20260505041"
|
||||
|
||||
@@ -15,7 +15,7 @@ MODULE_VERSIONS = {
|
||||
"groups": "0.1.0",
|
||||
"skills": "0.1.0",
|
||||
"methods": "0.1.0",
|
||||
"exercises": "2.6.4", # Detail-Lesen über library_content_visible_to_profile
|
||||
"exercises": "2.7.0", # PATCH /exercises/bulk-metadata — Massenänderung Sichtbarkeit/Status
|
||||
"training_units": "0.1.0",
|
||||
"training_programs": "0.1.0",
|
||||
"planning": "0.8.1", # Vorlagen Leseprüfung library_content_visible_to_profile
|
||||
@@ -27,6 +27,14 @@ MODULE_VERSIONS = {
|
||||
}
|
||||
|
||||
CHANGELOG = [
|
||||
{
|
||||
"version": "0.8.29",
|
||||
"date": "2026-05-05",
|
||||
"changes": [
|
||||
"Übungen: PATCH /api/exercises/bulk-metadata (bis 500 IDs) — Sichtbarkeit und/oder Status; Ersteller oder Plattform-Admin; Governance wie Einzel-PUT",
|
||||
"Übungsliste: Mehrfachauswahl, Alle auf dieser Seite, Dialog Massenänderung (Verein/offiziell nur Admins)",
|
||||
],
|
||||
},
|
||||
{
|
||||
"version": "0.8.28",
|
||||
"date": "2026-05-05",
|
||||
|
||||
Reference in new issue
Block a user