feat: FDDB-Listen auflösen und Katalog nach Name suchen
Tagebuchzeilen eigener Rezepte werden über den Listen-Import in Zutaten zerlegt. Zuordnen erfolgt im Namens-Popup statt per BLS-Code. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4b1be3019d
commit
86f15e6957
17 files changed
+866
-86
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""Parse FDDB lists_*.csv (name;…;produkte) into recipe + ingredients."""
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import io
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from data_layer.food_mapping import normalize_food_name, parse_quantity_g
|
||||
|
||||
ING_RE = re.compile(
|
||||
r"(?P<qty>\d+(?:[.,]\d+)?)\s*(?P<unit>g|kg|ml|l)\b\s*(?P<name>.+?)"
|
||||
r"(?=,\s*\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l)\b|$)",
|
||||
re.IGNORECASE | re.DOTALL,
|
||||
)
|
||||
|
||||
|
||||
def parse_fddb_produkte(text: str) -> list[dict[str, Any]]:
|
||||
raw = (text or "").strip().strip('"')
|
||||
if not raw:
|
||||
return []
|
||||
out: list[dict[str, Any]] = []
|
||||
for i, m in enumerate(ING_RE.finditer(raw)):
|
||||
qty = float(m.group("qty").replace(",", "."))
|
||||
unit = m.group("unit").lower()
|
||||
name = re.sub(r"\s+", " ", m.group("name")).strip(" ,;")
|
||||
if not name:
|
||||
continue
|
||||
grams = qty
|
||||
if unit == "kg":
|
||||
grams = qty * 1000.0
|
||||
elif unit == "l":
|
||||
grams = qty * 1000.0
|
||||
elif unit == "ml":
|
||||
grams = qty
|
||||
out.append({
|
||||
"source_name_raw": name,
|
||||
"source_name_normalized": normalize_food_name(name),
|
||||
"quantity_raw": f"{m.group('qty').replace(',', '.')} {unit}",
|
||||
"quantity_g": round(grams, 3),
|
||||
"sort_order": i,
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def parse_fddb_lists_csv(text: str) -> list[dict[str, Any]]:
|
||||
if text.startswith("\ufeff"):
|
||||
text = text[1:]
|
||||
reader = csv.DictReader(io.StringIO(text), delimiter=";")
|
||||
recipes = []
|
||||
for row in reader:
|
||||
name = (row.get("name") or "").strip().strip('"')
|
||||
if not name:
|
||||
continue
|
||||
try:
|
||||
portions = float(str(row.get("anzahl_portionen") or "1").replace(",", "."))
|
||||
except ValueError:
|
||||
portions = 1.0
|
||||
if portions <= 0:
|
||||
portions = 1.0
|
||||
ingredients = parse_fddb_produkte(row.get("produkte") or "")
|
||||
if not ingredients:
|
||||
leftover = (row.get("produkte") or "").strip().strip('"')
|
||||
if leftover:
|
||||
ingredients = [{
|
||||
"source_name_raw": leftover,
|
||||
"source_name_normalized": normalize_food_name(leftover),
|
||||
"quantity_raw": None,
|
||||
"quantity_g": parse_quantity_g(leftover),
|
||||
"sort_order": 0,
|
||||
}]
|
||||
recipes.append({
|
||||
"name_raw": name,
|
||||
"name_normalized": normalize_food_name(name),
|
||||
"portions": portions,
|
||||
"description": (row.get("beschreibung") or "").strip() or None,
|
||||
"ingredients": ingredients,
|
||||
})
|
||||
return recipes
|
||||
@@ -17,6 +17,7 @@ def normalize_food_name(raw: str | None) -> str:
|
||||
if not raw:
|
||||
return ""
|
||||
s = unicodedata.normalize("NFKC", str(raw)).strip().strip('"').strip("'")
|
||||
s = s.lstrip("!")
|
||||
s = LEADING_QTY_RE.sub("", s)
|
||||
s = DECIMAL_IN_NAME_RE.sub(r"\1.\2", s)
|
||||
s = MULTISPACE_RE.sub(" ", s).strip().lower()
|
||||
@@ -133,6 +134,7 @@ def apply_mapping_to_items(cur, profile_id: str, source_name_normalized: str, fo
|
||||
UPDATE nutrition_items
|
||||
SET food_id = %s, mapping_id = %s, value_origin = %s, updated_at = NOW()
|
||||
WHERE profile_id = %s AND source_name_normalized = %s
|
||||
AND recipe_id IS NULL
|
||||
""",
|
||||
(food_id, mapping_id, origin, profile_id, source_name_normalized),
|
||||
)
|
||||
@@ -166,8 +168,11 @@ def suggest_catalog_foods(cur, query: str, profile_id: str | None, limit: int =
|
||||
q = (query or "").strip()
|
||||
if not q:
|
||||
return []
|
||||
like = f"%{q}%"
|
||||
norm = normalize_food_name(q)
|
||||
primary = q.split(",")[0].strip() or q
|
||||
like_full = f"%{q}%"
|
||||
like_primary = f"%{primary}%"
|
||||
prefix = f"{primary}%"
|
||||
norm = normalize_food_name(primary)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, bls_code, name_de, name_en, catalog_kind, food_group
|
||||
@@ -178,17 +183,24 @@ def suggest_catalog_foods(cur, query: str, profile_id: str | None, limit: int =
|
||||
OR owner_profile_id = %s
|
||||
)
|
||||
AND (
|
||||
name_de ILIKE %s OR COALESCE(name_en, '') ILIKE %s
|
||||
name_de ILIKE %s OR name_de ILIKE %s
|
||||
OR COALESCE(name_en, '') ILIKE %s OR COALESCE(name_en, '') ILIKE %s
|
||||
OR COALESCE(bls_code, '') ILIKE %s
|
||||
OR lower(name_de) = %s
|
||||
)
|
||||
ORDER BY
|
||||
CASE WHEN lower(name_de) = %s THEN 0
|
||||
WHEN COALESCE(bls_code, '') ILIKE %s THEN 1
|
||||
ELSE 2 END,
|
||||
CASE
|
||||
WHEN lower(name_de) = %s THEN 0
|
||||
WHEN name_de ILIKE %s THEN 1
|
||||
WHEN COALESCE(bls_code, '') ILIKE %s THEN 2
|
||||
ELSE 3 END,
|
||||
name_de
|
||||
LIMIT %s
|
||||
""",
|
||||
(profile_id, like, like, like, norm, norm, q, limit),
|
||||
(
|
||||
profile_id,
|
||||
like_full, like_primary, like_full, like_primary, like_full, norm,
|
||||
norm, prefix, q, limit,
|
||||
),
|
||||
)
|
||||
return [dict(r) for r in cur.fetchall()]
|
||||
@@ -0,0 +1,192 @@
|
||||
"""FDDB recipe lists: upsert, link to diary items, catalog macros via ingredients."""
|
||||
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.nutrition_items import catalog_macros_for_item, _f
|
||||
|
||||
|
||||
def upsert_recipes(cur, profile_id: str, recipes: list[dict[str, Any]]) -> dict[str, int]:
|
||||
inserted = updated = ingredients = 0
|
||||
for rec in recipes:
|
||||
norm = rec.get("name_normalized") or normalize_food_name(rec.get("name_raw"))
|
||||
if not norm:
|
||||
continue
|
||||
cur.execute(
|
||||
"SELECT id FROM food_recipes WHERE profile_id = %s AND name_normalized = %s",
|
||||
(profile_id, norm),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row:
|
||||
rid = row["id"]
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE food_recipes
|
||||
SET name_raw=%s, portions=%s, description=%s, updated_at=NOW()
|
||||
WHERE id=%s
|
||||
""",
|
||||
(rec["name_raw"], rec.get("portions") or 1, rec.get("description"), rid),
|
||||
)
|
||||
cur.execute("DELETE FROM food_recipe_ingredients WHERE recipe_id = %s", (rid,))
|
||||
updated += 1
|
||||
else:
|
||||
rid = str(uuid.uuid4())
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO food_recipes
|
||||
(id, profile_id, name_raw, name_normalized, portions, description, source)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,'fddb_list')
|
||||
""",
|
||||
(rid, profile_id, rec["name_raw"], norm, rec.get("portions") or 1, rec.get("description")),
|
||||
)
|
||||
inserted += 1
|
||||
for ing in rec.get("ingredients") or []:
|
||||
inorm = ing.get("source_name_normalized") or normalize_food_name(ing.get("source_name_raw"))
|
||||
if not inorm:
|
||||
continue
|
||||
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)
|
||||
""",
|
||||
(
|
||||
str(uuid.uuid4()), rid, ing["source_name_raw"], inorm,
|
||||
ing.get("quantity_raw"), ing.get("quantity_g"), ing.get("sort_order") or 0,
|
||||
),
|
||||
)
|
||||
ingredients += 1
|
||||
linked, dates = link_recipes_to_items(cur, profile_id)
|
||||
return {
|
||||
"inserted": inserted,
|
||||
"updated": updated,
|
||||
"ingredients": ingredients,
|
||||
"items_linked": linked,
|
||||
"dates_linked": dates,
|
||||
}
|
||||
|
||||
|
||||
def link_recipes_to_items(cur, profile_id: str) -> tuple[int, list[str]]:
|
||||
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
|
||||
""",
|
||||
(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
|
||||
|
||||
|
||||
def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, name_raw, name_normalized, portions, description, source
|
||||
FROM food_recipes
|
||||
WHERE profile_id = %s
|
||||
ORDER BY name_normalized
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
recipes = [dict(r) for r in cur.fetchall()]
|
||||
if not recipes:
|
||||
return []
|
||||
ids = [r["id"] for r in recipes]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT recipe_id, source_name_raw, source_name_normalized, quantity_raw, quantity_g, sort_order
|
||||
FROM food_recipe_ingredients
|
||||
WHERE recipe_id = ANY(%s)
|
||||
ORDER BY sort_order, source_name_raw
|
||||
""",
|
||||
(ids,),
|
||||
)
|
||||
by_r: dict[str, list] = {str(i): [] for i in ids}
|
||||
for row in cur.fetchall():
|
||||
by_r.setdefault(str(row["recipe_id"]), []).append(dict(row))
|
||||
for rec in recipes:
|
||||
rec["ingredients"] = by_r.get(str(rec["id"]), [])
|
||||
return recipes
|
||||
|
||||
|
||||
def apply_recipe_to_items(cur, profile_id: str, source_name_normalized: str, recipe_id: str) -> int:
|
||||
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
|
||||
""",
|
||||
(recipe_id, profile_id, source_name_normalized),
|
||||
)
|
||||
return cur.rowcount or 0
|
||||
|
||||
|
||||
def mapped_ingredient_quantities(
|
||||
cur, profile_id: str, recipe_id: str, eaten_qty_g: float | None
|
||||
) -> list[dict[str, Any]] | None:
|
||||
cur.execute(
|
||||
"SELECT portions FROM food_recipes WHERE id = %s AND profile_id = %s",
|
||||
(recipe_id, profile_id),
|
||||
)
|
||||
rec = cur.fetchone()
|
||||
if not rec:
|
||||
return None
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT source_name_raw, quantity_g
|
||||
FROM food_recipe_ingredients
|
||||
WHERE recipe_id = %s
|
||||
ORDER BY sort_order
|
||||
""",
|
||||
(recipe_id,),
|
||||
)
|
||||
ings = cur.fetchall()
|
||||
if not ings:
|
||||
return None
|
||||
total_g = sum(_f(i.get("quantity_g")) for i in 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
|
||||
|
||||
|
||||
def catalog_macros_for_recipe(cur, profile_id: str, recipe_id: str, eaten_qty_g: float | None) -> dict[str, float] | None:
|
||||
parts = mapped_ingredient_quantities(cur, profile_id, recipe_id, eaten_qty_g)
|
||||
if not parts:
|
||||
return None
|
||||
acc = {"kcal": 0.0, "protein_g": 0.0, "fat_g": 0.0, "carbs_g": 0.0}
|
||||
for part in parts:
|
||||
cat = catalog_macros_for_item(cur, part["food_id"], part["quantity_g"])
|
||||
if not cat:
|
||||
return None
|
||||
for k in acc:
|
||||
acc[k] += cat[k]
|
||||
return acc
|
||||
@@ -99,7 +99,7 @@ def compute_day_macro_sums(cur, profile_id: str, day: date | str) -> dict[str, A
|
||||
)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT food_id, quantity_g, fddb_kcal, fddb_protein_g, fddb_fat_g, fddb_carbs_g, value_origin
|
||||
SELECT food_id, recipe_id, quantity_g, fddb_kcal, fddb_protein_g, fddb_fat_g, fddb_carbs_g, value_origin
|
||||
FROM nutrition_items
|
||||
WHERE profile_id = %s AND date = %s
|
||||
""",
|
||||
@@ -115,7 +115,11 @@ def compute_day_macro_sums(cur, profile_id: str, day: date | str) -> dict[str, A
|
||||
fddb["protein_g"] += _f(it.get("fddb_protein_g"))
|
||||
fddb["fat_g"] += _f(it.get("fddb_fat_g"))
|
||||
fddb["carbs_g"] += _f(it.get("fddb_carbs_g"))
|
||||
cat = catalog_macros_for_item(cur, it.get("food_id"), it.get("quantity_g"))
|
||||
if it.get("recipe_id") and not it.get("food_id"):
|
||||
from data_layer.food_recipes import catalog_macros_for_recipe
|
||||
cat = catalog_macros_for_recipe(cur, profile_id, it["recipe_id"], it.get("quantity_g"))
|
||||
else:
|
||||
cat = catalog_macros_for_item(cur, it.get("food_id"), it.get("quantity_g"))
|
||||
if cat:
|
||||
mapped += 1
|
||||
used_bls = True
|
||||
@@ -200,6 +204,24 @@ def apply_nutrition_day_macros(
|
||||
return "created"
|
||||
|
||||
|
||||
def _accumulate_food_qty(cur, acc: dict[int, list[float]], food_id: str, quantity_g: float) -> None:
|
||||
if not food_id or not quantity_g or quantity_g <= 0:
|
||||
return
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT v.attribute_id, v.value_num, v.is_trace, a.data_type
|
||||
FROM food_attribute_values v
|
||||
JOIN food_attributes a ON a.id = v.attribute_id
|
||||
WHERE v.food_id = %s AND a.data_type = 'num_per_100g'
|
||||
AND v.value_num IS NOT NULL AND v.is_trace = false
|
||||
""",
|
||||
(food_id,),
|
||||
)
|
||||
factor = float(quantity_g) / 100.0
|
||||
for row in cur.fetchall():
|
||||
acc.setdefault(row["attribute_id"], []).append(float(row["value_num"]) * factor)
|
||||
|
||||
|
||||
def rebuild_daily_nutrients(cur, profile_id: str, day: date | str) -> None:
|
||||
cur.execute(
|
||||
"DELETE FROM nutrition_daily_nutrients WHERE profile_id = %s AND date = %s",
|
||||
@@ -207,28 +229,24 @@ def rebuild_daily_nutrients(cur, profile_id: str, day: date | str) -> None:
|
||||
)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT i.food_id, i.quantity_g
|
||||
FROM nutrition_items i
|
||||
WHERE i.profile_id = %s AND i.date = %s
|
||||
AND i.food_id IS NOT NULL AND i.quantity_g IS NOT NULL AND i.quantity_g > 0
|
||||
SELECT food_id, recipe_id, quantity_g
|
||||
FROM nutrition_items
|
||||
WHERE profile_id = %s AND date = %s
|
||||
""",
|
||||
(profile_id, day),
|
||||
)
|
||||
acc: dict[int, list[float]] = {}
|
||||
from data_layer.food_recipes import mapped_ingredient_quantities
|
||||
for it in cur.fetchall():
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT v.attribute_id, v.value_num, v.is_trace, a.data_type
|
||||
FROM food_attribute_values v
|
||||
JOIN food_attributes a ON a.id = v.attribute_id
|
||||
WHERE v.food_id = %s AND a.data_type = 'num_per_100g'
|
||||
AND v.value_num IS NOT NULL AND v.is_trace = false
|
||||
""",
|
||||
(it["food_id"],),
|
||||
)
|
||||
factor = float(it["quantity_g"]) / 100.0
|
||||
for row in cur.fetchall():
|
||||
acc.setdefault(row["attribute_id"], []).append(float(row["value_num"]) * factor)
|
||||
if it.get("food_id"):
|
||||
_accumulate_food_qty(cur, acc, it["food_id"], _f(it.get("quantity_g")))
|
||||
continue
|
||||
if it.get("recipe_id"):
|
||||
parts = mapped_ingredient_quantities(cur, profile_id, it["recipe_id"], it.get("quantity_g"))
|
||||
if not parts:
|
||||
continue
|
||||
for part in parts:
|
||||
_accumulate_food_qty(cur, acc, part["food_id"], part["quantity_g"])
|
||||
for attr_id, vals in acc.items():
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -302,9 +320,9 @@ def replace_csv_items_for_dates(
|
||||
id, profile_id, date, logged_at, source_name_raw, source_name_normalized,
|
||||
source_system, quantity_raw, quantity_g,
|
||||
fddb_kcal, fddb_protein_g, fddb_fat_g, fddb_carbs_g,
|
||||
food_id, mapping_id, value_origin, source
|
||||
food_id, mapping_id, value_origin, source, recipe_id
|
||||
) VALUES (
|
||||
%s,%s,%s,%s,%s,%s,'fddb',%s,%s,%s,%s,%s,%s,%s,%s,%s,'csv'
|
||||
%s,%s,%s,%s,%s,%s,'fddb',%s,%s,%s,%s,%s,%s,%s,%s,%s,'csv',%s
|
||||
)
|
||||
""",
|
||||
(
|
||||
@@ -323,9 +341,12 @@ def replace_csv_items_for_dates(
|
||||
mapping["food_id"] if mapping else None,
|
||||
mapping["mapping_id"] if mapping else None,
|
||||
_item_value_origin(mapping),
|
||||
None,
|
||||
),
|
||||
)
|
||||
items_written += 1
|
||||
from data_layer.food_recipes import link_recipes_to_items
|
||||
link_recipes_to_items(cur, profile_id)
|
||||
days_written += 1
|
||||
sums = compute_day_macro_sums(cur, profile_id, iso)
|
||||
rebuild_daily_nutrients(cur, profile_id, iso)
|
||||
@@ -431,7 +452,12 @@ def dates_for_normalized_name(cur, profile_id: str, source_name_normalized: str)
|
||||
SELECT DISTINCT date::text AS date
|
||||
FROM nutrition_items
|
||||
WHERE profile_id = %s AND source_name_normalized = %s
|
||||
UNION
|
||||
SELECT DISTINCT i.date::text AS date
|
||||
FROM nutrition_items i
|
||||
JOIN food_recipe_ingredients ri ON ri.recipe_id = i.recipe_id
|
||||
WHERE i.profile_id = %s AND ri.source_name_normalized = %s
|
||||
""",
|
||||
(profile_id, source_name_normalized),
|
||||
(profile_id, source_name_normalized, profile_id, source_name_normalized),
|
||||
)
|
||||
return [r["date"] for r in cur.fetchall()]
|
||||
@@ -0,0 +1,43 @@
|
||||
-- Migration 063: FDDB-Listen/Rezepte + Verknüpfung an nutrition_items
|
||||
|
||||
CREATE TABLE IF NOT EXISTS food_recipes (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
name_raw VARCHAR(500) NOT NULL,
|
||||
name_normalized VARCHAR(500) NOT NULL,
|
||||
portions NUMERIC(8,2) NOT NULL DEFAULT 1,
|
||||
description TEXT,
|
||||
source VARCHAR(20) NOT NULL DEFAULT 'fddb_list',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT uq_food_recipe_profile_name UNIQUE (profile_id, name_normalized)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_food_recipes_profile ON food_recipes (profile_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS food_recipe_ingredients (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
recipe_id UUID NOT NULL REFERENCES food_recipes(id) ON DELETE CASCADE,
|
||||
source_name_raw VARCHAR(500) NOT NULL,
|
||||
source_name_normalized VARCHAR(500) NOT NULL,
|
||||
quantity_raw VARCHAR(80),
|
||||
quantity_g NUMERIC(10,3),
|
||||
sort_order INT NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_food_recipe_ing_recipe ON food_recipe_ingredients (recipe_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_food_recipe_ing_norm ON food_recipe_ingredients (source_name_normalized);
|
||||
|
||||
ALTER TABLE nutrition_items
|
||||
ADD COLUMN IF NOT EXISTS recipe_id UUID REFERENCES food_recipes(id) ON DELETE SET NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_nutrition_items_recipe
|
||||
ON nutrition_items (recipe_id)
|
||||
WHERE recipe_id IS NOT NULL;
|
||||
|
||||
COMMENT ON TABLE food_recipes IS 'FDDB-Listen/Rezepte; Tagebuchzeile kann statt Einzel-BLS auf ein Rezept zeigen';
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
RAISE NOTICE 'Migration 063: FDDB recipes + nutrition_items.recipe_id';
|
||||
END $$;
|
||||
@@ -354,16 +354,109 @@ def list_unmapped_foods(
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT source_name_raw, source_name_normalized,
|
||||
COUNT(*) AS count, MIN(date) AS first_date, MAX(date) AS last_date
|
||||
FROM nutrition_items
|
||||
WHERE profile_id=%s AND food_id IS NULL
|
||||
GROUP BY source_name_raw, source_name_normalized
|
||||
ORDER BY count DESC, source_name_normalized
|
||||
SELECT i.source_name_raw, i.source_name_normalized,
|
||||
COUNT(*) AS count, MIN(i.date) AS first_date, MAX(i.date) AS last_date,
|
||||
MIN(r.id::text) AS matching_recipe_id
|
||||
FROM nutrition_items i
|
||||
LEFT JOIN food_recipes r
|
||||
ON r.profile_id = i.profile_id AND r.name_normalized = i.source_name_normalized
|
||||
WHERE i.profile_id=%s AND i.food_id IS NULL AND i.recipe_id IS NULL
|
||||
GROUP BY i.source_name_raw, i.source_name_normalized
|
||||
ORDER BY count DESC, i.source_name_normalized
|
||||
""",
|
||||
(pid,),
|
||||
)
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
diary = [r2d(r) | {"kind": "diary"} for r in cur.fetchall()]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT i.source_name_raw, i.source_name_normalized,
|
||||
COUNT(*) AS count, NULL::date AS first_date, NULL::date AS last_date
|
||||
FROM food_recipe_ingredients i
|
||||
JOIN food_recipes r ON r.id = i.recipe_id
|
||||
LEFT JOIN food_name_mappings m
|
||||
ON m.profile_id = r.profile_id
|
||||
AND m.source_name_normalized = i.source_name_normalized
|
||||
WHERE r.profile_id = %s AND m.id IS NULL
|
||||
GROUP BY i.source_name_raw, i.source_name_normalized
|
||||
ORDER BY count DESC, i.source_name_normalized
|
||||
""",
|
||||
(pid,),
|
||||
)
|
||||
ings = [r2d(r) | {"kind": "recipe_ingredient"} for r in cur.fetchall()]
|
||||
seen = {d["source_name_normalized"] for d in diary}
|
||||
for ing in ings:
|
||||
if ing["source_name_normalized"] not in seen:
|
||||
diary.append(ing)
|
||||
seen.add(ing["source_name_normalized"])
|
||||
diary.sort(key=lambda x: (-int(x.get("count") or 0), x.get("source_name_normalized") or ""))
|
||||
return diary
|
||||
|
||||
|
||||
@router.get("/recipes")
|
||||
def list_food_recipes(
|
||||
x_profile_id: Optional[str] = Header(default=None),
|
||||
session: dict = Depends(require_auth),
|
||||
):
|
||||
from data_layer.food_recipes import list_recipes
|
||||
pid = get_pid(x_profile_id)
|
||||
with get_db() as conn:
|
||||
return list_recipes(get_cursor(conn), pid)
|
||||
|
||||
|
||||
@router.post("/recipes/import-fddb-lists")
|
||||
async def import_fddb_lists(
|
||||
file: UploadFile = File(...),
|
||||
x_profile_id: Optional[str] = Header(default=None),
|
||||
session: dict = Depends(require_auth),
|
||||
):
|
||||
from bls.recipe_parser import parse_fddb_lists_csv
|
||||
from data_layer.food_recipes import upsert_recipes
|
||||
|
||||
pid = get_pid(x_profile_id)
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
raise HTTPException(400, "Leere Datei")
|
||||
try:
|
||||
text = raw.decode("utf-8-sig")
|
||||
except UnicodeDecodeError:
|
||||
text = raw.decode("latin-1")
|
||||
recipes = parse_fddb_lists_csv(text)
|
||||
if not recipes:
|
||||
raise HTTPException(400, "Keine Rezepte in der Datei erkannt")
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
stats = upsert_recipes(cur, pid, recipes)
|
||||
from data_layer.nutrition_items import rebuild_daily_nutrients
|
||||
for d in stats.pop("dates_linked", []) or []:
|
||||
rebuild_daily_nutrients(cur, pid, d)
|
||||
return {"ok": True, "recipes": len(recipes), **stats}
|
||||
|
||||
|
||||
@router.post("/recipes/{recipe_id}/apply")
|
||||
def apply_recipe_name(
|
||||
recipe_id: str,
|
||||
body: dict,
|
||||
x_profile_id: Optional[str] = Header(default=None),
|
||||
session: dict = Depends(require_auth),
|
||||
):
|
||||
from data_layer.food_mapping import normalize_food_name
|
||||
from data_layer.food_recipes import apply_recipe_to_items
|
||||
from data_layer.nutrition_items import dates_for_normalized_name, rebuild_daily_nutrients
|
||||
|
||||
pid = get_pid(x_profile_id)
|
||||
source_name = (body.get("source_name") or "").strip()
|
||||
if not source_name:
|
||||
raise HTTPException(400, "source_name fehlt")
|
||||
norm = normalize_food_name(source_name)
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("SELECT id FROM food_recipes WHERE id = %s AND profile_id = %s", (recipe_id, pid))
|
||||
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):
|
||||
rebuild_daily_nutrients(cur, pid, d)
|
||||
return {"ok": True, "items_updated": n}
|
||||
|
||||
|
||||
@router.post("/import-conflicts/resolve")
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
from bls.recipe_parser import parse_fddb_lists_csv, parse_fddb_produkte
|
||||
from data_layer.food_mapping import normalize_food_name
|
||||
|
||||
|
||||
PORRIDGE = (
|
||||
"160 g Apfel, Braeburn, 5 ml Omega-3 Vegan Algenöl, 100 g Wildheidelbeeren, "
|
||||
"30 g Haferflocken, 100% Hafer-Vollkorn, 40 g Hafer Flocken, Großblatt, 11 g Flohsamenschalen"
|
||||
)
|
||||
|
||||
|
||||
def test_parse_fddb_produkte_splits_on_next_quantity_not_commas():
|
||||
ings = parse_fddb_produkte(PORRIDGE)
|
||||
names = [i["source_name_raw"] for i in ings]
|
||||
assert names == [
|
||||
"Apfel, Braeburn",
|
||||
"Omega-3 Vegan Algenöl",
|
||||
"Wildheidelbeeren",
|
||||
"Haferflocken, 100% Hafer-Vollkorn",
|
||||
"Hafer Flocken, Großblatt",
|
||||
"Flohsamenschalen",
|
||||
]
|
||||
assert ings[0]["quantity_g"] == 160.0
|
||||
assert ings[1]["quantity_g"] == 5.0
|
||||
assert ings[1]["quantity_raw"] == "5 ml"
|
||||
|
||||
|
||||
def test_parse_fddb_lists_csv_porridge_and_portions():
|
||||
text = (
|
||||
"name;beschreibung;anzahl_portionen;zeit_vorbereitung;zeit_kochen;produkte;\n"
|
||||
'"!PorridgeBreakfast ";"";"1";"0";"0";"' + PORRIDGE + '";\n'
|
||||
'"Aloo gobi";"";"4";"0";"0";"32 ml Rapso Rapsöl, 83 g Zwiebel, frisch";\n'
|
||||
)
|
||||
recipes = parse_fddb_lists_csv(text)
|
||||
assert len(recipes) == 2
|
||||
assert recipes[0]["name_raw"] == "!PorridgeBreakfast"
|
||||
assert recipes[0]["name_normalized"] == normalize_food_name("PorridgeBreakfast")
|
||||
assert len(recipes[0]["ingredients"]) == 6
|
||||
assert recipes[1]["portions"] == 4.0
|
||||
assert recipes[1]["ingredients"][0]["source_name_raw"] == "Rapso Rapsöl"
|
||||
|
||||
|
||||
def test_normalize_strips_list_bang_prefix():
|
||||
assert normalize_food_name("!PorridgeBreakfast") == normalize_food_name("PorridgeBreakfast")
|
||||
+4
-2
@@ -9,7 +9,7 @@ Semantic Versioning: MAJOR.MINOR.PATCH
|
||||
|
||||
APP_VERSION = "0.9v"
|
||||
BUILD_DATE = "2026-09-12"
|
||||
DB_SCHEMA_VERSION = "20260912" # 062 BLS catalog + nutrition items/marks
|
||||
DB_SCHEMA_VERSION = "20260912" # 063 FDDB recipes
|
||||
|
||||
MODULE_VERSIONS = {
|
||||
"auth": "1.2.0",
|
||||
@@ -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.1.0", # BLS mapping, items, day marks, import policy
|
||||
"nutrition": "1.2.0", # FDDB-Listen/Rezepte + Katalog-Suchpopup
|
||||
"bls": "1.0.1",
|
||||
"photos": "1.0.0",
|
||||
"insights": "1.3.0",
|
||||
@@ -45,6 +45,8 @@ CHANGELOG = [
|
||||
"Lernendes FDDB-Mapping ohne KI, änder- und löschbar",
|
||||
"Optionale nutrition_items, Import-Policy, Fasten-/Lücken-Marken",
|
||||
"BLS-Import als Hintergrundjob (kein Proxy-504)",
|
||||
"Zuordnen: Katalog-Suche nach Name (Popup), nicht nach BLS-Code",
|
||||
"FDDB-Listen/Rezepte importieren und Tagebuchzeilen in Zutaten auflösen",
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user