"""Scenes, and signage schedule lines that name their screens (owner 2026-11-21). - `scenes`: named modes of the venue (party mode…) that are on or off (models/scene.py). - `signage_schedule_items` replaces the named schedules of 5.4d: each line has its playlist, its screens ([] = every screen), its priority, days, hours, dates, smart days and an optional scene. A schedule's lines become lines for the screens that had that schedule (a schedule no screen used: its lines are kept, turned off). A screen's own playlist is its default; a screen on a schedule without its own playlist gets the schedule's "otherwise" playlist as its default. The device config key `schedule` is gone. - The key `core.scenes.manage` for owners and store managers. Revision ID: 0043_scenes_and_schedule_items Revises: 0042_menu_texts_per_language Create Date: 2026-11-21 16:00:00 """ import json from alembic import op import sqlalchemy as sa from core.timeutil import UTCDateTime revision = '0043_scenes_and_schedule_items' down_revision = '0042_menu_texts_per_language' branch_labels = None depends_on = None def upgrade() -> None: op.create_table( 'scenes', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=False), sa.Column('color', sa.String(), nullable=True), sa.Column('description', sa.String(), nullable=True), sa.Column('is_active', sa.Boolean(), nullable=False), sa.Column('activated_at', UTCDateTime(), nullable=True), sa.Column('until', UTCDateTime(), nullable=True), sa.Column('sort_order', sa.Integer(), nullable=False), sa.Column('created_at', UTCDateTime(), nullable=False), sa.PrimaryKeyConstraint('id'), ) op.create_index('ix_scenes_id', 'scenes', ['id']) op.create_table( 'signage_schedule_items', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=True), sa.Column('playlist_id', sa.Integer(), sa.ForeignKey('signage_playlists.id', ondelete='CASCADE'), nullable=False), sa.Column('screen_ids', sa.JSON(), nullable=False), sa.Column('priority', sa.Integer(), nullable=False), sa.Column('enabled', sa.Boolean(), nullable=False), sa.Column('days', sa.JSON(), nullable=False), sa.Column('month_days', sa.JSON(), nullable=False), sa.Column('time_from', sa.String(), nullable=True), sa.Column('time_to', sa.String(), nullable=True), sa.Column('date_from', sa.String(), nullable=True), sa.Column('date_to', sa.String(), nullable=True), sa.Column('scene_id', sa.Integer(), sa.ForeignKey('scenes.id', ondelete='SET NULL'), nullable=True), sa.Column('position', sa.Integer(), nullable=False), sa.Column('created_at', UTCDateTime(), nullable=False), sa.Column('updated_at', UTCDateTime(), nullable=False), sa.PrimaryKeyConstraint('id'), ) op.create_index('ix_signage_schedule_items_id', 'signage_schedule_items', ['id']) op.create_index('ix_signage_schedule_items_playlist_id', 'signage_schedule_items', ['playlist_id']) op.create_index('ix_signage_schedule_items_scene_id', 'signage_schedule_items', ['scene_id']) bind = op.get_bind() playlists = {r[0] for r in bind.execute(sa.text("SELECT id FROM signage_playlists")).fetchall()} screens = [] for did, cfg in bind.execute(sa.text("SELECT id, config FROM devices WHERE app_key = 'signage'")).fetchall(): c = json.loads(cfg) if isinstance(cfg, str) and cfg else (cfg or {}) screens.append((did, c if isinstance(c, dict) else {})) from datetime import datetime, timezone stamp = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S.%f') pos = 0 for sid, name, fallback, entries in bind.execute(sa.text("SELECT id, name, fallback_playlist_id, entries FROM signage_schedules ORDER BY id")).fetchall(): entries = json.loads(entries) if isinstance(entries, str) and entries else (entries or []) on = [did for did, c in screens if c.get('schedule') == sid] for e in entries: if e.get('playlist_id') not in playlists: continue bind.execute(sa.text( "INSERT INTO signage_schedule_items (name, playlist_id, screen_ids, priority, enabled, days, month_days, time_from, time_to, " "date_from, date_to, scene_id, position, created_at, updated_at) VALUES (:name, :pl, :scr, :pri, :en, :days, '[]', :tf, :tt, :df, :dt, " "NULL, :pos, :now, :now)"), {'name': name, 'pl': e['playlist_id'], 'scr': json.dumps(on), 'pri': int(e.get('priority') or 0), 'en': bool(on), 'days': json.dumps(e.get('days') or []), 'tf': e.get('time_from'), 'tt': e.get('time_to'), 'df': e.get('date_from'), 'dt': e.get('date_to'), 'pos': pos, 'now': stamp}) pos += 1 for did, c in screens: if c.get('schedule') == sid and not c.get('playlist') and fallback in playlists: c['playlist'] = fallback for did, c in screens: if 'schedule' in c: c.pop('schedule') bind.execute(sa.text("UPDATE devices SET config = :c WHERE id = :id"), {'c': json.dumps(c) if c else None, 'id': did}) elif c.get('playlist'): bind.execute(sa.text("UPDATE devices SET config = :c WHERE id = :id"), {'c': json.dumps(c), 'id': did}) op.drop_table('signage_schedules') # the new key core.scenes.manage for owners and store managers (their role's template) bind.execute(sa.text("insert or ignore into user_permissions (user_id, key) " "select id, 'core.scenes.manage' from users where role in ('owner', 'store_manager')")) def downgrade() -> None: op.create_table( 'signage_schedules', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=False), sa.Column('fallback_playlist_id', sa.Integer(), sa.ForeignKey('signage_playlists.id', ondelete='SET NULL'), nullable=True), sa.Column('entries', sa.JSON(), nullable=False), sa.Column('created_at', UTCDateTime(), nullable=False), sa.Column('updated_at', UTCDateTime(), nullable=False), sa.PrimaryKeyConstraint('id'), ) op.drop_table('signage_schedule_items') op.drop_table('scenes') op.get_bind().execute(sa.text("delete from user_permissions where key = 'core.scenes.manage'"))