feat: enhance media asset management and exercise integration
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Successful in 23s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 6s
Test Suite / playwright-tests (push) Successful in 23s
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Successful in 23s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 6s
Test Suite / playwright-tests (push) Successful in 23s
- Added new API endpoints for listing media assets and attaching existing archive media to exercises, improving media reuse and governance. - Updated frontend components to support media asset selection from the archive, enhancing user experience and reducing duplication. - Incremented version to 0.8.43, reflecting the latest changes in media handling and exercise integration.
This commit is contained in:
1 parent
8ac723eafe
commit
631ba1cb43
7 files changed
+610
-20
No files matched your search
@@ -209,6 +209,17 @@ class ExerciseMediaReorder(BaseModel):
|
||||
media_ids: list[int]
|
||||
|
||||
|
||||
class ExerciseMediaFromAsset(BaseModel):
|
||||
"""Bestehendes Archiv-Medium (media_assets) mit Übung verknüpfen — ohne erneuten Upload."""
|
||||
|
||||
media_asset_id: int = Field(..., ge=1)
|
||||
title: Optional[str] = ""
|
||||
description: Optional[str] = ""
|
||||
context: str = "ablauf"
|
||||
is_primary: bool = False
|
||||
media_type: Optional[str] = None
|
||||
|
||||
|
||||
class ExerciseVariantCreate(BaseModel):
|
||||
variant_name: str = Field(..., min_length=3, max_length=200)
|
||||
description: Optional[str] = None
|
||||
@@ -302,6 +313,17 @@ def _detect_embed_platform(url: str) -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
def _media_type_from_mime(mime: Optional[str]) -> str:
|
||||
m = (mime or "").strip().lower()
|
||||
if m.startswith("image/"):
|
||||
return "image"
|
||||
if m.startswith("video/"):
|
||||
return "video"
|
||||
if m == "application/pdf":
|
||||
return "document"
|
||||
return "document"
|
||||
|
||||
|
||||
def _fetch_exercise_governance_row(cur, exercise_id: int) -> Optional[dict]:
|
||||
cur.execute(
|
||||
"SELECT id, visibility, club_id, created_by FROM exercises WHERE id = %s",
|
||||
@@ -2222,6 +2244,115 @@ async def upload_exercise_media(
|
||||
return r2d(row)
|
||||
|
||||
|
||||
@router.post("/exercises/{exercise_id}/media/from-asset", status_code=201)
|
||||
def attach_exercise_media_from_asset(
|
||||
exercise_id: int,
|
||||
body: ExerciseMediaFromAsset,
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""Archiv-Medium an Übung anhängen (Wiederverwendung, deduplizierte Datei)."""
|
||||
profile_id = tenant.profile_id
|
||||
ctx = (body.context or "ablauf").strip()
|
||||
if ctx not in ("ablauf", "detail", "trainer_hint"):
|
||||
raise HTTPException(status_code=400, detail="Ungültiger context")
|
||||
|
||||
mt_in = body.media_type
|
||||
if mt_in is not None and mt_in not in ("image", "video", "document", "sketch"):
|
||||
raise HTTPException(status_code=400, detail="Ungültiger media_type")
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
_assert_can_edit_exercise(cur, exercise_id, tenant)
|
||||
|
||||
if _count_exercise_media(cur, exercise_id) >= MAX_EXERCISE_MEDIA:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Maximal {MAX_EXERCISE_MEDIA} Medien pro Übung",
|
||||
)
|
||||
|
||||
cur.execute(
|
||||
"""SELECT id, mime_type, byte_size, original_filename, visibility, club_id,
|
||||
uploaded_by_profile_id, storage_key, lifecycle_state
|
||||
FROM media_assets WHERE id = %s""",
|
||||
(body.media_asset_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="Archiv-Medium nicht gefunden")
|
||||
asset = r2d(row)
|
||||
if (asset.get("lifecycle_state") or "").strip().lower() != "active":
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Nur aktive Archiv-Medien können verknüpft werden",
|
||||
)
|
||||
|
||||
if not library_content_visible_to_profile(
|
||||
cur,
|
||||
profile_id,
|
||||
(asset.get("visibility") or "").strip().lower(),
|
||||
asset.get("club_id"),
|
||||
asset.get("uploaded_by_profile_id"),
|
||||
tenant.global_role,
|
||||
):
|
||||
raise HTTPException(status_code=403, detail="Keine Berechtigung für dieses Archiv-Medium")
|
||||
|
||||
cur.execute(
|
||||
"SELECT 1 FROM exercise_media WHERE exercise_id = %s AND media_asset_id = %s",
|
||||
(exercise_id, body.media_asset_id),
|
||||
)
|
||||
if cur.fetchone():
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Dieses Archiv-Medium ist bereits mit der Übung verknüpft",
|
||||
)
|
||||
|
||||
sk = asset.get("storage_key")
|
||||
if not sk:
|
||||
raise HTTPException(status_code=400, detail="Archiv-Eintrag hat keine Datei")
|
||||
|
||||
media_type = mt_in or _media_type_from_mime(asset.get("mime_type"))
|
||||
title = (body.title or "").strip() or None
|
||||
if title is None and asset.get("original_filename"):
|
||||
title = str(asset.get("original_filename") or "")[:300] or None
|
||||
description = (body.description or "").strip() or None
|
||||
db_path = f"/media/{sk}"
|
||||
|
||||
sort_sql = (
|
||||
"COALESCE((SELECT MAX(sort_order) + 1 FROM exercise_media WHERE exercise_id = %s), 1)"
|
||||
)
|
||||
cur.execute(
|
||||
f"""INSERT INTO exercise_media (
|
||||
exercise_id, media_type, file_path, file_size, mime_type, original_filename,
|
||||
embed_url, embed_platform, title, description, context, is_primary, sort_order,
|
||||
media_asset_id
|
||||
) VALUES (
|
||||
%s, %s, %s, %s, %s, %s, NULL, NULL, %s, %s, %s, %s, {sort_sql}, %s
|
||||
)
|
||||
RETURNING id, exercise_id, media_type, file_path, file_size, mime_type, original_filename,
|
||||
embed_url, embed_platform, title, description, sort_order, is_primary, context, created_at,
|
||||
media_asset_id""",
|
||||
(
|
||||
exercise_id,
|
||||
media_type,
|
||||
db_path,
|
||||
asset.get("byte_size"),
|
||||
asset.get("mime_type"),
|
||||
asset.get("original_filename"),
|
||||
title,
|
||||
description,
|
||||
ctx,
|
||||
body.is_primary,
|
||||
exercise_id,
|
||||
body.media_asset_id,
|
||||
),
|
||||
)
|
||||
out = r2d(cur.fetchone())
|
||||
conn.commit()
|
||||
|
||||
out["asset_lifecycle_state"] = "active"
|
||||
return out
|
||||
|
||||
|
||||
@router.put("/exercises/{exercise_id}/media/reorder")
|
||||
def reorder_exercise_media(
|
||||
exercise_id: int,
|
||||
|
||||
Reference in new issue
Block a user