fix: Offene Lebensmittel nach Listen-Import wieder anzeigen
Deploy Development / deploy (push) Successful in 1m1s
Build Test / pytest-backend (push) Failing after 2s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 27s

Unmapped ohne Kreuzjoin, Listen-GET kurz, Rebuild im Hintergrund.
Tandoor-Zugang (URL/Token) in den Einstellungen mit ausliefern.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-09-13 10:36:08 +02:00
1 parent fa542e0fb9
commit d6838abda7
23 files changed
+786 -114

No files matched your search

+85
View File
@@ -99,6 +99,91 @@ def filter_unmapped_since(rows: list[dict], since_days: int, today: date | None
return out
def fetch_unmapped_rows(cur, profile_id: str) -> list[dict[str, Any]]:
"""Diary lines without food/recipe plus list ingredients without mapping.
Ingredient dates come from recipe.updated_at or last diary use of that list
(items that already have recipe_id) — not a join of every recipe name to
every diary row.
"""
cur.execute(
"""
SELECT source_name_normalized
FROM food_name_mappings
WHERE profile_id = %s
""",
(profile_id,),
)
mapped = {r["source_name_normalized"] for r in cur.fetchall()}
cur.execute(
"""
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,
MIN(i.quantity_raw) AS sample_quantity_raw
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
""",
(profile_id,),
)
diary = [dict(r) | {"kind": "diary"} for r in cur.fetchall()]
cur.execute(
"""
SELECT i.source_name_raw, i.source_name_normalized,
COUNT(*) AS count,
MIN(COALESCE(u.last_used, r.updated_at::date, r.created_at::date)) AS first_date,
MAX(COALESCE(u.last_used, r.updated_at::date, r.created_at::date)) AS last_date,
MIN(i.quantity_raw) AS sample_quantity_raw
FROM food_recipe_ingredients i
JOIN food_recipes r ON r.id = i.recipe_id
LEFT JOIN (
SELECT recipe_id, MIN(date) AS first_used, MAX(date) AS last_used
FROM nutrition_items
WHERE profile_id = %s AND recipe_id IS NOT NULL
GROUP BY recipe_id
) u ON u.recipe_id = r.id
WHERE r.profile_id = %s
AND NOT EXISTS (
SELECT 1 FROM food_name_mappings m
WHERE m.profile_id = r.profile_id
AND m.source_name_normalized = i.source_name_normalized
)
GROUP BY i.source_name_raw, i.source_name_normalized
""",
(profile_id, profile_id),
)
ings = [dict(r) | {"kind": "recipe_ingredient"} for r in cur.fetchall()]
out = []
for row in merge_unmapped_rows(diary + ings):
key = row.get("source_name_normalized") or normalize_food_name(row.get("source_name_raw"))
if not key or key in mapped:
continue
row["last_date"] = as_iso_date(row.get("last_date"))
row["first_date"] = as_iso_date(row.get("first_date"))
out.append(row)
return out
def list_unmapped_payload(
cur,
profile_id: str,
since_days: int = 0,
count_only: bool = False,
meta: bool = False,
) -> Any:
days = max(0, min(int(since_days or 0), 3650))
out = fetch_unmapped_rows(cur, profile_id)
items = sort_unmapped_rows(filter_unmapped_since(out, days) if days else out, days)
if count_only:
return {"count": len(items), "total": len(out), "since_days": days}
if meta:
return {"items": items, "count": len(items), "total": len(out), "since_days": days}
return items
def sort_unmapped_rows(rows: list[dict], since_days: int = 0) -> list[dict]:
rows = list(rows)
if int(since_days or 0) > 0:
+53 -12
View File
@@ -69,7 +69,12 @@ def upsert_recipes(cur, profile_id: str, recipes: list[dict[str, Any]]) -> dict[
if not inorm:
continue
raw_name = ing.get("source_name_raw") or inorm
qty = _resolved_ingredient_qty(cur, profile_id, raw_name, ing)
qty = resolve_quantity(
quantity_raw=ing.get("quantity_raw"),
quantity_amount=ing.get("quantity_amount"),
source_unit=ing.get("source_unit"),
quantity_g=ing.get("quantity_g"),
)
cur.execute(
"""
INSERT INTO food_recipe_ingredients
@@ -120,7 +125,7 @@ def link_recipes_to_items(cur, profile_id: str) -> tuple[int, list[str]]:
return cur.rowcount or 0, dates
def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
def list_recipes(cur, profile_id: str, with_ingredients: bool = True) -> list[dict[str, Any]]:
cur.execute(
"""
SELECT id, name_raw, name_normalized, portions, description, source
@@ -134,6 +139,33 @@ def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
if not recipes:
return []
ids = [str(r["id"]) for r in recipes]
if not with_ingredients:
cur.execute(
"""
SELECT recipe_id, COUNT(*) AS n
FROM food_recipe_ingredients
WHERE recipe_id = ANY(%s::uuid[])
GROUP BY recipe_id
""",
(ids,),
)
counts = {str(r["recipe_id"]): int(r["n"]) for r in cur.fetchall()}
for rec in recipes:
rec["id"] = str(rec["id"])
rec["ingredient_count"] = counts.get(rec["id"], 0)
rec["ingredients"] = []
return recipes
by_r = _ingredients_by_recipe(cur, ids)
for rec in recipes:
rec["id"] = str(rec["id"])
rec["ingredients"] = by_r.get(rec["id"], [])
rec["ingredient_count"] = len(rec["ingredients"])
return recipes
def _ingredients_by_recipe(cur, recipe_ids: list[str]) -> dict[str, list]:
if not recipe_ids:
return {}
cur.execute(
"""
SELECT recipe_id, source_name_raw, source_name_normalized,
@@ -142,22 +174,31 @@ def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
WHERE recipe_id = ANY(%s::uuid[])
ORDER BY sort_order, source_name_raw
""",
(ids,),
(recipe_ids,),
)
by_r: dict[str, list] = {str(i): [] for i in ids}
by_r: dict[str, list] = {str(i): [] for i in recipe_ids}
for row in cur.fetchall():
by_r.setdefault(str(row["recipe_id"]), []).append(dict(row))
for rec in recipes:
rec["id"] = str(rec["id"])
rec["ingredients"] = by_r.get(rec["id"], [])
return recipes
return by_r
def get_recipe(cur, profile_id: str, recipe_id: str) -> dict[str, Any] | None:
for rec in list_recipes(cur, profile_id):
if rec["id"] == str(recipe_id):
return rec
return None
cur.execute(
"""
SELECT id, name_raw, name_normalized, portions, description, source
FROM food_recipes
WHERE profile_id = %s AND id = %s
""",
(profile_id, recipe_id),
)
rec = cur.fetchone()
if not rec:
return None
rec = dict(rec)
rec["id"] = str(rec["id"])
rec["ingredients"] = _ingredients_by_recipe(cur, [rec["id"]]).get(rec["id"], [])
rec["ingredient_count"] = len(rec["ingredients"])
return rec
def save_recipe(cur, profile_id: str, rec: dict[str, Any], recipe_id: str | None = None) -> dict[str, Any]:
+23
View File
@@ -1,10 +1,14 @@
"""Nutrition diary items, three macro sums, import policy, attribute resolve."""
from __future__ import annotations
import logging
import threading
import uuid
from datetime import date, datetime
from typing import Any
logger = logging.getLogger(__name__)
from data_layer.food_mapping import (
get_food_mapping_with_cursor,
normalize_food_name,
@@ -222,6 +226,25 @@ def _accumulate_food_qty(cur, acc: dict[int, list[float]], food_id: str, quantit
acc.setdefault(row["attribute_id"], []).append(float(row["value_num"]) * factor)
def schedule_daily_nutrient_rebuild(profile_id: str, dates: list[str], context: str) -> None:
days = [str(d) for d in (dates or []) if d]
if not days:
return
def _run() -> None:
try:
from db import get_cursor, get_db
with get_db() as conn:
cur = get_cursor(conn)
for day in days:
rebuild_daily_nutrients(cur, profile_id, day)
except Exception:
logger.exception("Nährwert-Rebuild nach %s fehlgeschlagen", context)
threading.Thread(target=_run, daemon=True, name="nutrition-rebuild").start()
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",
+120
View File
@@ -0,0 +1,120 @@
"""HTTP client for a user's Tandoor instance. Never logs the token."""
from __future__ import annotations
import json
import ssl
import urllib.error
import urllib.request
from typing import Any
from urllib.parse import urljoin, urlparse
class TandoorError(ValueError):
pass
def normalize_base_url(raw: str | None) -> str:
url = (raw or "").strip().rstrip("/")
if not url:
raise TandoorError("Tandoor-URL fehlt")
parsed = urlparse(url)
if parsed.scheme not in ("https", "http") or not parsed.netloc:
raise TandoorError("Tandoor-URL muss mit https:// oder http:// beginnen")
if parsed.scheme == "http" and parsed.hostname not in ("localhost", "127.0.0.1"):
raise TandoorError("Tandoor-URL muss https verwenden")
return url
def token_hint(token: str | None) -> str | None:
t = (token or "").strip()
if len(t) < 4:
return "gesetzt" if t else None
return f"…{t[-4:]}"
def public_settings(row: dict[str, Any] | None) -> dict[str, Any]:
if not row:
return {
"configured": False,
"base_url": "",
"token_set": False,
"token_hint": None,
"last_ok_at": None,
"last_error": None,
}
return {
"configured": True,
"base_url": row.get("base_url") or "",
"token_set": bool((row.get("api_token") or "").strip()),
"token_hint": token_hint(row.get("api_token")),
"last_ok_at": row.get("last_ok_at").isoformat() if row.get("last_ok_at") else None,
"last_error": row.get("last_error"),
}
def _request(base_url: str, token: str, path: str, timeout: float = 10.0) -> tuple[int, str, str]:
url = urljoin(base_url.rstrip("/") + "/", path.lstrip("/"))
req = urllib.request.Request(
url,
headers={
"Authorization": f"Token {token.strip()}",
"Accept": "application/json",
"User-Agent": "Mitai-Jinkendo/tandoor-connector",
},
method="GET",
)
ctx = ssl.create_default_context()
class _NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl): # noqa: ARG002
return None
opener = urllib.request.build_opener(_NoRedirect, urllib.request.HTTPSHandler(context=ctx))
try:
with opener.open(req, timeout=timeout) as resp:
body = resp.read().decode("utf-8", errors="replace")
ctype = resp.headers.get("Content-Type", "")
return resp.status, body, ctype
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace") if e.fp else ""
ctype = e.headers.get("Content-Type", "") if e.headers else ""
return e.code, body, ctype
except TimeoutError as e:
raise TandoorError("Tandoor antwortet nicht (Zeitüberschreitung)") from e
except urllib.error.URLError as e:
raise TandoorError(f"Tandoor nicht erreichbar: {e.reason}") from e
def probe_connection(base_url: str, token: str) -> dict[str, Any]:
url = normalize_base_url(base_url)
tok = (token or "").strip()
if not tok:
raise TandoorError("API-Token fehlt")
status, body, ctype = _request(url, tok, "api/user/")
if status in (301, 302, 303, 307, 308) or "text/html" in (ctype or "") and status < 400:
raise TandoorError("Tandoor hat nicht mit JSON geantwortet — URL oder Token prüfen")
if status in (401, 403):
raise TandoorError("Token ungültig oder ohne Rechte")
if status == 404:
status, body, ctype = _request(url, tok, "api/recipe/?page=1&page_size=1")
if status in (401, 403):
raise TandoorError("Token ungültig oder ohne Rechte")
if status >= 400:
raise TandoorError(f"Tandoor antwortete mit HTTP {status}")
if "json" not in (ctype or "") and not (body or "").lstrip().startswith(("{", "[")):
raise TandoorError("Tandoor hat nicht mit JSON geantwortet — URL oder Token prüfen")
try:
data = json.loads(body) if body else {}
except json.JSONDecodeError as e:
raise TandoorError("Tandoor-Antwort war kein JSON") from e
name = None
if isinstance(data, dict):
results = data.get("results")
if isinstance(results, list) and results and isinstance(results[0], dict):
name = results[0].get("display_name") or results[0].get("username") or results[0].get("name")
else:
name = data.get("display_name") or data.get("username") or data.get("name")
count = data.get("count")
else:
count = None
return {"ok": True, "user": name, "recipe_count": count}
+67
View File
@@ -0,0 +1,67 @@
"""Persist per-profile Tandoor credentials. Token never returned in full."""
from __future__ import annotations
from typing import Any
from data_layer.tandoor_client import public_settings
def get_settings(cur, profile_id: str) -> dict[str, Any] | None:
cur.execute(
"""
SELECT profile_id, base_url, api_token, last_ok_at, last_error, updated_at
FROM profile_tandoor_settings
WHERE profile_id = %s
""",
(profile_id,),
)
row = cur.fetchone()
return dict(row) if row else None
def get_public_settings(cur, profile_id: str) -> dict[str, Any]:
return public_settings(get_settings(cur, profile_id))
def upsert_settings(cur, profile_id: str, base_url: str, token: str | None, *, clear_token: bool = False) -> dict[str, Any]:
existing = get_settings(cur, profile_id)
if clear_token:
cur.execute("DELETE FROM profile_tandoor_settings WHERE profile_id = %s", (profile_id,))
return public_settings(None)
stored = (token or "").strip() or ((existing or {}).get("api_token") or "")
if not stored:
raise ValueError("API-Token fehlt")
cur.execute(
"""
INSERT INTO profile_tandoor_settings (profile_id, base_url, api_token, last_error, updated_at)
VALUES (%s, %s, %s, NULL, NOW())
ON CONFLICT (profile_id) DO UPDATE SET
base_url = EXCLUDED.base_url,
api_token = EXCLUDED.api_token,
last_error = NULL,
updated_at = NOW()
""",
(profile_id, base_url, stored),
)
return get_public_settings(cur, profile_id)
def record_probe(cur, profile_id: str, ok: bool, error: str | None = None) -> None:
if ok:
cur.execute(
"""
UPDATE profile_tandoor_settings
SET last_ok_at = NOW(), last_error = NULL, updated_at = NOW()
WHERE profile_id = %s
""",
(profile_id,),
)
else:
cur.execute(
"""
UPDATE profile_tandoor_settings
SET last_error = %s, updated_at = NOW()
WHERE profile_id = %s
""",
((error or "")[:300], profile_id),
)