feat: Implement widget-feature assignment management in admin dashboard
Deploy Development / deploy (push) Successful in 53s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 15s

- Added new API endpoints for listing and updating widget-feature assignments, allowing for custom feature requirements.
- Introduced a new admin page for managing widget-feature assignments, enhancing the admin interface.
- Updated navigation to include a link to the new widget-feature assignments page.
- Refactored widget access logic to support AND-based feature requirements for widgets.
- Bumped app_dashboard version to 1.11.0 to reflect these changes and improvements.
This commit is contained in:
Lars committed 2026-04-08 12:26:28 +02:00
1 parent 365ce49c6a
commit 24daeeb83c
10 files changed
+498 -13

No files matched your search

+80 -1
View File
@@ -14,8 +14,14 @@ from fastapi import APIRouter, HTTPException, Depends
from db import get_db, get_cursor, r2d
from auth import require_admin, hash_pin
from models import AdminProfileUpdate
from dashboard_layout_schema import DashboardLayoutPayload, product_default_layout_dict
from dashboard_layout_schema import ALLOWED_WIDGET_IDS, DashboardLayoutPayload, product_default_layout_dict
from dashboard_widget_entitlements import widgets_catalog_admin_payload
from widget_catalog import WIDGET_CATALOG
from widget_feature_requirements_db import (
clear_custom_requirements,
fetch_assignments_bundle,
set_custom_requirements,
)
from system_dashboard_product_default import (
delete_product_default_override,
get_product_default_base_dict,
@@ -213,3 +219,76 @@ def admin_delete_dashboard_product_default(session: dict = Depends(require_admin
delete_product_default_override(conn)
layout = get_product_default_base_dict(conn)
return {"ok": True, "layout": layout, "from_database": False}
@router.get("/widget-feature-assignments")
def admin_list_widget_feature_assignments(session: dict = Depends(require_admin)):
"""Alle Katalog-Widgets mit Custom-/Katalog-Feature-Listen zur Admin-Pflege."""
_ = session
with get_db() as conn:
custom_ids, by_w = fetch_assignments_bundle(conn)
cur = get_cursor(conn)
cur.execute(
"""
SELECT id, name, category, active
FROM features
ORDER BY category NULLS LAST, name
"""
)
features = [r2d(r) for r in cur.fetchall()]
widgets_out: list[dict[str, Any]] = []
for e in WIDGET_CATALOG:
wid = e["id"]
cf = e.get("requires_feature")
code_fids = [cf] if cf else []
uses_custom = wid in custom_ids
widgets_out.append(
{
"id": wid,
"title": e["title"],
"description": e["description"],
"uses_custom_requirements": uses_custom,
"feature_ids": list(by_w.get(wid, [])) if uses_custom else [],
"catalog_feature_ids": code_fids,
}
)
return {"widgets": widgets_out, "features": features}
@router.put("/widget-feature-assignments/{widget_id}")
def admin_put_widget_feature_assignment(
widget_id: str,
body: dict[str, Any],
session: dict = Depends(require_admin),
):
"""
mode=catalog: Katalog-Fallback (requires_feature im Code).
mode=custom: AND über feature_ids (leere Liste = Widget ohne Feature-Gate).
"""
_ = session
if widget_id not in ALLOWED_WIDGET_IDS:
raise HTTPException(404, "Unbekanntes Widget")
mode = body.get("mode", "custom")
if mode == "catalog":
with get_db() as conn:
clear_custom_requirements(conn, widget_id)
return {"ok": True, "widget_id": widget_id, "uses_custom_requirements": False, "feature_ids": []}
if mode != "custom":
raise HTTPException(422, "mode muss catalog oder custom sein")
raw_ids = body.get("feature_ids")
if not isinstance(raw_ids, list):
raise HTTPException(422, "feature_ids muss Liste sein")
cleaned = [str(x).strip() for x in raw_ids if x is not None and str(x).strip()]
with get_db() as conn:
cur = get_cursor(conn)
for fid in cleaned:
cur.execute("SELECT id FROM features WHERE id = %s", (fid,))
if not cur.fetchone():
raise HTTPException(422, f"Unbekanntes Feature: {fid}")
set_custom_requirements(conn, widget_id, cleaned)
return {
"ok": True,
"widget_id": widget_id,
"uses_custom_requirements": True,
"feature_ids": cleaned,
}