feat(backend): signage menu boards: any products, the board's own order, live prices (platform-v2 step 5.4b)
- MenuContent: select all / whole categories (with sub-categories, live: new products join) / single products, exclusions; grouped by category or not, headings; the board's own category and product order; a sales point's prices or none; photos, descriptions, badges (new, chef, offer, popular, spicy, vegetarian, vegan); sold-out crossed out or hidden; columns, items per page, seconds per page, font, colours, size, background. - services/signage_menu.resolve: the products in that order with customer-facing names, prices from pos_cart.unit_prices (what the counter charges right now), availability, card-size photos. GET /api/signage/screen includes each menu slide's `menu`. - Every request that changes the menu (the online menu's paths) also sends signage.changed to the screens with a menu board (main.py, off the loop). - tests: test_signage_menu.py (6); 515 passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
"""The menu board's content (plan step 5.4b): which products a menu slide lists, in its own order,
|
||||
with live prices and availability.
|
||||
|
||||
A menu slide picks products three ways, combined (routers/signage.py MenuContent):
|
||||
select = "all" every active product
|
||||
category_ids whole categories (and their sub-categories): products added to them later join
|
||||
product_ids single products, from any category
|
||||
minus `exclude_ids`. System products (the delivery fee line) and archived products never show.
|
||||
With a `sales_point`, products not sold there are left out and prices are that sales point's,
|
||||
right now (services/pos_cart.unit_prices, the same rules the counter uses); without one there
|
||||
are no prices.
|
||||
|
||||
The order is the board's own (`category_order`, `product_order`), never the waiters' or the
|
||||
counter's: listed ids first in that order, the rest after them in the catalogue's order.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from core import sales_points
|
||||
|
||||
|
||||
def _descendants(cats_by_parent: dict, roots: list[int]) -> set[int]:
|
||||
out, stack = set(), list(roots)
|
||||
while stack:
|
||||
c = stack.pop()
|
||||
if c in out:
|
||||
continue
|
||||
out.add(c)
|
||||
stack.extend(cats_by_parent.get(c, []))
|
||||
return out
|
||||
|
||||
|
||||
def _rank(order: list[int]):
|
||||
pos = {v: i for i, v in enumerate(order or [])}
|
||||
return lambda v: (0, pos[v]) if v in pos else (1, 0)
|
||||
|
||||
|
||||
def resolve(db, content: dict, now=None) -> dict:
|
||||
"""{"groups": [{"category": {id, name} | None, "items": [...]}], "count": n} for a menu slide."""
|
||||
from models.product import Category, Product, ProductCategory
|
||||
from services import media as media_store
|
||||
from services.pos_cart import unit_prices
|
||||
|
||||
cats = db.query(Category).all()
|
||||
by_id = {c.id: c for c in cats}
|
||||
by_parent: dict = {}
|
||||
for c in cats:
|
||||
by_parent.setdefault(c.parent_id, []).append(c.id)
|
||||
|
||||
q = db.query(Product).filter(Product.lifecycle_status == "active", Product.system_key.is_(None))
|
||||
if content.get("select") == "all":
|
||||
products = q.all()
|
||||
else:
|
||||
wanted_cats = _descendants(by_parent, [i for i in content.get("category_ids") or [] if i in by_id])
|
||||
in_cats = {pid for (pid,) in db.query(ProductCategory.product_id).filter(ProductCategory.category_id.in_(wanted_cats))} if wanted_cats else set()
|
||||
ids = in_cats | set(content.get("product_ids") or [])
|
||||
products = q.filter(Product.id.in_(ids)).all() if ids else []
|
||||
excluded = set(content.get("exclude_ids") or [])
|
||||
products = [p for p in products if p.id not in excluded]
|
||||
|
||||
point = content.get("sales_point")
|
||||
if point:
|
||||
products = [p for p in products if sales_points.is_sold_at(p, point)]
|
||||
if content.get("sold_out") == "hide":
|
||||
products = [p for p in products if p.is_available]
|
||||
|
||||
# the catalogue's order inside each product's main category (the fallback after the board's own)
|
||||
member_order = {(m.category_id, m.product_id): m.sort_order for m in db.query(ProductCategory).all()}
|
||||
cat_rank = _rank(content.get("category_order"))
|
||||
prod_rank = _rank(content.get("product_order"))
|
||||
|
||||
def cat_key(cid):
|
||||
c = by_id.get(cid)
|
||||
return (cat_rank(cid), c.sort_order if c else 0, c.name.lower() if c else "")
|
||||
|
||||
def prod_key(p):
|
||||
return (prod_rank(p.id), member_order.get((p.primary_category_id, p.id), p.sort_order or 0), (p.name or "").lower())
|
||||
|
||||
prices = dict(zip([p.id for p in products], unit_prices(db, products, point, now=now))) if point and products else {}
|
||||
badges = {int(k): v for k, v in (content.get("badges") or {}).items() if str(k).isdigit()}
|
||||
|
||||
def item(p):
|
||||
image = media_store.image_of(p.media) if content.get("show_photos") else None
|
||||
return {"id": p.id, "name": p.digital_name or p.name,
|
||||
"description": (p.digital_description or p.description or None) if content.get("show_descriptions") else None,
|
||||
"price": round(prices[p.id], 2) if p.id in prices else None,
|
||||
"available": bool(p.is_available), "image": image["card"] if image else None,
|
||||
"badge": badges.get(p.id)}
|
||||
|
||||
if content.get("group_by_category", True):
|
||||
groups: dict = {}
|
||||
for p in products:
|
||||
groups.setdefault(p.primary_category_id, []).append(p)
|
||||
out = [{"category": {"id": cid, "name": by_id[cid].name} if cid in by_id else None,
|
||||
"items": [item(p) for p in sorted(ps, key=prod_key)]}
|
||||
for cid, ps in sorted(groups.items(), key=lambda kv: cat_key(kv[0]))]
|
||||
else:
|
||||
ordered = sorted(products, key=lambda p: (prod_rank(p.id), cat_key(p.primary_category_id), prod_key(p)))
|
||||
out = [{"category": None, "items": [item(p) for p in ordered]}] if ordered else []
|
||||
return {"groups": out, "count": len(products)}
|
||||
Reference in New Issue
Block a user