feat: Listen-Dialog mit Mengen/Einheiten, Mitai ohne Kochrezepte
Roh-Kombinationen statt Rezept-Sprache; Zutaten suchen, EL/TL/Prise auf Gramm. Gekochte Gerichte und Ausbeute bleiben Tandoor. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a7e5511039
commit
fa542e0fb9
17 files changed
+776
-203
No files matched your search
@@ -4,10 +4,32 @@ from __future__ import annotations
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from data_layer.food_mapping import get_food_mapping_with_cursor, normalize_food_name
|
||||
from data_layer.food_mapping import (
|
||||
get_food_mapping_with_cursor,
|
||||
normalize_food_name,
|
||||
resolve_quantity,
|
||||
)
|
||||
from data_layer.nutrition_items import catalog_macros_for_item, _f
|
||||
|
||||
|
||||
def _resolved_ingredient_qty(cur, profile_id: str, source_name_raw: str, ing: dict[str, Any]) -> dict[str, Any]:
|
||||
mapping = get_food_mapping_with_cursor(cur, source_name_raw, profile_id)
|
||||
gpu = ing.get("grams_per_unit")
|
||||
if gpu in (None, ""):
|
||||
gpu = mapping.get("grams_per_unit") if mapping else None
|
||||
try:
|
||||
gpu = float(gpu) if gpu not in (None, "") else None
|
||||
except (TypeError, ValueError):
|
||||
gpu = None
|
||||
return resolve_quantity(
|
||||
quantity_raw=ing.get("quantity_raw"),
|
||||
quantity_amount=ing.get("quantity_amount"),
|
||||
source_unit=ing.get("source_unit"),
|
||||
quantity_g=ing.get("quantity_g"),
|
||||
grams_per_unit=gpu,
|
||||
)
|
||||
|
||||
|
||||
def upsert_recipes(cur, profile_id: str, recipes: list[dict[str, Any]]) -> dict[str, int]:
|
||||
inserted = updated = ingredients = 0
|
||||
for rec in recipes:
|
||||
@@ -46,16 +68,19 @@ def upsert_recipes(cur, profile_id: str, recipes: list[dict[str, Any]]) -> dict[
|
||||
inorm = ing.get("source_name_normalized") or normalize_food_name(ing.get("source_name_raw"))
|
||||
if not inorm:
|
||||
continue
|
||||
raw_name = ing.get("source_name_raw") or inorm
|
||||
qty = _resolved_ingredient_qty(cur, profile_id, raw_name, ing)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO food_recipe_ingredients
|
||||
(id, recipe_id, source_name_raw, source_name_normalized,
|
||||
quantity_raw, quantity_g, sort_order)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s)
|
||||
quantity_raw, quantity_g, quantity_amount, source_unit, sort_order)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
||||
""",
|
||||
(
|
||||
str(uuid.uuid4()), rid, ing["source_name_raw"], inorm,
|
||||
ing.get("quantity_raw"), ing.get("quantity_g"), ing.get("sort_order") or 0,
|
||||
str(uuid.uuid4()), rid, raw_name, inorm,
|
||||
qty["quantity_raw"], qty["quantity_g"], qty["quantity_amount"], qty["source_unit"],
|
||||
ing.get("sort_order") or 0,
|
||||
),
|
||||
)
|
||||
ingredients += 1
|
||||
@@ -111,7 +136,8 @@ def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
|
||||
ids = [str(r["id"]) for r in recipes]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT recipe_id, source_name_raw, source_name_normalized, quantity_raw, quantity_g, sort_order
|
||||
SELECT recipe_id, source_name_raw, source_name_normalized,
|
||||
quantity_raw, quantity_g, quantity_amount, source_unit, sort_order
|
||||
FROM food_recipe_ingredients
|
||||
WHERE recipe_id = ANY(%s::uuid[])
|
||||
ORDER BY sort_order, source_name_raw
|
||||
@@ -195,19 +221,18 @@ def save_recipe(cur, profile_id: str, rec: dict[str, Any], recipe_id: str | None
|
||||
inorm = ing.get("source_name_normalized") or normalize_food_name(raw)
|
||||
if not inorm:
|
||||
continue
|
||||
qty = ing.get("quantity_g")
|
||||
try:
|
||||
qty = float(qty) if qty not in (None, "") else None
|
||||
except (TypeError, ValueError):
|
||||
qty = None
|
||||
qty = _resolved_ingredient_qty(cur, profile_id, raw or inorm, ing)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO food_recipe_ingredients
|
||||
(id, recipe_id, source_name_raw, source_name_normalized,
|
||||
quantity_raw, quantity_g, sort_order)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s)
|
||||
quantity_raw, quantity_g, quantity_amount, source_unit, sort_order)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
||||
""",
|
||||
(str(uuid.uuid4()), rid, raw or inorm, inorm, ing.get("quantity_raw"), qty, i),
|
||||
(
|
||||
str(uuid.uuid4()), rid, raw or inorm, inorm,
|
||||
qty["quantity_raw"], qty["quantity_g"], qty["quantity_amount"], qty["source_unit"], i,
|
||||
),
|
||||
)
|
||||
link_recipes_to_items(cur, profile_id)
|
||||
saved = get_recipe(cur, profile_id, rid)
|
||||
@@ -249,7 +274,7 @@ def mapped_ingredient_quantities(
|
||||
return None
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT source_name_raw, quantity_g
|
||||
SELECT source_name_raw, quantity_g, quantity_raw, quantity_amount, source_unit
|
||||
FROM food_recipe_ingredients
|
||||
WHERE recipe_id = %s
|
||||
ORDER BY sort_order
|
||||
@@ -259,22 +284,29 @@ def mapped_ingredient_quantities(
|
||||
ings = cur.fetchall()
|
||||
if not ings:
|
||||
return None
|
||||
total_g = sum(_f(i.get("quantity_g")) for i in ings)
|
||||
resolved_ings = []
|
||||
for ing in ings:
|
||||
mapping = get_food_mapping_with_cursor(cur, ing["source_name_raw"], profile_id)
|
||||
if not mapping:
|
||||
return None
|
||||
qty = resolve_quantity(
|
||||
quantity_raw=ing.get("quantity_raw"),
|
||||
quantity_amount=ing.get("quantity_amount"),
|
||||
source_unit=ing.get("source_unit"),
|
||||
quantity_g=ing.get("quantity_g"),
|
||||
grams_per_unit=mapping.get("grams_per_unit"),
|
||||
)
|
||||
grams = _f(qty.get("quantity_g"))
|
||||
if grams <= 0:
|
||||
return None
|
||||
resolved_ings.append({"food_id": mapping["food_id"], "quantity_g": grams})
|
||||
total_g = sum(i["quantity_g"] for i in resolved_ings)
|
||||
if eaten_qty_g and eaten_qty_g > 0 and total_g > 0:
|
||||
scale = float(eaten_qty_g) / total_g
|
||||
else:
|
||||
portions = float(rec.get("portions") or 1) or 1.0
|
||||
scale = 1.0 / portions
|
||||
out = []
|
||||
for ing in ings:
|
||||
mapping = get_food_mapping_with_cursor(cur, ing["source_name_raw"], profile_id)
|
||||
if not mapping:
|
||||
return None
|
||||
out.append({
|
||||
"food_id": mapping["food_id"],
|
||||
"quantity_g": _f(ing.get("quantity_g")) * scale,
|
||||
})
|
||||
return out
|
||||
return [{**i, "quantity_g": i["quantity_g"] * scale} for i in resolved_ings]
|
||||
|
||||
|
||||
def catalog_macros_for_recipe(cur, profile_id: str, recipe_id: str, eaten_qty_g: float | None) -> dict[str, float] | None:
|
||||
|
||||
Reference in new issue
Block a user