feat: Stoff-Bezeichnungen editieren und BLS-Glossar fuellen
Kryptische Schluessel bleiben Identitaet; Namen/Einheit sind aenderbar. Nur ENERCC/PROT625/FAT/CHO schreiben die Makro-Haupttabelle. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e452d9a8fe
commit
94ddf84262
13 files changed
+586
-29
No files matched your search
@@ -28,6 +28,14 @@ class AttributeCreate(BaseModel):
|
||||
enum_values: Optional[list] = None
|
||||
|
||||
|
||||
class AttributeUpdate(BaseModel):
|
||||
name_de: Optional[str] = None
|
||||
name_en: Optional[str] = None
|
||||
unit: Optional[str] = None
|
||||
category: Optional[str] = None
|
||||
sort_order: Optional[int] = None
|
||||
|
||||
|
||||
class ManualFoodCreate(BaseModel):
|
||||
name_de: str
|
||||
name_en: Optional[str] = None
|
||||
@@ -292,6 +300,8 @@ def admin_update_manual_food(food_id: str, body: ManualFoodCreate, session: dict
|
||||
|
||||
@router.get("/attributes")
|
||||
def admin_list_attributes(session: dict = Depends(require_admin)):
|
||||
from bls.attribute_labels import MACRO_KEYS, label_is_placeholder
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
@@ -302,7 +312,23 @@ def admin_list_attributes(session: dict = Depends(require_admin)):
|
||||
ORDER BY origin, sort_order, attr_key
|
||||
"""
|
||||
)
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
rows = []
|
||||
for r in cur.fetchall():
|
||||
row = r2d(r)
|
||||
key = row.get("attr_key")
|
||||
row["is_macro"] = key in MACRO_KEYS
|
||||
row["needs_label"] = label_is_placeholder(row.get("name_de"), key)
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
@router.post("/attributes/fill-labels")
|
||||
def admin_fill_attribute_labels(session: dict = Depends(require_admin)):
|
||||
from bls.attribute_labels import fill_placeholder_labels
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
return fill_placeholder_labels(cur)
|
||||
|
||||
|
||||
@router.post("/attributes")
|
||||
@@ -332,6 +358,53 @@ def admin_create_attribute(body: AttributeCreate, session: dict = Depends(requir
|
||||
return r2d(cur.fetchone())
|
||||
|
||||
|
||||
@router.put("/attributes/{attr_id}")
|
||||
def admin_update_attribute(attr_id: int, body: AttributeUpdate, session: dict = Depends(require_admin)):
|
||||
fields = []
|
||||
params: list = []
|
||||
if body.name_de is not None:
|
||||
name = body.name_de.strip()
|
||||
if not name:
|
||||
raise HTTPException(400, "Name fehlt")
|
||||
fields.append("name_de = %s")
|
||||
params.append(name)
|
||||
if body.name_en is not None:
|
||||
fields.append("name_en = %s")
|
||||
params.append(body.name_en.strip() or None)
|
||||
if body.unit is not None:
|
||||
fields.append("unit = %s")
|
||||
params.append(body.unit.strip() or None)
|
||||
if body.category is not None:
|
||||
fields.append("category = %s")
|
||||
params.append(body.category.strip() or None)
|
||||
if body.sort_order is not None:
|
||||
fields.append("sort_order = %s")
|
||||
params.append(body.sort_order)
|
||||
if not fields:
|
||||
raise HTTPException(400, "Keine Änderung")
|
||||
fields.append("updated_at = NOW()")
|
||||
params.append(attr_id)
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
f"""
|
||||
UPDATE food_attributes
|
||||
SET {', '.join(fields)}
|
||||
WHERE id = %s AND is_active = true
|
||||
RETURNING id, attr_key, name_de, name_en, unit, category, data_type, origin, sort_order
|
||||
""",
|
||||
params,
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(404, "Stoff nicht gefunden")
|
||||
from bls.attribute_labels import MACRO_KEYS, label_is_placeholder
|
||||
out = r2d(row)
|
||||
out["is_macro"] = out.get("attr_key") in MACRO_KEYS
|
||||
out["needs_label"] = label_is_placeholder(out.get("name_de"), out.get("attr_key"))
|
||||
return out
|
||||
|
||||
|
||||
def _write_manual_macros(cur, food_id: str, macros: dict | None) -> None:
|
||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
||||
write_numeric_attributes(cur, food_id, macros_and_attributes_to_values(macros, None))
|
||||
Reference in new issue
Block a user