feat: Implement reference value types reordering and confidence level sorting
- Added a new API endpoint for reordering reference value types based on user-defined order. - Updated the AdminReferenceValueTypesPage to allow users to reorder types using up/down buttons. - Introduced a consistent confidence level sorting mechanism across the application. - Refactored related components to remove unused sort order fields and improve user experience.
This commit is contained in:
1 parent
45e4e64f15
commit
296e79c3b3
7 files changed
+149
-31
No files matched your search
@@ -37,6 +37,9 @@ REF_VALUE_METHODS = frozenset(
|
||||
|
||||
REF_VALUE_CONFIDENCE = frozenset({"high", "medium", "low", "unknown"})
|
||||
|
||||
# Anzeigereihenfolge (nicht alphabetisch)
|
||||
REF_VALUE_CONFIDENCE_ORDER = ("high", "medium", "low", "unknown")
|
||||
|
||||
VALUE_DATA_TYPES = frozenset({"integer", "decimal", "percentage", "text", "enum"})
|
||||
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ class ReferenceValueTypeAdminCreate(BaseModel):
|
||||
default_unit: Optional[str] = Field(None, max_length=32)
|
||||
value_data_type: str = "decimal"
|
||||
validation_rules: Optional[dict] = None
|
||||
sort_order: int = 0
|
||||
active: bool = True
|
||||
metadata: Optional[dict] = None
|
||||
|
||||
@@ -60,11 +59,14 @@ class ReferenceValueTypeAdminUpdate(BaseModel):
|
||||
default_unit: Optional[str] = Field(None, max_length=32)
|
||||
value_data_type: Optional[str] = None
|
||||
validation_rules: Optional[dict] = None
|
||||
sort_order: Optional[int] = None
|
||||
active: Optional[bool] = None
|
||||
metadata: Optional[dict] = None
|
||||
|
||||
|
||||
class ReferenceValueTypesReorderBody(BaseModel):
|
||||
ordered_ids: list[int] = Field(..., min_length=1)
|
||||
|
||||
|
||||
def _normalize_key(key: str) -> str:
|
||||
k = key.strip().lower()
|
||||
if not KEY_PATTERN.match(k):
|
||||
@@ -106,6 +108,37 @@ def admin_list_reference_value_types(session: dict = Depends(require_admin)):
|
||||
return [_serialize_type(r2d(r)) for r in cur.fetchall()]
|
||||
|
||||
|
||||
@router.post("/reorder")
|
||||
def admin_reorder_reference_value_types(
|
||||
body: ReferenceValueTypesReorderBody,
|
||||
session: dict = Depends(require_admin),
|
||||
):
|
||||
"""Globale Reihenfolge setzen (sort_order = 10, 20, …). Liste muss alle Typ-IDs genau einmal enthalten."""
|
||||
ids = body.ordered_ids
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("SELECT id FROM reference_value_types")
|
||||
all_ids = sorted([r["id"] for r in cur.fetchall()])
|
||||
if len(ids) != len(all_ids):
|
||||
raise HTTPException(
|
||||
400,
|
||||
f"Erwartet {len(all_ids)} Einträge, erhalten {len(ids)}.",
|
||||
)
|
||||
if sorted(ids) != all_ids:
|
||||
raise HTTPException(400, "Die ID-Liste muss alle Kennwert-Typen exakt einmal enthalten (keine Duplikate).")
|
||||
if len(set(ids)) != len(ids):
|
||||
raise HTTPException(400, "Doppelte IDs sind nicht erlaubt.")
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
for idx, tid in enumerate(ids):
|
||||
cur.execute(
|
||||
"UPDATE reference_value_types SET sort_order = %s WHERE id = %s",
|
||||
((idx + 1) * 10, tid),
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/{type_id}")
|
||||
def admin_get_reference_value_type(type_id: int, session: dict = Depends(require_admin)):
|
||||
with get_db() as conn:
|
||||
@@ -148,6 +181,8 @@ def admin_create_reference_value_type(
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
try:
|
||||
cur.execute("SELECT COALESCE(MAX(sort_order), 0) AS m FROM reference_value_types")
|
||||
next_sort = int(cur.fetchone()["m"]) + 10
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO reference_value_types
|
||||
@@ -166,7 +201,7 @@ def admin_create_reference_value_type(
|
||||
du,
|
||||
vdt,
|
||||
Json(rules),
|
||||
body.sort_order,
|
||||
next_sort,
|
||||
body.active,
|
||||
Json(meta),
|
||||
),
|
||||
|
||||
@@ -18,6 +18,7 @@ from auth import require_auth
|
||||
from db import get_db, get_cursor, r2d
|
||||
from reference_value_validation import (
|
||||
REF_VALUE_CONFIDENCE,
|
||||
REF_VALUE_CONFIDENCE_ORDER,
|
||||
REF_VALUE_METHODS,
|
||||
REF_VALUE_SOURCES,
|
||||
validate_meta_confidence,
|
||||
@@ -112,7 +113,7 @@ def list_reference_value_meta_enums(session: dict = Depends(require_auth)):
|
||||
return {
|
||||
"sources": sorted(REF_VALUE_SOURCES),
|
||||
"methods": sorted(REF_VALUE_METHODS),
|
||||
"confidence_levels": sorted(REF_VALUE_CONFIDENCE),
|
||||
"confidence_levels": [x for x in REF_VALUE_CONFIDENCE_ORDER if x in REF_VALUE_CONFIDENCE],
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user