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
@@ -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