"""display_looks: the customer display's idle screens, named, one per display (plan step 5.3). The venue's one idle screen (setting customer_display.idle) becomes the look "Main", the default; its pictures (files in data/display_media, by name) are processed into the media library and the look points at them by id. Missing or unreadable files are logged and left out. The old folder stays on disk. The setting row goes (its SettingDef is gone too). Revision ID: 0028_display_looks Revises: 0027_product_media Create Date: 2026-11-17 18:00:00 """ import json import logging import os from alembic import op import sqlalchemy as sa from core.timeutil import UTCDateTime revision = '0028_display_looks' down_revision = '0027_product_media' branch_labels = None depends_on = None log = logging.getLogger("alembic.0028") OLD_DIR = os.environ.get("XENIA_DISPLAY_MEDIA_DIR", "/app/data/display_media") def upgrade() -> None: op.create_table('display_looks', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(), nullable=False), sa.Column('is_default', sa.Boolean(), nullable=False), sa.Column('config', sa.JSON(), nullable=False), sa.Column('created_at', UTCDateTime(), nullable=False), sa.Column('updated_at', UTCDateTime(), nullable=False), sa.PrimaryKeyConstraint('id') ) with op.batch_alter_table('display_looks', schema=None) as batch_op: batch_op.create_index(batch_op.f('ix_display_looks_id'), ['id'], unique=False) conn = op.get_bind() raw = conn.execute(sa.text("SELECT value FROM pos_settings WHERE key = 'customer_display.idle'")).scalar() try: cfg = json.loads(raw or "{}") or {} except ValueError: cfg = {} if not isinstance(cfg, dict): cfg = {} names = [n for n in [cfg.get("logo"), cfg.get("image"), *(cfg.get("slides") or [])] if isinstance(n, str) and n] ids = _move_pictures(conn, names) cfg["logo"] = ids.get(cfg.get("logo")) cfg["image"] = ids.get(cfg.get("image")) cfg["slides"] = [ids[n] for n in (cfg.get("slides") or []) if n in ids] from core.timeutil import DB_FORMAT, utcnow now = utcnow().strftime(DB_FORMAT) conn.execute(sa.text("INSERT INTO display_looks (name, is_default, config, created_at, updated_at) VALUES ('Main', 1, :c, :t, :t)"), {"c": json.dumps(cfg), "t": now}) conn.execute(sa.text("DELETE FROM pos_settings WHERE key = 'customer_display.idle'")) def _move_pictures(conn, names) -> dict: """{old file name: media id} for the pictures that made it into the library.""" if not names: return {} from core.timeutil import DB_FORMAT, utcnow from services import media as store out = {} for i, name in enumerate(dict.fromkeys(names), 1): path = os.path.join(OLD_DIR, os.path.basename(name)) if not os.path.isfile(path): log.warning("customer display picture %s not found, left out", path) continue try: sha, size, info = store.ingest_path(path) except Exception as e: log.warning("customer display picture %s not moved (%s)", path, getattr(e, "code", e)) continue mid = conn.execute(sa.text("SELECT id FROM media WHERE sha256 = :s"), {"s": sha}).scalar() if mid is None: if not info: store.delete_files(sha) sha, size, info = store.ingest_path(path) conn.execute(sa.text( "INSERT INTO media (kind, name, original_name, content_type, sha256, bytes, width, height, duration_ms, created_at) " "VALUES (:kind, :name, :orig, :ctype, :sha, :bytes, :w, :h, :d, :at)"), {"kind": info["kind"], "name": f"Customer display {i}", "orig": os.path.basename(path), "ctype": info["content_type"], "sha": sha, "bytes": size, "w": info.get("width"), "h": info.get("height"), "d": info.get("duration_ms"), "at": utcnow().strftime(DB_FORMAT)}) mid = conn.execute(sa.text("SELECT id FROM media WHERE sha256 = :s"), {"s": sha}).scalar() out[name] = mid return out def downgrade() -> None: with op.batch_alter_table('display_looks', schema=None) as batch_op: batch_op.drop_index(batch_op.f('ix_display_looks_id')) op.drop_table('display_looks')