feat: Rezepte beim Zuordnen anlegen und offene Zutaten listen
Lebensmittel oder Rezept wählbar; gemappte Zutaten werden übernommen, offene erscheinen in der Liste. Dazu Fettgehalt-Suche, EPA-Stoffe, Rezept-CRUD und Wechsel bestehender Zuordnungen. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
132a364a3a
commit
a7e5511039
26 files changed
+1234
-147
No files matched your search
@@ -22,13 +22,19 @@ BUNDLE_VERSION = 1
|
||||
def _jsonable(value: Any) -> Any:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, (str, int, float, bool)):
|
||||
return value
|
||||
if isinstance(value, Decimal):
|
||||
return float(value)
|
||||
if isinstance(value, UUID):
|
||||
return str(value)
|
||||
if isinstance(value, dict):
|
||||
return {str(k): _jsonable(v) for k, v in value.items()}
|
||||
if isinstance(value, (list, tuple)):
|
||||
return [_jsonable(v) for v in value]
|
||||
if hasattr(value, "isoformat"):
|
||||
return value.isoformat()
|
||||
return value
|
||||
return str(value)
|
||||
|
||||
|
||||
def parse_food_knowledge_bundle(data: Any) -> dict[str, Any]:
|
||||
@@ -54,7 +60,7 @@ def portable_mapping(row: dict[str, Any]) -> dict[str, Any]:
|
||||
"food_name_de": row.get("food_name_de") or row.get("name_de"),
|
||||
"catalog_kind": row.get("catalog_kind"),
|
||||
"external_key": row.get("external_key"),
|
||||
"grams_per_unit": row.get("grams_per_unit"),
|
||||
"grams_per_unit": _jsonable(row.get("grams_per_unit")),
|
||||
"source_unit": row.get("source_unit"),
|
||||
}
|
||||
|
||||
@@ -126,12 +132,14 @@ def export_food_knowledge(cur, profile_id: str) -> dict[str, Any]:
|
||||
manuals = []
|
||||
for food in cur.fetchall():
|
||||
macros = catalog_macros_for_item(cur, food["id"], 100.0)
|
||||
from data_layer.food_attributes import extra_attributes_for_food
|
||||
manuals.append({
|
||||
"name_de": food["name_de"],
|
||||
"name_en": food.get("name_en"),
|
||||
"catalog_kind": food["catalog_kind"],
|
||||
"external_key": food.get("external_key"),
|
||||
"macros_per_100g": macros,
|
||||
"attributes": extra_attributes_for_food(cur, food["id"]),
|
||||
})
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -165,14 +173,14 @@ def export_food_knowledge(cur, profile_id: str) -> dict[str, Any]:
|
||||
for ing in rec.get("ingredients") or []
|
||||
],
|
||||
})
|
||||
return {
|
||||
return _jsonable({
|
||||
"format": BUNDLE_FORMAT,
|
||||
"version": BUNDLE_VERSION,
|
||||
"exported_at": datetime.now(timezone.utc).isoformat(),
|
||||
"manual_foods": manuals,
|
||||
"mappings": mappings,
|
||||
"recipes": recipes,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
def _upsert_manual_food(cur, profile_id: str, food: dict[str, Any]) -> str | None:
|
||||
@@ -180,9 +188,10 @@ def _upsert_manual_food(cur, profile_id: str, food: dict[str, Any]) -> str | Non
|
||||
if not name:
|
||||
return None
|
||||
existing = resolve_catalog_food(cur, profile_id, {**food, "food_name_de": name, "catalog_kind": "manual_user"})
|
||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
||||
values = macros_and_attributes_to_values(food.get("macros_per_100g"), food.get("attributes"))
|
||||
if existing:
|
||||
from routers.admin_bls import _write_manual_macros
|
||||
_write_manual_macros(cur, existing, food.get("macros_per_100g"))
|
||||
write_numeric_attributes(cur, existing, values)
|
||||
return existing
|
||||
key = (food.get("external_key") or "").strip() or f"man-user-{normalize_food_name(name)[:40]}"
|
||||
cur.execute(
|
||||
@@ -195,8 +204,7 @@ def _upsert_manual_food(cur, profile_id: str, food: dict[str, Any]) -> str | Non
|
||||
(name, food.get("name_en"), profile_id, key),
|
||||
)
|
||||
food_id = str(cur.fetchone()["id"])
|
||||
from routers.admin_bls import _write_manual_macros
|
||||
_write_manual_macros(cur, food_id, food.get("macros_per_100g"))
|
||||
write_numeric_attributes(cur, food_id, values)
|
||||
return food_id
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user