feat: Implement focus area usage types management in API and UI
- Added endpoints for listing and updating focus area usage types in the backend. - Enhanced the AdminFocusAreasPage to display and manage allowed usage types for focus areas. - Introduced a new state for usage types catalog and integrated it into the focus area editing process. - Updated API utility functions to support new usage types operations.
This commit is contained in:
1 parent
49e9c9c214
commit
e7dedd527f
6 files changed
+435
-16
No files matched your search
@@ -3,10 +3,11 @@ Focus Areas Router
|
||||
Manages dynamic focus area definitions and user preferences
|
||||
"""
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
from focus_area_usage_helpers import coerce_usage_type_keys
|
||||
|
||||
router = APIRouter(prefix="/api/focus-areas", tags=["focus-areas"])
|
||||
|
||||
@@ -36,6 +37,11 @@ class UserFocusPreferences(BaseModel):
|
||||
"""User's focus area weightings (dynamic)"""
|
||||
preferences: dict # {focus_area_id: weight_pct}
|
||||
|
||||
|
||||
class FocusAreaUsageTypesUpdate(BaseModel):
|
||||
"""Replace all usage-type assignments for one focus area (admin)."""
|
||||
usage_type_keys: List[str] = Field(default_factory=list)
|
||||
|
||||
# ============================================================================
|
||||
# Focus Area Definitions (Admin)
|
||||
# ============================================================================
|
||||
@@ -58,14 +64,27 @@ def list_focus_area_definitions(
|
||||
|
||||
query = """
|
||||
SELECT id, key, name_de, name_en, icon, description, category, is_active,
|
||||
created_at, updated_at
|
||||
created_at, updated_at,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT json_agg(faut.key ORDER BY faut.sort_order, faut.key)
|
||||
FROM focus_area_definition_usage_types fadut
|
||||
JOIN focus_area_usage_types faut ON faut.id = fadut.usage_type_id
|
||||
WHERE fadut.focus_area_id = focus_area_definitions.id
|
||||
),
|
||||
'[]'::json
|
||||
) AS allowed_usage_type_keys
|
||||
FROM focus_area_definitions
|
||||
WHERE is_active = true OR %s
|
||||
ORDER BY category, name_de
|
||||
"""
|
||||
|
||||
cur.execute(query, (include_inactive,))
|
||||
areas = [r2d(row) for row in cur.fetchall()]
|
||||
areas = []
|
||||
for row in cur.fetchall():
|
||||
d = r2d(row)
|
||||
d['allowed_usage_type_keys'] = coerce_usage_type_keys(d.get('allowed_usage_type_keys'))
|
||||
areas.append(d)
|
||||
|
||||
# Group by category
|
||||
grouped = {}
|
||||
@@ -75,6 +94,10 @@ def list_focus_area_definitions(
|
||||
grouped[cat] = []
|
||||
grouped[cat].append(area)
|
||||
|
||||
if session.get('role') != 'admin':
|
||||
for area in areas:
|
||||
area.pop('allowed_usage_type_keys', None)
|
||||
|
||||
return {
|
||||
"areas": areas,
|
||||
"grouped": grouped,
|
||||
@@ -226,6 +249,92 @@ def delete_focus_area_definition(
|
||||
|
||||
return {"message": "Focus Area gelöscht"}
|
||||
|
||||
|
||||
@router.get("/usage-types")
|
||||
def list_focus_area_usage_types(session: dict = Depends(require_auth)):
|
||||
"""
|
||||
Liste aller systemdefinierten Nutzungstypen (Admin, Konfigurations-UI).
|
||||
Keine freie Anlage neuer Typen über die API.
|
||||
"""
|
||||
if session.get('role') != 'admin':
|
||||
raise HTTPException(status_code=403, detail="Admin-Rechte erforderlich")
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT id, key, label_de, sort_order
|
||||
FROM focus_area_usage_types
|
||||
ORDER BY sort_order, key
|
||||
""")
|
||||
rows = [r2d(r) for r in cur.fetchall()]
|
||||
return {"usage_types": rows, "total": len(rows)}
|
||||
|
||||
|
||||
@router.put("/definitions/{area_id}/usage-types")
|
||||
def replace_focus_area_usage_types(
|
||||
area_id: str,
|
||||
data: FocusAreaUsageTypesUpdate,
|
||||
session: dict = Depends(require_auth),
|
||||
):
|
||||
"""
|
||||
Ersetzt die Nutzungstyp-Zuweisungen einer Focus Area (Admin).
|
||||
Leere Liste entfernt alle Zuordnungen. Unbekannte Keys → 400.
|
||||
"""
|
||||
if session.get('role') != 'admin':
|
||||
raise HTTPException(status_code=403, detail="Admin-Rechte erforderlich")
|
||||
|
||||
keys = list(dict.fromkeys(data.usage_type_keys)) # dedupe, preserve order
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
cur.execute(
|
||||
"SELECT id FROM focus_area_definitions WHERE id = %s",
|
||||
(area_id,),
|
||||
)
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(status_code=404, detail="Focus Area nicht gefunden")
|
||||
|
||||
if not keys:
|
||||
cur.execute(
|
||||
"DELETE FROM focus_area_definition_usage_types WHERE focus_area_id = %s",
|
||||
(area_id,),
|
||||
)
|
||||
return {"message": "Nutzungstyp-Zuweisungen entfernt", "usage_type_keys": []}
|
||||
|
||||
placeholders = ','.join(['%s'] * len(keys))
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT id, key FROM focus_area_usage_types
|
||||
WHERE key IN ({placeholders})
|
||||
""",
|
||||
keys,
|
||||
)
|
||||
found = {row['key']: row['id'] for row in cur.fetchall()}
|
||||
missing = [k for k in keys if k not in found]
|
||||
if missing:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Unbekannte Nutzungstyp-Keys: {', '.join(missing)}",
|
||||
)
|
||||
|
||||
cur.execute(
|
||||
"DELETE FROM focus_area_definition_usage_types WHERE focus_area_id = %s",
|
||||
(area_id,),
|
||||
)
|
||||
for k in keys:
|
||||
ut_id = found[k]
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO focus_area_definition_usage_types (focus_area_id, usage_type_id)
|
||||
VALUES (%s, %s)
|
||||
ON CONFLICT (focus_area_id, usage_type_id) DO NOTHING
|
||||
""",
|
||||
(area_id, ut_id),
|
||||
)
|
||||
|
||||
return {"message": "Nutzungstyp-Zuweisungen aktualisiert", "usage_type_keys": keys}
|
||||
|
||||
# ============================================================================
|
||||
# User Focus Preferences
|
||||
# ============================================================================
|
||||
|
||||
Reference in new issue
Block a user