advent22/api/advent22_api/routers/admin.py

89 lines
2.1 KiB
Python

from datetime import date
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from ..core.calendar_config import CalendarConfig, get_calendar_config
from ..core.config import Config, get_config
from ..core.depends import shuffle_solution
from ..core.settings import SETTINGS
from ._security import require_admin
router = APIRouter(prefix="/admin", tags=["admin"])
@router.get("/check")
async def check_admin(
_: None = Depends(require_admin),
) -> bool:
return True
class PrivatePuzzle(BaseModel):
title: str
solution: str
shuffled: str
begin: date
end: date
closing: date
seed: str
class PrivateCalendar(BaseModel):
config_file: str
background: str
doors: list[int]
class PrivateImage(BaseModel):
size: int
border: int
fonts: list[tuple[str, int]]
class PrivateWebDAV(BaseModel):
url: str
cache_ttl: int
config_file: str
class PrivateConfig(BaseModel):
puzzle: PrivatePuzzle
calendar: PrivateCalendar
image: PrivateImage
webdav: PrivateWebDAV
@router.get("/private_config")
async def get_private_config(
_: None = Depends(require_admin),
cfg: Config = Depends(get_config),
cal_cfg: CalendarConfig = Depends(get_calendar_config),
shuffled_solution: str = Depends(shuffle_solution),
) -> PrivateConfig:
return PrivateConfig(
puzzle=PrivatePuzzle(
title="Adventskalender 2023",
solution=cfg.puzzle.solution,
shuffled=shuffled_solution,
begin=date.today(),
end=date.today(),
closing=date.today(),
seed=cfg.puzzle.random_pepper,
),
calendar=PrivateCalendar(
config_file=cfg.puzzle.calendar,
background=cal_cfg.background,
doors=[door.day for door in cal_cfg.doors],
),
image=PrivateImage(
size=500,
border=30,
fonts=[(cfg.server.font, 50)],
),
webdav=PrivateWebDAV(
url=SETTINGS.webdav.url,
cache_ttl=SETTINGS.webdav.cache_ttl,
config_file=SETTINGS.webdav.config_filename,
),
)