feat(backend): the menu tablet's menu (platform-v2 step 5.7a)

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>
This commit is contained in:
2026-11-17 23:44:42 +02:00
co-authored by Claude Opus 5.5
parent 6c982e3139
commit 2a6640fe5c
6 changed files with 397 additions and 6 deletions
+88
View File
@@ -0,0 +1,88 @@
"""The menu tablet (plan step 5.7a): the menu for a paired tablet, categories in the catalogue's
order with their products (a product in several categories shows in each), a sales point's live
prices, sold out crossed out or hidden, one category or all; menu changes reach the tablets."""
import pytest
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
from tests.test_media import jpeg, media_dir, up # noqa: F401 (fixture + helpers)
@pytest.fixture
def menu(db):
from models.product import Category, Product, ProductCategory
grill, drinks = Category(name="Grill", sort_order=0), Category(name="Drinks", sort_order=1)
db.add_all([grill, drinks])
db.flush()
pita = Category(name="Pita", sort_order=0, parent_id=grill.id)
db.add(pita)
db.flush()
def prod(name, cats, price, **kw):
p = Product(name=name, base_price=price, primary_category_id=cats[0].id, **kw)
db.add(p)
db.flush()
for i, c in enumerate(cats):
db.add(ProductCategory(product_id=p.id, category_id=c.id, sort_order=kw.get("sort_order", 0)))
return p
out = {"steak": prod("Steak", [grill], 12.0, description="Pork, 300 g", tags='["gluten-free"]', sort_order=1),
"souvlaki": prod("Souvlaki", [grill, pita], 3.5, sort_order=0),
"beer": prod("Beer", [drinks], 4.5),
"wine": prod("Wine", [drinks], 5.0, sold_at='["online_delivery"]'),
"old": prod("Old", [grill], 1.0, lifecycle_status="archived")}
db.commit()
out["cats"] = {"grill": grill.id, "drinks": drinks.id, "pita": pita.id}
return out
@pytest.fixture
def tablet(pair):
token, r = pair("menu_tablet", "Entrance")
assert r.status_code == 201, r.text
return {"h": {"X-Xenia-Device": token}, "id": r.json()["id"]}
def names(data):
return [(c["name"], [data["products"][str(i)]["name"] for i in c["products"]]) for c in data["categories"]]
def test_the_menu_on_a_tablet(client, owner_h, tablet, menu):
photo = up(client, owner_h, jpeg(800, 600)).json()
client.put(f"/api/products/{menu['steak'].id}", headers=owner_h, json={"media_id": photo["id"]})
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert names(data) == [("Grill", ["Souvlaki", "Steak"]), ("Pita", ["Souvlaki"]), ("Drinks", ["Beer"])] # Wine: delivery only
assert [c["depth"] for c in data["categories"]] == [0, 1, 0]
steak = data["products"][str(menu["steak"].id)]
assert (steak["price"], steak["description"], steak["tags"], steak["image"]["card"]) == (12.0, "Pork, 300 g", ["gluten-free"], photo["urls"]["card"])
assert data["config"]["view"] == "expanded" and data["config"]["search"] is True
def test_the_tablets_settings_change_the_menu(client, owner_h, tablet, menu, db):
menu["beer"].is_available = False
db.commit()
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert data["products"][str(menu["beer"].id)]["available"] is False
cfg = {"sold_out": "hide", "category": menu["cats"]["grill"], "sales_point": "none", "view": "full"}
assert client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": cfg}).status_code == 200
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert names(data) == [("Grill", ["Souvlaki", "Steak"]), ("Pita", ["Souvlaki"])]
assert {p["price"] for p in data["products"].values()} == {None} and data["config"]["view"] == "full"
client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"sales_point": "online_delivery"}})
data = client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()
assert names(data)[0][0] == "Grill" and None not in {p["price"] for p in data["products"].values()} # prices again, still only Grill
assert err(client.patch(f"/api/devices/{tablet['id']}", headers=owner_h, json={"config": {"view": "huge"}})) == "validation.device_config"
def test_menu_changes_reach_the_tablets(client, owner_h, tablet, menu, db, monkeypatch):
import routers.menu_tablet as mt
sent = []
monkeypatch.setattr("services.ws_bus.broadcast_sync", lambda kind, data, **kw: sent.append((kind, kw.get("device_ids"))))
mt.menu_changed(db)
assert sent == [("menu_tablet.changed", [tablet["id"]])]
client.put(f"/api/products/{menu['steak'].id}", headers=owner_h, json={"base_price": 13.5})
assert client.get("/api/menu-tablet/menu", headers=tablet["h"]).json()["products"][str(menu["steak"].id)]["price"] == 13.5
def test_only_menu_tablets(client, pair):
token, _ = pair("kds", "Kitchen")
assert err(client.get("/api/menu-tablet/menu", headers={"X-Xenia-Device": token})) == "device.out_of_scope"
assert err(client.get("/api/menu-tablet/menu")) == "device.not_paired"