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
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user