Set once (GET/PUT /api/identity, venue.name + venue.identity); every colour field offers the venue's colours first (VenueColoursProvider in @xenia/ui); signage screens get the identity and the venue's time zone with their venue and are told on a change; logos are protected in the media library; wifiQr() for the join-the-network code. Plan 6R.14, F-111. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
194 lines
6.6 KiB
Python
194 lines
6.6 KiB
Python
"""The venue's identity (6R.14, owner 2026-11-22): set once, used by every app that shows the venue.
|
|
|
|
GET /api/identity its name, slogan, colours, logos, Wi-Fi and contact details (anyone who opens the manager)
|
|
PUT /api/identity all of it at once (core.settings.manage)
|
|
|
|
Stored as the settings `venue.name` (the name everything already shows) and `venue.identity` (JSON,
|
|
the rest). Logos are media library pictures, protected while used. Signage screens get it with
|
|
their venue (routers/signage._venue) and are told when it changes; the colours are offered first in
|
|
every colour field of signage. Other apps follow it in 6R.20.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import List, Literal, Optional
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.access.deps import require
|
|
from core.errors import XeniaError
|
|
from database import get_db
|
|
|
|
router = APIRouter()
|
|
KEY = "venue.identity"
|
|
HEX = r"^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$"
|
|
LOGOS = ("main", "light", "dark", "icon")
|
|
|
|
|
|
class _Strict(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
|
|
def _text(v, n):
|
|
v = " ".join(str(v or "").split())[:n]
|
|
return v or None
|
|
|
|
|
|
class Colour(_Strict):
|
|
"""One of the venue's colours: a name people see ("Main", "Olive") and its colour."""
|
|
name: str = Field(min_length=1, max_length=40)
|
|
colour: str = Field(pattern=HEX)
|
|
|
|
@field_validator("name")
|
|
@classmethod
|
|
def _name(cls, v):
|
|
v = _text(v, 40)
|
|
if not v:
|
|
raise ValueError("a colour has a name")
|
|
return v
|
|
|
|
@field_validator("colour")
|
|
@classmethod
|
|
def _lower(cls, v):
|
|
return v.lower()
|
|
|
|
|
|
class Logos(_Strict):
|
|
"""Pictures of the media library: the main logo, one for dark backgrounds (light), one for light
|
|
backgrounds (dark), a small square icon."""
|
|
main: Optional[int] = None
|
|
light: Optional[int] = None
|
|
dark: Optional[int] = None
|
|
icon: Optional[int] = None
|
|
|
|
|
|
class Wifi(_Strict):
|
|
ssid: Optional[str] = Field(default=None, max_length=32)
|
|
password: Optional[str] = Field(default=None, max_length=63)
|
|
security: Literal["WPA", "WEP", "nopass"] = "WPA"
|
|
hidden: bool = False
|
|
note: Optional[str] = Field(default=None, max_length=120) # "Ask us for the guest network"
|
|
|
|
@field_validator("ssid", "note")
|
|
@classmethod
|
|
def _trim(cls, v):
|
|
return (v or "").strip() or None
|
|
|
|
@field_validator("password")
|
|
@classmethod
|
|
def _pw(cls, v):
|
|
return v or None # spaces count in a password
|
|
|
|
|
|
class Contact(_Strict):
|
|
phone: Optional[str] = Field(default=None, max_length=40)
|
|
phone2: Optional[str] = Field(default=None, max_length=40)
|
|
email: Optional[str] = Field(default=None, max_length=120)
|
|
address: Optional[str] = Field(default=None, max_length=200)
|
|
website: Optional[str] = Field(default=None, max_length=200)
|
|
facebook: Optional[str] = Field(default=None, max_length=200)
|
|
instagram: Optional[str] = Field(default=None, max_length=200)
|
|
tiktok: Optional[str] = Field(default=None, max_length=200)
|
|
tripadvisor: Optional[str] = Field(default=None, max_length=200)
|
|
google: Optional[str] = Field(default=None, max_length=300) # its Google Maps / reviews link
|
|
whatsapp: Optional[str] = Field(default=None, max_length=40)
|
|
viber: Optional[str] = Field(default=None, max_length=40)
|
|
|
|
@field_validator("*")
|
|
@classmethod
|
|
def _trim(cls, v):
|
|
return (v or "").strip() or None if isinstance(v, str) or v is None else v
|
|
|
|
|
|
class Identity(_Strict):
|
|
slogan: Optional[str] = Field(default=None, max_length=120)
|
|
colours: List[Colour] = Field(default_factory=list, max_length=12)
|
|
logos: Logos = Field(default_factory=Logos)
|
|
wifi: Wifi = Field(default_factory=Wifi)
|
|
contact: Contact = Field(default_factory=Contact)
|
|
|
|
@field_validator("slogan")
|
|
@classmethod
|
|
def _slogan(cls, v):
|
|
return _text(v, 120)
|
|
|
|
|
|
class IdentityBody(Identity):
|
|
name: Optional[str] = Field(default=None, max_length=80)
|
|
|
|
|
|
def _setting(db, key: str) -> str:
|
|
from models.settings import PosSettings
|
|
row = db.query(PosSettings).filter(PosSettings.key == key).first()
|
|
return row.value if row and row.value is not None else ""
|
|
|
|
|
|
def _save(db, key: str, value: str) -> None:
|
|
from models.settings import PosSettings
|
|
row = db.query(PosSettings).filter(PosSettings.key == key).first()
|
|
if row is None:
|
|
db.add(PosSettings(key=key, value=value))
|
|
else:
|
|
row.value = value
|
|
|
|
|
|
def stored(db) -> Identity:
|
|
"""The identity as stored (an old or broken value reads as empty, never an error)."""
|
|
try:
|
|
return Identity(**json.loads(_setting(db, KEY) or "{}"))
|
|
except Exception:
|
|
return Identity()
|
|
|
|
|
|
def identity_out(db) -> dict:
|
|
"""The identity for an app: the logos as pictures ({id, name, thumb, card, full} or None)."""
|
|
from models.media import Media
|
|
from services.media import image_of
|
|
ident = stored(db).model_dump()
|
|
ident["logos"] = {k: image_of(db.get(Media, v)) if v else None for k, v in ident["logos"].items()}
|
|
return {"name": _setting(db, "venue.name") or None, **ident}
|
|
|
|
|
|
def _uses(db, media_id: int) -> list:
|
|
logos = stored(db).logos
|
|
return [{"id": 0, "name": f"identity.{k}"} for k in LOGOS if getattr(logos, k) == media_id]
|
|
|
|
|
|
def _register():
|
|
from services import media as media_store
|
|
media_store.register_usage("identity", _uses)
|
|
|
|
|
|
_register()
|
|
|
|
|
|
@router.get("")
|
|
def get_identity(db: Session = Depends(get_db), user=Depends(require("core.dashboard.access"))):
|
|
return identity_out(db)
|
|
|
|
|
|
@router.put("")
|
|
def put_identity(body: IdentityBody, db: Session = Depends(get_db), user=Depends(require("core.settings.manage"))):
|
|
from models.media import Media
|
|
for k in LOGOS:
|
|
mid = getattr(body.logos, k)
|
|
if mid is None:
|
|
continue
|
|
m = db.get(Media, mid)
|
|
if m is None:
|
|
raise XeniaError("media.not_found", media_id=mid)
|
|
if m.kind != "image":
|
|
raise XeniaError("media.not_a_picture")
|
|
if "name" in body.model_fields_set:
|
|
_save(db, "venue.name", (body.name or "").strip())
|
|
_save(db, KEY, json.dumps(Identity(**body.model_dump(exclude={"name"})).model_dump(), ensure_ascii=False))
|
|
db.commit()
|
|
try:
|
|
from routers.signage import tell_all_screens
|
|
tell_all_screens(db) # the screens show the venue's name, logo, Wi-Fi…
|
|
except Exception:
|
|
pass # signage not installed / off: nothing to tell
|
|
return identity_out(db)
|