feat: Stoffe nach BLS-Gruppen sortieren und aufklappbar listen
Deploy Development / deploy (push) Successful in 1m4s
Build Test / pytest-backend (push) Successful in 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 21s

Energie und Makros zuerst, restliche Naehrstoffgruppen zuklappbar, damit die Liste bedienbar bleibt.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-09-14 13:09:44 +02:00
1 parent 94ddf84262
commit 44e3e216ab
6 files changed
+230 -95

No files matched your search

+65 -2
View File
@@ -9,6 +9,24 @@ from typing import Any
# Keys that feed nutrition_log kcal / protein_g / fat_g / carbs_g
MACRO_KEYS = ("ENERCC", "PROT625", "FAT", "CHO")
GROUP_META = (
("energy", "Energie"),
("macro", "Makronährstoffe"),
("carbs", "Kohlenhydrate"),
("fiber", "Ballaststoffe"),
("fatty_acids", "Fettsäuren"),
("amino_acids", "Aminosäuren"),
("vitamins", "Vitamine"),
("minerals", "Mineralstoffe"),
("organic_acids", "Organische Säuren"),
("polyols", "Zuckeralkohole"),
("other", "Sonstige"),
("extension", "Eigene Erweiterungen"),
("unknown", "Ohne Gruppe"),
)
GROUP_LABELS = {key: label for key, label in GROUP_META}
GROUP_RANK = {key: i for i, (key, _) in enumerate(GROUP_META)}
# Code → (name_de, unit, category) from BLS 4.0 Referenzhandbuch (Jan 2026)
_OFFICIAL: dict[str, tuple[str, str, str]] = {
"ENERCJ": ("Energie (Kilojoule)", "kJ", "energy"),
@@ -162,6 +180,42 @@ def lookup_key(raw: str | None) -> str:
return str(raw or "").strip().upper()
def resolve_group(attr_key: str | None, category: str | None = None, origin: str | None = None) -> tuple[str, str]:
cat = (category or "").strip().lower()
if cat in GROUP_LABELS:
return cat, GROUP_LABELS[cat]
lab = official_label(attr_key)
if lab and lab["category"] in GROUP_LABELS:
return lab["category"], GROUP_LABELS[lab["category"]]
if (origin or "") == "extension":
return "extension", GROUP_LABELS["extension"]
return "unknown", GROUP_LABELS["unknown"]
def annotate_attribute(row: dict[str, Any]) -> dict[str, Any]:
key = row.get("attr_key")
group_key, group_label = resolve_group(key, row.get("category"), row.get("origin"))
row["is_macro"] = key in MACRO_KEYS
row["needs_label"] = label_is_placeholder(row.get("name_de"), key)
row["group_key"] = group_key
row["group_label"] = group_label
row["group_rank"] = GROUP_RANK[group_key]
return row
def sort_attributes(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
def key_of(row: dict[str, Any]):
return (
row.get("group_rank", 99),
0 if row.get("is_macro") else 1,
row.get("sort_order") if row.get("sort_order") is not None else 0,
(row.get("name_de") or row.get("attr_key") or "").casefold(),
row.get("attr_key") or "",
)
return sorted(rows, key=key_of)
def official_label(attr_key: str | None) -> dict[str, str] | None:
key = lookup_key(attr_key)
hit = _OFFICIAL.get(key)
@@ -211,7 +265,11 @@ def fill_placeholder_labels(cur) -> dict[str, int]:
for row in cur.fetchall():
rec = dict(row)
lab = official_label(rec.get("attr_key"))
if not lab or not label_is_placeholder(rec.get("name_de"), rec.get("attr_key")):
if not lab:
continue
need_name = label_is_placeholder(rec.get("name_de"), rec.get("attr_key"))
need_cat = not (rec.get("category") or "").strip()
if not need_name and not need_cat:
continue
cur.execute(
"""
@@ -222,7 +280,12 @@ def fill_placeholder_labels(cur) -> dict[str, int]:
updated_at = NOW()
WHERE id = %s
""",
(lab["name_de"], lab["unit"], lab["category"], rec["id"]),
(
lab["name_de"] if need_name else rec.get("name_de"),
lab["unit"],
lab["category"],
rec["id"],
),
)
updated += 1
return {"updated": updated}
+4 -15
View File
@@ -300,7 +300,7 @@ def admin_update_manual_food(food_id: str, body: ManualFoodCreate, session: dict
@router.get("/attributes")
def admin_list_attributes(session: dict = Depends(require_admin)):
from bls.attribute_labels import MACRO_KEYS, label_is_placeholder
from bls.attribute_labels import annotate_attribute, sort_attributes
with get_db() as conn:
cur = get_cursor(conn)
@@ -309,17 +309,9 @@ def admin_list_attributes(session: dict = Depends(require_admin)):
SELECT id, attr_key, name_de, name_en, unit, category, data_type, origin, sort_order
FROM food_attributes
WHERE is_active = true
ORDER BY origin, sort_order, attr_key
"""
)
rows = []
for r in cur.fetchall():
row = r2d(r)
key = row.get("attr_key")
row["is_macro"] = key in MACRO_KEYS
row["needs_label"] = label_is_placeholder(row.get("name_de"), key)
rows.append(row)
return rows
return sort_attributes([annotate_attribute(r2d(r)) for r in cur.fetchall()])
@router.post("/attributes/fill-labels")
@@ -398,11 +390,8 @@ def admin_update_attribute(attr_id: int, body: AttributeUpdate, session: dict =
row = cur.fetchone()
if not row:
raise HTTPException(404, "Stoff nicht gefunden")
from bls.attribute_labels import MACRO_KEYS, label_is_placeholder
out = r2d(row)
out["is_macro"] = out.get("attr_key") in MACRO_KEYS
out["needs_label"] = label_is_placeholder(out.get("name_de"), out.get("attr_key"))
return out
from bls.attribute_labels import annotate_attribute
return annotate_attribute(r2d(row))
def _write_manual_macros(cur, food_id: str, macros: dict | None) -> None:
+18 -1
View File
@@ -1,8 +1,11 @@
from bls.attribute_labels import (
annotate_attribute,
apply_official_name,
fill_placeholder_labels,
label_is_placeholder,
official_label,
resolve_group,
sort_attributes,
)
@@ -35,6 +38,20 @@ def test_fill_placeholder_labels_skips_custom_names():
cur = _Cur()
stats = fill_placeholder_labels(cur)
assert stats["updated"] == 1
assert stats["updated"] == 2
assert cur.updated[0][0] == "Fett"
assert cur.updated[0][3] == 1
assert cur.updated[1][0] == "Mein EPA"
assert cur.updated[1][2] == "fatty_acids"
def test_group_and_sort_macros_first():
assert resolve_group("ENERCC")[0] == "energy"
assert resolve_group("NA", None, None)[1] == "Mineralstoffe"
assert resolve_group("XYZ", None, "extension")[0] == "extension"
rows = sort_attributes([
annotate_attribute({"attr_key": "NA", "name_de": "Natrium", "sort_order": 2, "origin": "official_bls"}),
annotate_attribute({"attr_key": "FAT", "name_de": "Fett", "sort_order": 9, "origin": "official_bls"}),
annotate_attribute({"attr_key": "ENERCC", "name_de": "Energie", "sort_order": 1, "origin": "official_bls"}),
])
assert [r["attr_key"] for r in rows] == ["ENERCC", "FAT", "NA"]
+1 -1
View File
@@ -22,7 +22,7 @@ MODULE_VERSIONS = {
"activity": "1.2.1", # Legacy CSV import: activity_entries feature enforcement
"nutrition": "1.3.7", # Sichtbarkeit persönlich vs. zentral im Mapping
"tandoor": "1.0.1", # Router-Import ohne future-annotations (Backend startet)
"bls": "1.0.12", # Stoff-Bezeichnungen editierbar; BLS-Glossar; Makro-Pfad dokumentiert
"bls": "1.0.13", # Stoffe gruppiert und sortiert (BLS-Nährstoffgruppen)
"photos": "1.0.0",
"insights": "1.3.0",