feat: Eigene Stoffe am Lebensmittel anlegen
EPA-Chips und mg/µg vom Etikett; fehlende Parameter ohne Admin. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a5adff6196
commit
d35087ad84
9 files changed
+246
-29
No files matched your search
@@ -1,8 +1,12 @@
|
||||
"""Catalog attribute definitions and numeric values (BLS EAV + extensions)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
_ATTR_KEY_RE = re.compile(r"[^A-Z0-9]+")
|
||||
_ALLOWED_UNITS = {"g": "g", "mg": "mg", "µg": "µg", "ug": "µg", "mcg": "µg", "kcal": "kcal", "kj": "kJ"}
|
||||
|
||||
MACRO_TO_KEY = {
|
||||
"kcal": "ENERCC",
|
||||
"protein_g": "PROT625",
|
||||
@@ -17,9 +21,24 @@ def scale_to_per_100g(value: float, serving_g: float | None) -> float:
|
||||
return float(value) * (100.0 / float(serving_g))
|
||||
|
||||
|
||||
def list_numeric_attributes(cur, query: str = "", limit: int = 40) -> list[dict[str, Any]]:
|
||||
q = (query or "").strip()
|
||||
def list_numeric_attributes(
|
||||
cur, query: str = "", limit: int = 40, keys: list[str] | None = None
|
||||
) -> list[dict[str, Any]]:
|
||||
lim = min(max(int(limit or 40), 1), 80)
|
||||
wanted = [str(k).strip() for k in (keys or []) if str(k).strip()]
|
||||
if wanted:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, attr_key, name_de, name_en, unit, category, origin
|
||||
FROM food_attributes
|
||||
WHERE is_active = true AND data_type = 'num_per_100g'
|
||||
AND attr_key = ANY(%s)
|
||||
ORDER BY sort_order, attr_key
|
||||
""",
|
||||
(wanted,),
|
||||
)
|
||||
return [dict(r) for r in cur.fetchall()]
|
||||
q = (query or "").strip()
|
||||
if q:
|
||||
like = f"%{q}%"
|
||||
cur.execute(
|
||||
@@ -126,3 +145,43 @@ def extra_attributes_for_food(cur, food_id: str) -> dict[str, float]:
|
||||
(food_id, list(MACRO_TO_KEY.values())),
|
||||
)
|
||||
return {r["attr_key"]: float(r["value_num"]) for r in cur.fetchall()}
|
||||
|
||||
|
||||
def normalize_attr_key(raw: str | None) -> str:
|
||||
key = _ATTR_KEY_RE.sub("_", (raw or "").strip().upper()).strip("_")
|
||||
return key[:64]
|
||||
|
||||
|
||||
def normalize_attr_unit(raw: str | None) -> str:
|
||||
return _ALLOWED_UNITS.get((raw or "g").strip().lower(), "g")
|
||||
|
||||
|
||||
def create_extension_attribute(cur, name_de: str, unit: str = "g", attr_key: str | None = None) -> dict[str, Any]:
|
||||
name = (name_de or "").strip()
|
||||
if not name:
|
||||
raise ValueError("Name fehlt")
|
||||
key = normalize_attr_key(attr_key or name)
|
||||
if not key:
|
||||
raise ValueError("attr_key fehlt")
|
||||
unit_n = normalize_attr_unit(unit)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, attr_key, name_de, name_en, unit, category, origin
|
||||
FROM food_attributes
|
||||
WHERE attr_key = %s AND is_active = true
|
||||
""",
|
||||
(key,),
|
||||
)
|
||||
existing = cur.fetchone()
|
||||
if existing:
|
||||
return dict(existing)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO food_attributes
|
||||
(attr_key, name_de, name_en, unit, category, data_type, origin, sort_order)
|
||||
VALUES (%s, %s, %s, %s, 'extension', 'num_per_100g', 'extension', 9100)
|
||||
RETURNING id, attr_key, name_de, name_en, unit, category, origin
|
||||
""",
|
||||
(key, name, None, unit_n),
|
||||
)
|
||||
return dict(cur.fetchone())
|
||||
Reference in new issue
Block a user