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}