feat(cloud): the QR menu at the table, ordering only with a table's QR code

Plan phase 3S, step S.7.
- services/qr.py: the snapshot's `qr` {ordering, tables: [{id, label, token_hash}]} (sha256
  of each table's token; the cloud never holds the tokens). A table order (dine_in) needs
  the venue's ordering on, the site's menu_mode "order" and a valid table token: otherwise
  order.service_unavailable / 403 order.table_invalid (a forged, old or typed-in table is
  refused). The order keeps the table's id and label (online_orders.table_id, migration 0006).
- Public menu: ?t=<token> → `table` {id, label} or `table_invalid`; `table_ordering`; `qr`
  never leaves the cloud.
- Heartbeat returns `apps_url` (new setting PUBLIC_APPS_URL: where /menu/ and /order/ are
  served) so sites can print table QR codes that open the right address.
- Menu app: the table from its QR code (kept for the visit), a "Table 5" banner, ordering
  only at a real table (otherwise read-only, with a notice for an invalid code); checkout
  without a typed table number; prices are the QR menu's (sales point qr_table — it showed
  in-store prices since S.1 removed digital_price) and only products sold there; the
  confirmation page for table orders (in the kitchen); the delivery/takeaway leftovers
  removed (they have their own app since S.4); test ids for e2e.
- Tests: tests/test_qr.py (3); test_checkout's typed-table case moved there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 01:26:33 +02:00
co-authored by Claude Opus 5.5
parent 3459c80611
commit 2835b5dc68
17 changed files with 245 additions and 74 deletions
+35
View File
@@ -0,0 +1,35 @@
"""The QR menu at the table (plan phase 3S, step S.7).
Each table's QR code carries a random token the venue's server made for that table
(client-services services/table_qr.py). The menu snapshot brings, under `qr`:
{ordering: bool, tables: [{id, label, token_hash}]} token_hash = sha256(token) hex
so the cloud never holds the tokens themselves, and a token is checked by its hash. A new
QR code for a table (the venue "rotates" it) makes the old one useless at the next menu push.
Ordering at the table needs the venue's `ordering`, the site's menu_mode "order" (sysadmin)
and a valid token; otherwise the QR menu is read-only. The public menu never shows `qr`.
"""
from __future__ import annotations
import hashlib
import hmac
def token_hash(token: str) -> str:
return hashlib.sha256(token.encode("utf-8")).hexdigest()
def table_for(snapshot: dict, token: str | None) -> dict | None:
"""{id, label} of the table this token belongs to, or None."""
if not token or not isinstance(token, str) or len(token) > 100:
return None
wanted = token_hash(token)
for t in (snapshot.get("qr") or {}).get("tables") or []:
if isinstance(t, dict) and hmac.compare_digest(str(t.get("token_hash") or ""), wanted):
return {"id": t.get("id"), "label": t.get("label")}
return None
def ordering_on(snapshot: dict, site) -> bool:
return site.menu_mode == "order" and bool((snapshot.get("qr") or {}).get("ordering"))