fix: Listen in der Tagesuebersicht und Katalog-Makros uebernehmen
Tagebuchzeilen mit Liste gelten als zugeordnet. Berechnete Zutaten-Makros lassen sich am Tag in nutrition_log schreiben. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
44e3e216ab
commit
95453d548a
10 files changed
+221
-28
No files matched your search
@@ -10,6 +10,10 @@ LEADING_QTY_RE = re.compile(
|
||||
r"^\s*\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l|stück|stk|st\.?|portion(?:en)?)\b[\s,.:\-–]*",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
TRAILING_QTY_RE = re.compile(
|
||||
r"[\s,.:\-–]+\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l)\s*$",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
MULTISPACE_RE = re.compile(r"\s+")
|
||||
DECIMAL_IN_NAME_RE = re.compile(r"(\d),(\d)")
|
||||
|
||||
@@ -27,8 +31,10 @@ def normalize_food_name(raw: str | None) -> str:
|
||||
s = strip_leading_quantity(raw)
|
||||
if not s:
|
||||
return ""
|
||||
s = TRAILING_QTY_RE.sub("", s)
|
||||
s = s.replace("-", " ")
|
||||
s = DECIMAL_IN_NAME_RE.sub(r"\1.\2", s)
|
||||
return s.lower()
|
||||
return MULTISPACE_RE.sub(" ", s).strip().lower()
|
||||
|
||||
|
||||
def item_name_keys(raw: str | None, stored_norm: str | None = None) -> set[str]:
|
||||
|
||||
@@ -326,10 +326,12 @@ def save_recipe(cur, profile_id: str, rec: dict[str, Any], recipe_id: str | None
|
||||
qty["quantity_raw"], qty["quantity_g"], qty["quantity_amount"], qty["source_unit"], i,
|
||||
),
|
||||
)
|
||||
link_recipes_to_items(cur, profile_id)
|
||||
linked, dates = link_recipes_to_items(cur, profile_id)
|
||||
saved = get_recipe(cur, profile_id, rid)
|
||||
if not saved:
|
||||
raise ValueError("Rezept konnte nicht gelesen werden")
|
||||
saved["items_linked"] = linked
|
||||
saved["dates_linked"] = dates
|
||||
return saved
|
||||
|
||||
|
||||
|
||||
@@ -446,6 +446,65 @@ def _touch_item_counts(cur, profile_id: str, day: str, sums: dict) -> None:
|
||||
)
|
||||
|
||||
|
||||
def day_macro_preview(cur, profile_id: str, day: date | str) -> dict[str, Any]:
|
||||
sums = compute_day_macro_sums(cur, profile_id, day)
|
||||
catalog = sums.get("catalog")
|
||||
existing = sums.get("existing")
|
||||
mapped = int(sums.get("mapped_item_count") or 0)
|
||||
return {
|
||||
"date": str(day)[:10],
|
||||
"existing": existing,
|
||||
"catalog": catalog,
|
||||
"fddb": sums.get("fddb"),
|
||||
"mapped_item_count": mapped,
|
||||
"unmapped_item_count": int(sums.get("unmapped_item_count") or 0),
|
||||
"catalog_origin": sums.get("catalog_origin"),
|
||||
"can_apply": bool(catalog) and mapped > 0,
|
||||
"differs": bool(existing and catalog and macros_differ(existing, catalog)),
|
||||
}
|
||||
|
||||
|
||||
def apply_day_catalog_macros(cur, profile_id: str, day: date | str) -> dict[str, Any]:
|
||||
preview = day_macro_preview(cur, profile_id, day)
|
||||
if not preview["can_apply"]:
|
||||
raise ValueError("Keine aus Zuordnungen berechenbaren Tagesmakros")
|
||||
cur.execute(
|
||||
"SELECT source FROM nutrition_log WHERE profile_id = %s AND date = %s",
|
||||
(profile_id, day),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
source = (row.get("source") if row else None) or "csv"
|
||||
apply_nutrition_day_macros(
|
||||
cur,
|
||||
profile_id,
|
||||
day,
|
||||
preview["catalog"],
|
||||
macro_origin=preview.get("catalog_origin") or "bls",
|
||||
source=source,
|
||||
confirm=True,
|
||||
)
|
||||
rebuild_daily_nutrients(cur, profile_id, day)
|
||||
preview = day_macro_preview(cur, profile_id, day)
|
||||
preview["applied"] = True
|
||||
return preview
|
||||
|
||||
|
||||
def annotate_nutrition_item(cur, profile_id: str, row: dict[str, Any]) -> dict[str, Any]:
|
||||
item = dict(row)
|
||||
item["id"] = str(item["id"])
|
||||
if item.get("food_id"):
|
||||
item["food_id"] = str(item["food_id"])
|
||||
if item.get("recipe_id"):
|
||||
item["recipe_id"] = str(item["recipe_id"])
|
||||
from data_layer.food_recipes import catalog_macros_for_recipe
|
||||
item["recipe_complete"] = catalog_macros_for_recipe(
|
||||
cur, profile_id, item["recipe_id"], item.get("quantity_g")
|
||||
) is not None
|
||||
else:
|
||||
item["recipe_complete"] = False
|
||||
return item
|
||||
|
||||
|
||||
def resolve_choice_macros(sums: dict[str, Any], choice: str) -> tuple[dict[str, float], str]:
|
||||
if choice == "existing":
|
||||
return sums["existing"], "user_confirmed"
|
||||
|
||||
Reference in new issue
Block a user