feat: Eigene und zentrale Lebensmittel bearbeiten, mit Sichtbarkeit im Mapping
Nutzer können persönliche Foods anlegen und ändern; der Admin legt zentrale Einträge für alle an. Suche und Namens-Mapping berücksichtigen persönlich, zentral und BLS. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d35087ad84
commit
8b1e1da5d8
30 files changed
+1195
-120
No files matched your search
@@ -32,6 +32,8 @@ class ManualFoodCreate(BaseModel):
|
||||
name_de: str
|
||||
name_en: Optional[str] = None
|
||||
macros_per_100g: Optional[dict] = None
|
||||
attributes: Optional[dict] = None
|
||||
serving_g: Optional[float] = None
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
@@ -162,7 +164,9 @@ def admin_list_foods(
|
||||
cur = get_cursor(conn)
|
||||
conds = ["is_active = true"]
|
||||
params: list = []
|
||||
if kind:
|
||||
if kind == "manual":
|
||||
conds.append("catalog_kind IN ('manual_user', 'manual_admin')")
|
||||
elif kind:
|
||||
conds.append("catalog_kind = %s")
|
||||
params.append(kind)
|
||||
if q:
|
||||
@@ -172,7 +176,7 @@ def admin_list_foods(
|
||||
params.append(limit)
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, bls_version
|
||||
SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, bls_version, owner_profile_id
|
||||
FROM food_catalog
|
||||
WHERE {' AND '.join(conds)}
|
||||
ORDER BY name_de
|
||||
@@ -199,11 +203,15 @@ def admin_food_detail(food_id: str, session: dict = Depends(require_admin)):
|
||||
|
||||
@router.post("/foods/manual")
|
||||
def admin_create_manual_food(body: ManualFoodCreate, session: dict = Depends(require_admin)):
|
||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
||||
from data_layer.food_mapping import normalize_food_name
|
||||
from data_layer.food_visibility import attach_visibility
|
||||
|
||||
name = (body.name_de or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(400, "Name fehlt")
|
||||
serving = body.serving_g if body.serving_g and body.serving_g > 0 else None
|
||||
values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving)
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
@@ -215,10 +223,64 @@ def admin_create_manual_food(body: ManualFoodCreate, session: dict = Depends(req
|
||||
(name, body.name_en, f"man-admin-{normalize_food_name(name)[:40]}"),
|
||||
)
|
||||
food = r2d(cur.fetchone())
|
||||
_write_manual_macros(cur, food["id"], body.macros_per_100g)
|
||||
write_numeric_attributes(cur, food["id"], values)
|
||||
from data_layer.food_suggest import invalidate_suggest_index
|
||||
invalidate_suggest_index()
|
||||
return food
|
||||
return attach_visibility(food)
|
||||
|
||||
|
||||
@router.put("/foods/manual/{food_id}")
|
||||
def admin_update_manual_food(food_id: str, body: ManualFoodCreate, session: dict = Depends(require_admin)):
|
||||
from collections import defaultdict
|
||||
|
||||
from data_layer.food_attributes import (
|
||||
editor_payload_for_food,
|
||||
macros_and_attributes_to_values,
|
||||
replace_numeric_attributes,
|
||||
)
|
||||
from data_layer.food_visibility import attach_visibility
|
||||
from data_layer.nutrition_items import profiles_dates_using_food, schedule_daily_nutrient_rebuild
|
||||
|
||||
name = (body.name_de or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(400, "Name fehlt")
|
||||
serving = body.serving_g if body.serving_g and body.serving_g > 0 else None
|
||||
values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving)
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, name_de, name_en, catalog_kind, owner_profile_id
|
||||
FROM food_catalog
|
||||
WHERE id = %s AND catalog_kind = 'manual_admin' AND is_active = true
|
||||
""",
|
||||
(food_id,),
|
||||
)
|
||||
food = cur.fetchone()
|
||||
if not food:
|
||||
raise HTTPException(404, "Zentrales Lebensmittel nicht gefunden")
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE food_catalog
|
||||
SET name_de = %s, name_en = %s, updated_at = NOW()
|
||||
WHERE id = %s
|
||||
RETURNING id, name_de, name_en, catalog_kind, owner_profile_id
|
||||
""",
|
||||
(name, body.name_en, food_id),
|
||||
)
|
||||
updated = dict(cur.fetchone())
|
||||
replace_numeric_attributes(cur, food_id, values)
|
||||
pairs = profiles_dates_using_food(cur, food_id)
|
||||
payload = attach_visibility(editor_payload_for_food(cur, updated, editable=True))
|
||||
from data_layer.food_suggest import invalidate_suggest_index
|
||||
invalidate_suggest_index()
|
||||
by_profile = defaultdict(list)
|
||||
for pid, day in pairs:
|
||||
by_profile[pid].append(day)
|
||||
for pid, dates in by_profile.items():
|
||||
schedule_daily_nutrient_rebuild(pid, dates, f"Zentrales Lebensmittel {name}")
|
||||
payload["rebuild_pending"] = len(pairs)
|
||||
return payload
|
||||
|
||||
|
||||
@router.get("/attributes")
|
||||
|
||||
Reference in new issue
Block a user