feat(signage,scenes): schedule lines with screens and smart days, screens' defaults, horizontal timeline, scenes
- Scenes (Alembic 0043, models/scene.py, /api/scenes, core.scenes.manage): named modes on or off (optionally until a time); price rules / deals get the condition "a scene is on"; signage lines can play only during a scene; turning one on tells every screen and pushes the online menu. - Signage schedules are lines (signage_schedule_items): playlist, screens ([] = every screen), priority, days, smart days (1st / last day, first / last week, Greek public holidays or not: core/calendar_days.py, mirrored in @xenia/signage), hours, dates, scene. No line → the screen's default playlist. 0043 turns the old named schedules into lines for their screens. - Screens: default playlist, TV shape, turn; what plays now and why. - Manager: tabs Screens · Playlists · Schedules · Slides (placeholder) · Message and weather; schedule lines with playlist / screens / priority / delete on one line; the playlist editor's horizontal timeline of fixed cards, the slide's basics beside its preview (one height), then its content; Management → Scenes; the scene condition in pricing. - Tests: backend (lines, smart days, scenes, prices), player logic, e2e. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -713,7 +713,7 @@ async def _wait_for_site_id(max_seconds: int = 30) -> None:
|
||||
# not at the next 5-minute tick (phase 3S): request_menu_push() from any thread wakes the
|
||||
# worker; edits in a row within MENU_SETTLE_SECONDS become one push.
|
||||
MENU_SETTLE_SECONDS = 5
|
||||
MENU_PATHS = ("/api/products", "/api/pricing", "/api/connect/fees", "/api/data-transfer/import",
|
||||
MENU_PATHS = ("/api/products", "/api/pricing", "/api/scenes", "/api/connect/fees", "/api/data-transfer/import",
|
||||
"/api/connect/qr", "/api/tables") # S.7: the tables' QR codes travel with the menu
|
||||
_menu_wake: "asyncio.Event | None" = None
|
||||
_menu_loop = None
|
||||
|
||||
@@ -227,6 +227,8 @@ def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | No
|
||||
from services.pricing import OrderContext, build_active_price_group_ids, resolve_price
|
||||
now = utcnow()
|
||||
active = build_active_price_group_ids(db, now)
|
||||
from services.pricing import build_active_scene_ids
|
||||
scenes = build_active_scene_ids(db, now)
|
||||
channel = point or point_for(order_type)
|
||||
cart = list(existing_cart or [])
|
||||
out = []
|
||||
@@ -236,7 +238,7 @@ def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | No
|
||||
raise _bad(n, "not_sold_here")
|
||||
here = {"product_id": p.id, "category_id": p.primary_category_id, "category_ids": p.category_ids,
|
||||
"quantity": line.quantity, "unit_price": p.base_price + line.extra}
|
||||
ctx = OrderContext(channel=channel, now=now, cart_items=cart + [here], active_price_group_ids=active)
|
||||
ctx = OrderContext(channel=channel, now=now, cart_items=cart + [here], active_price_group_ids=active, active_scene_ids=scenes)
|
||||
unit, events = resolve_price(p, line.quantity, line.extra, ctx, db)
|
||||
out.append((unit, events))
|
||||
cart.append({**here, "unit_price": unit})
|
||||
@@ -250,11 +252,13 @@ def unit_prices(db, products, point: str, now=None) -> list[float]:
|
||||
from services.pricing import OrderContext, build_active_price_group_ids, resolve_price
|
||||
now = now or utcnow()
|
||||
active = build_active_price_group_ids(db, now)
|
||||
from services.pricing import build_active_scene_ids
|
||||
scenes = build_active_scene_ids(db, now)
|
||||
out = []
|
||||
for p in products:
|
||||
here = {"product_id": p.id, "category_id": p.primary_category_id, "category_ids": p.category_ids,
|
||||
"quantity": 1, "unit_price": p.base_price}
|
||||
ctx = OrderContext(channel=point, now=now, cart_items=[here], active_price_group_ids=active)
|
||||
ctx = OrderContext(channel=point, now=now, cart_items=[here], active_price_group_ids=active, active_scene_ids=scenes)
|
||||
out.append(resolve_price(p, 1, 0.0, ctx, db)[0])
|
||||
return out
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ class OrderContext:
|
||||
cart_items: list # list of {product_id, category_id (primary), category_ids, quantity, unit_price}
|
||||
active_price_group_ids: set # set of PriceGroup.id that are currently active
|
||||
user_tier: Optional[str] = None # 'bronze'|'silver'|'gold' — placeholder
|
||||
active_scene_ids: set = field(default_factory=set) # the scenes on now (services/scenes.py, 2026-11-21)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -144,6 +145,11 @@ def _eval_condition(ctype: str, params: dict, ctx: OrderContext,
|
||||
passed = pgid in ctx.active_price_group_ids
|
||||
return passed, {"price_group_active": passed}
|
||||
|
||||
if ctype == "scene_active":
|
||||
# a scene is on (owner 2026-11-21): party mode, match night…
|
||||
passed = params.get("scene_id") in ctx.active_scene_ids
|
||||
return passed, {"scene_active": passed}
|
||||
|
||||
if ctype == "user_tier":
|
||||
passed = ctx.user_tier in params.get("tiers", [])
|
||||
return passed, {"user_tier": ctx.user_tier}
|
||||
@@ -236,6 +242,12 @@ def _price_group_is_active(pg, now: datetime) -> bool:
|
||||
return bool(pg.is_active)
|
||||
|
||||
|
||||
def build_active_scene_ids(db: Session, now: datetime) -> set:
|
||||
"""The scenes on at `now` (services/scenes.py), for the condition "a scene is on"."""
|
||||
from services.scenes import active_ids
|
||||
return active_ids(db, now)
|
||||
|
||||
|
||||
def build_active_price_group_ids(db: Session, now: datetime) -> set:
|
||||
"""Fetch all price groups once per request and return active IDs."""
|
||||
from models.pricing import PriceGroup
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
"""Scenes (owner 2026-11-21, models/scene.py): which are on now, and telling what follows them.
|
||||
|
||||
A scene is on when it was turned on and its `until` (if any) has not passed. When scenes change,
|
||||
the signage screens are told (their schedules may pick another playlist) and the prices are
|
||||
recomputed by whoever asks next (price rules read `active_ids` through the pricing context).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from core.timeutil import utcnow
|
||||
|
||||
|
||||
def _aware(dt):
|
||||
from datetime import timezone
|
||||
return dt if dt is None or dt.tzinfo else dt.replace(tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def is_on(scene, now: datetime | None = None) -> bool:
|
||||
now = now or utcnow()
|
||||
return bool(scene.is_active) and (scene.until is None or _aware(scene.until) > _aware(now))
|
||||
|
||||
|
||||
def active_ids(db, now: datetime | None = None) -> set[int]:
|
||||
from models.scene import Scene
|
||||
return {s.id for s in db.query(Scene).filter(Scene.is_active.is_(True)) if is_on(s, now)}
|
||||
|
||||
|
||||
def out(scene, now: datetime | None = None) -> dict:
|
||||
return {"id": scene.id, "name": scene.name, "color": scene.color, "description": scene.description,
|
||||
"active": is_on(scene, now), "activated_at": scene.activated_at, "until": scene.until, "sort_order": scene.sort_order}
|
||||
|
||||
|
||||
def changed(db) -> None:
|
||||
"""Scenes changed: the signage screens look again; the dashboards refresh their scene lists."""
|
||||
from services.ws_bus import broadcast_sync
|
||||
broadcast_sync("scenes.changed", {})
|
||||
try:
|
||||
from routers.signage import tell_all_screens
|
||||
tell_all_screens(db)
|
||||
except Exception: # signage off or not loaded: nothing to tell
|
||||
pass
|
||||
@@ -1,13 +1,17 @@
|
||||
"""Signage schedules (plan step 5.4d): which playlist a screen plays when.
|
||||
"""Signage schedules (plan step 5.4d; lines with their screens and scenes, owner 2026-11-21): which
|
||||
playlist a screen plays when.
|
||||
|
||||
A schedule is a list of entries {playlist_id, days, time_from, time_to, date_from, date_to,
|
||||
priority} and a fallback playlist. At a moment (venue time) the entries that match are:
|
||||
A screen's lines are the schedule lines (models/signage.py SignageScheduleItem) that are on and
|
||||
name it (or no screen = every screen). At a moment (venue time) a line matches when:
|
||||
- its days include today (empty = every day),
|
||||
- its smart days hold (core/calendar_days.py: 1st / last day, first / last week, public
|
||||
holidays or not; empty = every day),
|
||||
- the time is inside [time_from, time_to) (empty = all day; a window that passes midnight,
|
||||
e.g. 22:00-02:00, counts the after-midnight part for the day it started),
|
||||
- today is inside [date_from, date_to] (empty = no limit).
|
||||
- today is inside [date_from, date_to] (empty = no limit),
|
||||
- its scene (if any) is on.
|
||||
The winner is the highest priority, then the most specific (dates > days > hours), then the
|
||||
later entry in the list. No match → the fallback (None = a blank screen).
|
||||
later line. No match → the screen's default playlist (None = a blank screen).
|
||||
|
||||
`pick()` is pure (unit-tested; the player in 5.5a does the same in JavaScript so a screen
|
||||
switches by itself while the server is down). `schedule_worker` tells a screen when its pick
|
||||
@@ -30,12 +34,15 @@ def _minutes(hhmm: str | None) -> int | None:
|
||||
|
||||
|
||||
def _date_ok(e: dict, day) -> bool:
|
||||
from core.calendar_days import month_days_ok
|
||||
d = day.isoformat()
|
||||
return (not e.get("date_from") or d >= e["date_from"]) and (not e.get("date_to") or d <= e["date_to"])
|
||||
return (not e.get("date_from") or d >= e["date_from"]) and (not e.get("date_to") or d <= e["date_to"]) and month_days_ok(e.get("month_days"), day)
|
||||
|
||||
|
||||
def matches(e: dict, local: datetime) -> bool:
|
||||
"""Does entry `e` cover this venue-local moment?"""
|
||||
def matches(e: dict, local: datetime, scenes=None) -> bool:
|
||||
"""Does entry `e` cover this venue-local moment (with these scenes on)?"""
|
||||
if e.get("scene_id") is not None and e["scene_id"] not in (scenes or ()):
|
||||
return False
|
||||
days = e.get("days") or []
|
||||
start, end = _minutes(e.get("time_from")), _minutes(e.get("time_to"))
|
||||
now = local.hour * 60 + local.minute
|
||||
@@ -55,15 +62,15 @@ def matches(e: dict, local: datetime) -> bool:
|
||||
|
||||
|
||||
def _specific(e: dict) -> int:
|
||||
return (4 if (e.get("date_from") or e.get("date_to")) else 0) + (2 if e.get("days") else 0) + \
|
||||
return (4 if (e.get("date_from") or e.get("date_to")) else 0) + (2 if (e.get("days") or e.get("month_days")) else 0) + \
|
||||
(1 if (e.get("time_from") or e.get("time_to")) else 0)
|
||||
|
||||
|
||||
def pick(entries: list[dict], fallback: int | None, local: datetime) -> int | None:
|
||||
def pick(entries: list[dict], fallback: int | None, local: datetime, scenes=None) -> int | None:
|
||||
"""The playlist id that plays at `local` (venue time)."""
|
||||
best, best_key = None, None
|
||||
for i, e in enumerate(entries or []):
|
||||
if e.get("playlist_id") is None or not matches(e, local):
|
||||
if e.get("playlist_id") is None or not matches(e, local, scenes):
|
||||
continue
|
||||
key = (e.get("priority") or 0, _specific(e), i)
|
||||
if best_key is None or key > best_key:
|
||||
@@ -76,20 +83,43 @@ def pick(entries: list[dict], fallback: int | None, local: datetime) -> int | No
|
||||
_playing: dict[int, int | None] = {} # screen id → the playlist it was told about
|
||||
|
||||
|
||||
def due_changes(db, local: datetime | None = None) -> list[int]:
|
||||
"""The screens on a schedule whose pick is different from the last one seen (and remember it)."""
|
||||
ENTRY_KEYS = ("playlist_id", "days", "month_days", "time_from", "time_to", "date_from", "date_to", "priority", "scene_id")
|
||||
|
||||
|
||||
def entry_of(item) -> dict:
|
||||
"""A schedule line as the screens and pick() read it."""
|
||||
return {k: getattr(item, k) for k in ENTRY_KEYS}
|
||||
|
||||
|
||||
def lines_for(db, screen_id: int) -> list:
|
||||
"""The schedule lines that are on and name this screen (or no screen), in order."""
|
||||
from models.signage import SignageScheduleItem
|
||||
return [i for i in db.query(SignageScheduleItem).filter(SignageScheduleItem.enabled.is_(True))
|
||||
.order_by(SignageScheduleItem.position, SignageScheduleItem.id) if not i.screen_ids or screen_id in i.screen_ids]
|
||||
|
||||
|
||||
def now_for(db, dev, local: datetime | None = None, scenes=None) -> tuple[int | None, bool]:
|
||||
"""(the playlist a screen plays now, whether a schedule line picked it)."""
|
||||
from core.timeutil import venue_now
|
||||
from models.signage import SignageSchedule
|
||||
from routers.signage import _screen_config, _screens
|
||||
from routers.signage import _screen_config
|
||||
from services.scenes import active_ids
|
||||
local = local or venue_now()
|
||||
scenes = active_ids(db) if scenes is None else scenes
|
||||
default = _screen_config(dev).get("playlist")
|
||||
got = pick([entry_of(i) for i in lines_for(db, dev.id)], None, local, scenes)
|
||||
return (got, True) if got is not None else (default, False)
|
||||
|
||||
|
||||
def due_changes(db, local: datetime | None = None) -> list[int]:
|
||||
"""The screens whose pick is different from the last one seen (and remember it)."""
|
||||
from core.timeutil import venue_now
|
||||
from routers.signage import _screens
|
||||
from services.scenes import active_ids
|
||||
local = local or venue_now()
|
||||
scenes = active_ids(db)
|
||||
changed = []
|
||||
for d in _screens(db):
|
||||
sid = _screen_config(d).get("schedule")
|
||||
if not sid:
|
||||
_playing.pop(d.id, None)
|
||||
continue
|
||||
s = db.get(SignageSchedule, sid)
|
||||
now = pick(s.entries or [], s.fallback_playlist_id, local) if s else None
|
||||
now = now_for(db, d, local, scenes)[0]
|
||||
if d.id in _playing and _playing[d.id] != now:
|
||||
changed.append(d.id)
|
||||
_playing[d.id] = now
|
||||
|
||||
Reference in New Issue
Block a user