GET /api/menu-tablet/menu for a paired menu tablet: categories in the catalogue's order (sub-categories after their parent, one category and its sub-categories when the tablet is set so), each with its products in its own order (a product in several categories shows in each), the products once with name, description, tags, photo, the price of the tablet's sales point (or none) and availability; sold-out hidden when set; the venue's name and the tablet's settings. Device config: view, photos, descriptions, group, theme, prices of, sold out, category, search, languages, idle reset (texts in both languages). Menu changes send menu_tablet.changed to every tablet (main.py, with signage). tests: test_menu_tablet.py (4); 531 passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
5.0 KiB
Python
118 lines
5.0 KiB
Python
"""The menu tablet (plan step 5.7a): the venue's menu on a tablet, for staff ("how much is the
|
|
steak?") and for guests (a menu at the entrance, later a digital menu at the table). Just the
|
|
products and their categories: no ordering, no tables, no sign-in.
|
|
|
|
GET /api/menu-tablet/menu (a paired menu tablet) the categories in the catalogue's order
|
|
(sub-categories after their parent), each with its products in its
|
|
own order (a product listed in several categories shows in each),
|
|
the products once with name, description, tags, photo, the price
|
|
of the tablet's sales point and whether it is available; the
|
|
tablet's settings (device config) and the venue's name.
|
|
|
|
How it looks is the tablet's device config (modules/menu_tablet/manifest.py): view, photos,
|
|
descriptions, grouping, whose prices, sold-out style, search, one category or all, languages,
|
|
idle reset, light / dark. Any menu change sends `menu_tablet.changed` to every tablet (main.py).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core import sales_points
|
|
from core.access.deps import require_device
|
|
from database import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _config(dev) -> dict:
|
|
from core.modules.device_config import effective, for_app
|
|
return effective(for_app("menu_tablet"), dev.config)
|
|
|
|
|
|
def tablets(db) -> list[int]:
|
|
from models.device import Device
|
|
return [i for (i,) in db.query(Device.id).filter(Device.app_key == "menu_tablet", Device.revoked_at.is_(None))]
|
|
|
|
|
|
def menu_changed(db) -> None:
|
|
"""Products, categories or prices changed (main.py): every tablet reads its menu again."""
|
|
from services.ws_bus import broadcast_sync
|
|
ids = tablets(db)
|
|
if ids:
|
|
broadcast_sync("menu_tablet.changed", {}, device_ids=ids)
|
|
|
|
|
|
def _tags(p) -> list[str]:
|
|
raw = getattr(p, "tags", None)
|
|
if isinstance(raw, str):
|
|
try:
|
|
raw = json.loads(raw)
|
|
except ValueError:
|
|
raw = []
|
|
return [t for t in (raw or []) if isinstance(t, str)]
|
|
|
|
|
|
@router.get("/menu")
|
|
def menu(db: Session = Depends(get_db), dev=Depends(require_device("menu_tablet"))):
|
|
from models.product import Category, Product, ProductCategory
|
|
from models.settings import PosSettings
|
|
from services import media as media_store
|
|
from services.pos_cart import unit_prices
|
|
cfg = _config(dev)
|
|
cats = db.query(Category).order_by(Category.sort_order, Category.name).all()
|
|
by_parent: dict = {}
|
|
for c in cats:
|
|
by_parent.setdefault(c.parent_id, []).append(c)
|
|
ordered = [] # parents, each followed by its sub-categories
|
|
|
|
def walk(parent_id, depth):
|
|
for c in by_parent.get(parent_id, []):
|
|
ordered.append((c, depth))
|
|
walk(c.id, depth + 1)
|
|
walk(None, 0)
|
|
if cfg.get("category"):
|
|
root = next(((c, d) for c, d in ordered if c.id == cfg["category"]), None)
|
|
keep = set()
|
|
if root:
|
|
stack = [root[0].id]
|
|
while stack:
|
|
cid = stack.pop()
|
|
keep.add(cid)
|
|
stack.extend(x.id for x in by_parent.get(cid, []))
|
|
ordered = [(c, d) for c, d in ordered if c.id in keep]
|
|
|
|
point = None if cfg["sales_point"] == "none" else cfg["sales_point"]
|
|
products = db.query(Product).filter(Product.lifecycle_status == "active", Product.system_key.is_(None)).all()
|
|
if point:
|
|
products = [p for p in products if sales_points.is_sold_at(p, point)]
|
|
if cfg["sold_out"] == "hide":
|
|
products = [p for p in products if p.is_available]
|
|
by_id = {p.id: p for p in products}
|
|
members = db.query(ProductCategory).order_by(ProductCategory.sort_order).all()
|
|
in_cat: dict = {}
|
|
for m in members:
|
|
if m.product_id in by_id:
|
|
in_cat.setdefault(m.category_id, []).append(m.product_id)
|
|
|
|
out_cats, used = [], set()
|
|
for c, depth in ordered:
|
|
ids = in_cat.get(c.id, [])
|
|
has_sub = any(in_cat.get(x.id) for x in by_parent.get(c.id, []))
|
|
if ids or has_sub:
|
|
out_cats.append({"id": c.id, "name": c.name, "parent_id": c.parent_id, "depth": depth, "products": ids})
|
|
used.update(ids)
|
|
shown = [by_id[i] for i in by_id if i in used]
|
|
prices = dict(zip([p.id for p in shown], unit_prices(db, shown, point))) if point and shown else {}
|
|
name_row = db.query(PosSettings).filter(PosSettings.key == "venue.name").first()
|
|
return {
|
|
"venue": {"name": (name_row.value if name_row else "") or None},
|
|
"config": cfg,
|
|
"categories": out_cats,
|
|
"products": {str(p.id): {"id": p.id, "name": p.name, "description": p.description or None, "tags": _tags(p),
|
|
"price": round(prices[p.id], 2) if p.id in prices else None, "available": bool(p.is_available),
|
|
"image": media_store.image_of(p.media)} for p in shown},
|
|
}
|