Files
xenia-pos-local/local_backend/core/allergens.py
T
bonaminandClaude Opus 5.5 5048f82a4c feat(manager): products and categories on v2, a product in several categories, allergens (platform-v2 step 6.3)
- Management/ProductsTab + ProductFormModal (3.1k lines) → src/pages/products/: category tree,
  list/grid, toolbar + filters (search folds accents and Latin letters), bulk actions, category
  dialogs; the editor in editor/ (details, sold at, cost, fiscal; the waiter's choices).
  Arithmetic in productsLogic.js / editor/formLogic.js (tests/products.test.mjs).
- Several categories per product with a main one (D-23): edited in the editor, "also in" on the
  rows, each category's list ordered on its own (drag or up/down → PUT /reorder with category_id);
  bulk "move to" / "also list in". The waiter's picker now shows a product in every category.
- Allergens: products.allergens (Alembic 0032, core/allergens.py: 14 EU, contains | may_contain,
  catalog.allergen), in the API, POS menu, menu tablet, catalogue bundles and the cloud snapshot
  (C-2); @xenia/access allergens.js + <Allergens> shown on the phone, the counter, the tablet.
- Bell: only the newest load sets the list (an older answer could hide a live notification).
- e2e: manager (two categories, order per category, allergens), terminal / menu tablet / waiter
  (allergens shown); media saves by test id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:04:35 +02:00

51 lines
1.9 KiB
Python

"""Allergens (plan step 6.3): the 14 that EU Regulation 1169/2011 (Annex II) requires a venue
to declare. A fixed list in code, in the Annex's order; labels live in the i18n packages
(`common:allergens.<key>`), the same list in `packages/access/src/allergens.js`.
A product stores what it has (`products.allergens`, JSON) as {key: level}:
contains it is in the recipe
may_contain traces, e.g. made in the same kitchen ("may contain")
A key that isn't listed means neither. Unknown keys or levels are refused (catalog.allergen).
"""
from __future__ import annotations
import json
ALLERGENS: tuple[str, ...] = (
"gluten", "crustaceans", "eggs", "fish", "peanuts", "soybeans", "milk",
"nuts", "celery", "mustard", "sesame", "sulphites", "lupin", "molluscs",
)
LEVELS: tuple[str, ...] = ("contains", "may_contain")
def normalize(value) -> dict[str, str]:
"""A product's allergens as {key: level} in the list's order; raises XeniaError on anything else."""
from core.errors import XeniaError
if value in (None, "", {}):
return {}
if isinstance(value, str):
value = json.loads(value)
if not isinstance(value, dict):
raise XeniaError("catalog.allergen", allergen=str(value))
for key, level in value.items():
if key not in ALLERGENS or level not in LEVELS:
raise XeniaError("catalog.allergen", allergen=f"{key}: {level}")
return {k: value[k] for k in ALLERGENS if k in value}
def of(product) -> dict[str, str]:
"""What a product row stores, read safely (bad data in the column: nothing)."""
raw = getattr(product, "allergens", None)
if not raw:
return {}
try:
return normalize(raw)
except Exception:
return {}
def dump(value) -> str | None:
"""For the column: JSON, or None when there are none."""
data = normalize(value)
return json.dumps(data) if data else None