feat: Listen-Tab und sichtbare Zutatenlisten-Verwaltung
Deploy Development / deploy (push) Successful in 59s
Build Test / pytest-backend (push) Successful in 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 22s

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:
LarsandCursor committed 2026-09-13 12:40:53 +02:00
1 parent 3b46630a37
commit b37005be01
12 files changed
+307 -73

No files matched your search

+45
View File
@@ -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,
+57 -20
View File
@@ -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
+12 -1
View File
@@ -504,7 +504,18 @@ def apply_recipe_name(
if not cur.fetchone():
raise HTTPException(404, "Rezept nicht gefunden")
n = apply_recipe_to_items(cur, pid, norm, recipe_id)
for d in dates_for_normalized_name(cur, pid, norm):
cur.execute(
"""
SELECT DISTINCT date::text AS date
FROM nutrition_items
WHERE profile_id = %s AND recipe_id = %s
""",
(pid, recipe_id),
)
days = [r["date"] for r in cur.fetchall()]
if not days:
days = dates_for_normalized_name(cur, pid, norm)
for d in days:
rebuild_daily_nutrients(cur, pid, d)
return {"ok": True, "items_updated": n}
+9
View File
@@ -7,6 +7,7 @@ from data_layer.food_mapping import (
list_quantity_units,
list_unmapped_payload,
merge_unmapped_rows,
names_match_list,
normalize_food_name,
parse_quantity,
parse_quantity_g,
@@ -104,6 +105,7 @@ def test_fetch_unmapped_keeps_ingredient_dates_and_skips_mapped():
cur = _FakeCur([
[{"source_name_normalized": "haferflocken"}],
[],
[],
[
{
"source_name_raw": "Salz",
@@ -129,8 +131,15 @@ def test_fetch_unmapped_keeps_ingredient_dates_and_skips_mapped():
assert rows[0]["kind"] == "recipe_ingredient"
def test_list_name_matches_despite_quantity_prefix():
assert names_match_list("50 g Müsli Mix", "50 g müsli mix", "müsli mix")
assert names_match_list("Müsli Mix", "müsli mix", "müsli mix")
assert not names_match_list("Haferflocken", "haferflocken", "müsli mix")
def test_unmapped_meta_includes_recent_ingredients():
batches = [
[],
[],
[],
[{
+1 -1
View File
@@ -20,7 +20,7 @@ MODULE_VERSIONS = {
"circumference": "1.0.1",
"caliper": "1.0.1",
"activity": "1.2.1", # Legacy CSV import: activity_entries feature enforcement
"nutrition": "1.3.3", # Unmapped ohne Kreuzjoin; Listen-Import Rebuild async
"nutrition": "1.3.4", # Listen-Tab; Verknüpfen trotz Mengen-Prefix; Liste wählen ohne Vorfilter
"tandoor": "1.0.1", # Router-Import ohne future-annotations (Backend startet)
"bls": "1.0.5", # GET /bls/units
"photos": "1.0.0",