feat(manager): the venue's identity (Settings -> Identity): name, slogan, named colours, logos, guest Wi-Fi, contact details
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>
This commit is contained in:
@@ -33,6 +33,7 @@ MODULE = Module(
|
||||
SettingDef('venue.language', 'en', 'Default language (en | el) for staff without their own language, and for printed reports/receipts and tickets of zones without one.', type='str'),
|
||||
SettingDef('venue.timezone', 'Europe/Athens', 'IANA time zone of the venue (e.g. Europe/Athens). All times are shown in it; DST is automatic. Applies immediately.', type='str'),
|
||||
SettingDef('venue.name', '', 'Venue name shown on screens and tickets.', type='str'),
|
||||
SettingDef('venue.identity', '{}', "The venue's identity (6R.14): slogan, colours, logos, Wi-Fi, contact details (JSON; Settings > Identity, routers/identity.py).", type='json'),
|
||||
SettingDef('venue.theme', 'amber', "Accent colour of the staff apps (the waiter's phones first, plan step 4.6): 'teal' | 'amber' | 'blue' | 'violet' | 'rose'", type='enum', choices=('teal', 'amber', 'blue', 'violet', 'rose')),
|
||||
SettingDef('venue.type', '', 'Kind of venue (restaurant, cafe, bar...), chosen in the setup wizard.', type='str'),
|
||||
SettingDef('venue.currency', 'EUR', 'ISO 4217 currency of all prices.', type='str'),
|
||||
@@ -50,6 +51,7 @@ MODULE = Module(
|
||||
Mount('routers.access', '/api/access'),
|
||||
Mount('routers.notifications', '/api/platform/notifications'),
|
||||
Mount('routers.scenes', '/api/scenes'),
|
||||
Mount('routers.identity', '/api/identity'),
|
||||
Mount('core.modules.api', '/api/platform'),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
"""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)
|
||||
@@ -1488,10 +1488,15 @@ def preview_venue(db: Session = Depends(get_db), user=Depends(require(KEY))):
|
||||
|
||||
def _venue(db) -> dict:
|
||||
"""What the widgets need of the venue: its name, the message of the day, the weather (None = hide
|
||||
it: no internet, no place, too old) and its badges (owner review 5)."""
|
||||
it: no internet, no place, too old) and its badges (owner review 5); its identity (6R.14: slogan,
|
||||
colours, logos, Wi-Fi, contact) and time zone."""
|
||||
from core.timeutil import venue_tz
|
||||
from routers.identity import identity_out
|
||||
from services import signage_badges, weather
|
||||
ident = identity_out(db)
|
||||
ident.pop("name", None)
|
||||
return {"name": _setting(db, "venue.name") or None, "message": _setting(db, MESSAGE) or None,
|
||||
"weather": weather.current(db), "badges": signage_badges.load(db)}
|
||||
"weather": weather.current(db), "badges": signage_badges.load(db), "identity": ident, "timezone": venue_tz().key}
|
||||
|
||||
|
||||
class SlidePreviewBody(BaseModel):
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
"""The venue's identity (6R.14): name, slogan, colours, logos, Wi-Fi and contact saved together,
|
||||
logos protected in the media library, signage screens told and given it with their venue."""
|
||||
from tests.test_live import err, owner_h, pair # noqa: F401 (fixtures)
|
||||
from tests.test_media import jpeg, media_dir, up # noqa: F401 (fixture + helpers)
|
||||
from tests.test_signage import screens, sent # noqa: F401 (fixtures)
|
||||
|
||||
|
||||
def test_empty_at_first(client, owner_h):
|
||||
r = client.get("/api/identity", headers=owner_h)
|
||||
assert r.status_code == 200
|
||||
j = r.json()
|
||||
assert j["colours"] == [] and j["logos"] == {"main": None, "light": None, "dark": None, "icon": None}
|
||||
assert j["wifi"]["security"] == "WPA" and j["contact"]["phone"] is None
|
||||
|
||||
|
||||
def test_saved_together_and_shown_to_the_screens(client, owner_h, screens, sent, media_dir):
|
||||
logo = up(client, owner_h, jpeg(400, 200)).json()
|
||||
body = {"name": " Taverna Xenia ", "slogan": "Since 1987", "colours": [{"name": "Main", "colour": "#1E3A8A"}, {"name": "Olive", "colour": "#556b2f80"}],
|
||||
"logos": {"main": logo["id"]}, "wifi": {"ssid": "Xenia Guests", "password": "kalimera 2026", "security": "WPA"},
|
||||
"contact": {"phone": " 2310 123456 ", "instagram": "@xenia"}}
|
||||
sent.clear()
|
||||
r = client.put("/api/identity", headers=owner_h, json=body)
|
||||
assert r.status_code == 200, r.text
|
||||
j = r.json()
|
||||
assert j["name"] == "Taverna Xenia" and j["slogan"] == "Since 1987"
|
||||
assert j["colours"] == [{"name": "Main", "colour": "#1e3a8a"}, {"name": "Olive", "colour": "#556b2f80"}]
|
||||
assert j["logos"]["main"]["id"] == logo["id"] and j["logos"]["main"]["card"]
|
||||
assert j["wifi"]["password"] == "kalimera 2026" and j["contact"]["phone"] == "2310 123456"
|
||||
assert ("signage.changed", sorted([screens[1]["id"], screens[2]["id"]])) in sent
|
||||
# the venue name is the one every app already reads
|
||||
assert client.get("/api/settings/", headers=owner_h).json()["venue.name"]["value"] == "Taverna Xenia"
|
||||
# the screens get it with their venue
|
||||
venue = client.get("/api/signage/screen", headers=screens[1]["h"]).json()["venue"]
|
||||
assert venue["name"] == "Taverna Xenia" and venue["identity"]["wifi"]["ssid"] == "Xenia Guests"
|
||||
assert venue["identity"]["logos"]["main"]["id"] == logo["id"]
|
||||
# a logo in use can't be deleted
|
||||
assert err(client.delete(f"/api/media/{logo['id']}", headers=owner_h)) == "media.in_use"
|
||||
|
||||
|
||||
def test_checked(client, owner_h):
|
||||
assert client.put("/api/identity", headers=owner_h, json={"colours": [{"name": "", "colour": "#000000"}]}).status_code == 422
|
||||
assert client.put("/api/identity", headers=owner_h, json={"colours": [{"name": "x", "colour": "red"}]}).status_code == 422
|
||||
assert client.put("/api/identity", headers=owner_h, json={"wifi": {"security": "WPA9"}}).status_code == 422
|
||||
assert client.put("/api/identity", headers=owner_h, json={"nope": 1}).status_code == 422
|
||||
assert err(client.put("/api/identity", headers=owner_h, json={"logos": {"main": 99999}})) == "media.not_found"
|
||||
too_many = [{"name": f"c{i}", "colour": "#000000"} for i in range(13)]
|
||||
assert client.put("/api/identity", headers=owner_h, json={"colours": too_many}).status_code == 422
|
||||
Reference in New Issue
Block a user