feat: Listen-Tab und sichtbare Zutatenlisten-Verwaltung
Listen anlegen, bearbeiten und importieren liegt unter Ernährung. Tagebuchzeilen werden auch mit Mengen-Prefix verknüpft. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3b46630a37
commit
b37005be01
12 files changed
+307
-73
No files matched your search
@@ -31,6 +31,28 @@ def normalize_food_name(raw: str | None) -> str:
|
||||
return s.lower()
|
||||
|
||||
|
||||
def item_name_keys(raw: str | None, stored_norm: str | None = None) -> set[str]:
|
||||
"""Compare diary names to list names despite stored quantity prefixes."""
|
||||
keys: set[str] = set()
|
||||
for v in (raw, stored_norm):
|
||||
if not v:
|
||||
continue
|
||||
text = str(v).strip()
|
||||
if text:
|
||||
keys.add(text.lower())
|
||||
norm = normalize_food_name(text)
|
||||
if norm:
|
||||
keys.add(norm)
|
||||
return keys
|
||||
|
||||
|
||||
def names_match_list(raw: str | None, stored_norm: str | None, list_norm: str | None) -> bool:
|
||||
target = normalize_food_name(list_norm) if list_norm else ""
|
||||
if not target:
|
||||
return False
|
||||
return target in item_name_keys(raw, stored_norm)
|
||||
|
||||
|
||||
_LIST_COMMA_RE = re.compile(r",(?!\s*\d)")
|
||||
|
||||
|
||||
@@ -130,6 +152,29 @@ def fetch_unmapped_rows(cur, profile_id: str) -> list[dict[str, Any]]:
|
||||
(profile_id,),
|
||||
)
|
||||
diary = [dict(r) | {"kind": "diary"} for r in cur.fetchall()]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, name_raw, name_normalized
|
||||
FROM food_recipes
|
||||
WHERE profile_id = %s
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
lists = [dict(r) for r in cur.fetchall()]
|
||||
for row in diary:
|
||||
keys = item_name_keys(row.get("source_name_raw"), row.get("source_name_normalized"))
|
||||
hit = next(
|
||||
(
|
||||
rec
|
||||
for rec in lists
|
||||
if rec.get("name_normalized") in keys
|
||||
or normalize_food_name(rec.get("name_raw")) in keys
|
||||
),
|
||||
None,
|
||||
)
|
||||
if hit:
|
||||
row["matching_recipe_id"] = str(hit["id"])
|
||||
row["matching_recipe_name"] = hit["name_raw"]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT i.source_name_raw, i.source_name_normalized,
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import Any
|
||||
|
||||
from data_layer.food_mapping import (
|
||||
get_food_mapping_with_cursor,
|
||||
names_match_list,
|
||||
normalize_food_name,
|
||||
resolve_quantity,
|
||||
)
|
||||
@@ -100,29 +101,49 @@ def upsert_recipes(cur, profile_id: str, recipes: list[dict[str, Any]]) -> dict[
|
||||
|
||||
|
||||
def link_recipes_to_items(cur, profile_id: str) -> tuple[int, list[str]]:
|
||||
cur.execute(
|
||||
"SELECT id, name_raw, name_normalized FROM food_recipes WHERE profile_id = %s",
|
||||
(profile_id,),
|
||||
)
|
||||
recipes = [dict(r) for r in cur.fetchall()]
|
||||
if not recipes:
|
||||
return 0, []
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT DISTINCT i.date::text AS date
|
||||
FROM nutrition_items i
|
||||
JOIN food_recipes r ON r.profile_id = i.profile_id
|
||||
AND i.source_name_normalized = r.name_normalized
|
||||
WHERE i.profile_id = %s AND i.food_id IS NULL
|
||||
SELECT id, date::text AS date, source_name_raw, source_name_normalized
|
||||
FROM nutrition_items
|
||||
WHERE profile_id = %s AND food_id IS NULL AND recipe_id IS NULL
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
dates = [r["date"] for r in cur.fetchall()]
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE nutrition_items i
|
||||
SET recipe_id = r.id, updated_at = NOW()
|
||||
FROM food_recipes r
|
||||
WHERE i.profile_id = %s AND r.profile_id = %s
|
||||
AND i.source_name_normalized = r.name_normalized
|
||||
AND i.food_id IS NULL
|
||||
""",
|
||||
(profile_id, profile_id),
|
||||
)
|
||||
return cur.rowcount or 0, dates
|
||||
by_rid: dict[str, list[str]] = {}
|
||||
dates: set[str] = set()
|
||||
for item in cur.fetchall():
|
||||
rid = next(
|
||||
(
|
||||
str(rec["id"])
|
||||
for rec in recipes
|
||||
if names_match_list(item.get("source_name_raw"), item.get("source_name_normalized"), rec.get("name_normalized"))
|
||||
),
|
||||
None,
|
||||
)
|
||||
if not rid:
|
||||
continue
|
||||
by_rid.setdefault(rid, []).append(str(item["id"]))
|
||||
if item.get("date"):
|
||||
dates.add(str(item["date"]))
|
||||
linked = 0
|
||||
for rid, ids in by_rid.items():
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE nutrition_items
|
||||
SET recipe_id = %s, updated_at = NOW()
|
||||
WHERE id = ANY(%s::uuid[])
|
||||
""",
|
||||
(rid, ids),
|
||||
)
|
||||
linked += cur.rowcount or 0
|
||||
return linked, list(dates)
|
||||
|
||||
|
||||
def list_recipes(cur, profile_id: str, with_ingredients: bool = True) -> list[dict[str, Any]]:
|
||||
@@ -292,13 +313,29 @@ def delete_recipe(cur, profile_id: str, recipe_id: str) -> None:
|
||||
|
||||
|
||||
def apply_recipe_to_items(cur, profile_id: str, source_name_normalized: str, recipe_id: str) -> int:
|
||||
target = normalize_food_name(source_name_normalized) or (source_name_normalized or "")
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, source_name_raw, source_name_normalized
|
||||
FROM nutrition_items
|
||||
WHERE profile_id = %s AND food_id IS NULL
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
ids = [
|
||||
str(row["id"])
|
||||
for row in cur.fetchall()
|
||||
if names_match_list(row.get("source_name_raw"), row.get("source_name_normalized"), target)
|
||||
]
|
||||
if not ids:
|
||||
return 0
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE nutrition_items
|
||||
SET recipe_id = %s, food_id = NULL, mapping_id = NULL, value_origin = 'fddb', updated_at = NOW()
|
||||
WHERE profile_id = %s AND source_name_normalized = %s
|
||||
WHERE id = ANY(%s::uuid[])
|
||||
""",
|
||||
(recipe_id, profile_id, source_name_normalized),
|
||||
(recipe_id, ids),
|
||||
)
|
||||
return cur.rowcount or 0
|
||||
|
||||
|
||||
Reference in new issue
Block a user