feat: enhance media library and lifecycle management
Deploy Development / deploy (push) Successful in 36s
Test Suite / pytest-backend (push) Successful in 25s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 23s
Deploy Development / deploy (push) Successful in 36s
Test Suite / pytest-backend (push) Successful in 25s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 23s
- Updated media library to include lifecycle filtering options (active, trash_soft, trash_hidden) and copyright management capabilities. - Implemented new API endpoints for listing media assets with lifecycle states and patching copyright notices. - Enhanced frontend components to support navigation to the media library and integration of media management features in the ExerciseFormPage. - Incremented version to 0.8.48, reflecting the latest improvements in media handling and governance.
This commit is contained in:
1 parent
95f5b0b2d7
commit
0a1816e38b
11 files changed
+571
-21
No files matched your search
@@ -5,7 +5,7 @@ from typing import Any, Literal, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from club_tenancy import is_platform_admin, library_content_visible_to_profile
|
||||
from db import get_db, get_cursor, r2d
|
||||
@@ -20,6 +20,26 @@ class MediaLifecycleBody(BaseModel):
|
||||
action: Literal["trash_soft", "trash_hidden", "recover", "purge", "reactivate"]
|
||||
|
||||
|
||||
class MediaAssetPatch(BaseModel):
|
||||
copyright_notice: Optional[str] = Field(None, max_length=8000)
|
||||
|
||||
|
||||
_LIFECYCLE_LIST_FILTERS = frozenset({"active", "trash_soft", "trash_hidden", "all"})
|
||||
|
||||
|
||||
def _lifecycle_where_sql(lifecycle: str) -> str:
|
||||
lc = (lifecycle or "active").strip().lower()
|
||||
if lc not in _LIFECYCLE_LIST_FILTERS:
|
||||
raise HTTPException(status_code=400, detail="Ungültiger lifecycle-Filter")
|
||||
if lc == "active":
|
||||
return "ma.lifecycle_state = 'active'"
|
||||
if lc == "trash_soft":
|
||||
return "ma.lifecycle_state = 'trash_soft'"
|
||||
if lc == "trash_hidden":
|
||||
return "ma.lifecycle_state = 'trash_hidden'"
|
||||
return "ma.lifecycle_state IN ('active', 'trash_soft', 'trash_hidden')"
|
||||
|
||||
|
||||
def _fetch_asset_file_row(cur: Any, asset_id: int) -> Optional[dict]:
|
||||
cur.execute(
|
||||
"""SELECT id, visibility, club_id, uploaded_by_profile_id, lifecycle_state,
|
||||
@@ -47,12 +67,18 @@ def _assert_can_view_archive_asset(cur: Any, tenant: TenantContext, asset: dict)
|
||||
def list_media_assets(
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
q: Optional[str] = Query(None, max_length=120),
|
||||
lifecycle: str = Query(
|
||||
"active",
|
||||
description="active | trash_soft | trash_hidden | all (nicht purgierte Zustände)",
|
||||
),
|
||||
limit: int = Query(30, ge=1, le=100),
|
||||
offset: int = Query(0, ge=0),
|
||||
):
|
||||
"""
|
||||
Durchsuchbares Medien-Archiv: nur aktive Assets, Sichtbarkeit wie Übungsbibliothek.
|
||||
Durchsuchbares Medien-Archiv; Sichtbarkeit wie Übungsbibliothek.
|
||||
Standard lifecycle=active (Archiv-Picker); Manager-UI kann Papierkorb-Ansicht wählen.
|
||||
"""
|
||||
lc_where = _lifecycle_where_sql(lifecycle)
|
||||
role = tenant.global_role or ""
|
||||
is_adm = is_platform_admin(role)
|
||||
profile_id = tenant.profile_id
|
||||
@@ -69,9 +95,10 @@ def list_media_assets(
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
f"""SELECT ma.id, ma.mime_type, ma.byte_size, ma.original_filename, ma.visibility, ma.club_id,
|
||||
ma.uploaded_by_profile_id, ma.lifecycle_state, ma.created_at, ma.sha256
|
||||
ma.uploaded_by_profile_id, ma.lifecycle_state, ma.created_at, ma.sha256,
|
||||
ma.copyright_notice
|
||||
FROM media_assets ma
|
||||
WHERE ma.lifecycle_state = 'active'
|
||||
WHERE {lc_where}
|
||||
AND (
|
||||
%s
|
||||
OR lower(trim(ma.visibility)) = 'official'
|
||||
@@ -90,12 +117,12 @@ def list_media_assets(
|
||||
)
|
||||
)
|
||||
{search_sql}
|
||||
ORDER BY ma.created_at DESC
|
||||
ORDER BY ma.updated_at DESC NULLS LAST, ma.created_at DESC
|
||||
LIMIT %s OFFSET %s""",
|
||||
params,
|
||||
)
|
||||
rows = [r2d(r) for r in cur.fetchall()]
|
||||
return {"items": rows, "limit": limit, "offset": offset}
|
||||
return {"items": rows, "limit": limit, "offset": offset, "lifecycle": lifecycle.strip().lower()}
|
||||
|
||||
|
||||
@router.api_route("/{asset_id}/file", methods=["GET", "HEAD"])
|
||||
@@ -113,9 +140,14 @@ def download_media_asset_file(
|
||||
if not asset:
|
||||
raise HTTPException(status_code=404, detail="Medium nicht gefunden")
|
||||
lc = (asset.get("lifecycle_state") or "").strip().lower()
|
||||
if lc != "active":
|
||||
if lc == "active":
|
||||
_assert_can_view_archive_asset(cur, tenant, asset)
|
||||
elif lc in ("trash_soft", "trash_hidden"):
|
||||
from media_lifecycle import assert_can_manage_media_asset_lifecycle
|
||||
|
||||
assert_can_manage_media_asset_lifecycle(cur, tenant, asset)
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail="Medium nicht verfügbar")
|
||||
_assert_can_view_archive_asset(cur, tenant, asset)
|
||||
|
||||
sk = asset.get("storage_key")
|
||||
if not sk:
|
||||
@@ -177,3 +209,47 @@ def post_media_asset_lifecycle(
|
||||
return reactivate_media_asset_from_trash(cur, conn, asset_id)
|
||||
|
||||
raise HTTPException(status_code=500, detail="Interner Fehler: lifecycle action")
|
||||
|
||||
|
||||
@router.patch("/{asset_id}")
|
||||
def patch_media_asset(
|
||||
asset_id: int,
|
||||
body: MediaAssetPatch,
|
||||
tenant: TenantContext = Depends(get_tenant_context),
|
||||
):
|
||||
"""Metadaten (z. B. Copyright) — gleiche Berechtigung wie Lifecycle-Verwaltung."""
|
||||
from media_lifecycle import assert_can_manage_media_asset_lifecycle
|
||||
|
||||
data = body.dict(exclude_unset=True)
|
||||
if not data:
|
||||
raise HTTPException(status_code=400, detail="Keine Felder zum Aktualisieren")
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"""SELECT id, visibility, club_id, uploaded_by_profile_id, lifecycle_state,
|
||||
copyright_notice, original_filename
|
||||
FROM media_assets WHERE id = %s""",
|
||||
(asset_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="Medium nicht gefunden")
|
||||
asset = r2d(row)
|
||||
assert_can_manage_media_asset_lifecycle(cur, tenant, asset)
|
||||
|
||||
if "copyright_notice" in data:
|
||||
cn = data["copyright_notice"]
|
||||
cur.execute(
|
||||
"UPDATE media_assets SET copyright_notice = %s, updated_at = NOW() WHERE id = %s",
|
||||
(cn, asset_id),
|
||||
)
|
||||
conn.commit()
|
||||
cur.execute(
|
||||
"""SELECT id, mime_type, byte_size, original_filename, visibility, club_id,
|
||||
uploaded_by_profile_id, lifecycle_state, created_at, sha256, copyright_notice
|
||||
FROM media_assets WHERE id = %s""",
|
||||
(asset_id,),
|
||||
)
|
||||
out = r2d(cur.fetchone())
|
||||
return out
|
||||
Reference in new issue
Block a user