advent22/api/advent22_api/routers/admin.py

94 lines
2.5 KiB
Python

from datetime import date
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from ..core import depends
from ..core.calendar_config import CalendarConfig, get_calendar_config
from ..core.config import Config, get_config
from ..core.settings import SETTINGS
from ._security import require_admin, user_is_admin
router = APIRouter(prefix="/admin", tags=["admin"])
@router.get("/is_admin")
async def is_admin(
is_admin: bool = Depends(user_is_admin),
) -> bool:
return is_admin
class ConfigModel(BaseModel):
class __Puzzle(BaseModel):
class __Part(BaseModel):
day: int
part: str
solution: str
day_parts: list[__Part]
begin: date
end: date
closing: date
seed: str
class __Calendar(BaseModel):
config_file: str
background: str
class __Image(BaseModel):
class __Font(BaseModel):
file: str
size: int
size: int
border: int
fonts: list[__Font]
class __WebDAV(BaseModel):
url: str
cache_ttl: int
config_file: str
puzzle: __Puzzle
calendar: __Calendar
image: __Image
webdav: __WebDAV
@router.get("/config_model")
async def get_config_model(
_: None = Depends(require_admin),
cfg: Config = Depends(get_config),
cal_cfg: CalendarConfig = Depends(get_calendar_config),
day_parts: dict[int, str] = Depends(depends.get_day_parts),
) -> ConfigModel:
return ConfigModel.model_validate(
{
"puzzle": {
"solution": cfg.puzzle.solution,
"day_parts": [
{"day": day, "part": part}
for day, part in sorted(day_parts.items())
],
"begin": date.today(), # TODO
"end": date.today(), # TODO
"closing": date.today(), # TODO
"seed": cfg.puzzle.random_seed,
},
"calendar": {
"config_file": cfg.puzzle.calendar,
"background": cal_cfg.background,
},
"image": {
"size": 500, # TODO
"border": 30, # TODO
"fonts": [{"file": cfg.server.font, "size": 50}],
},
"webdav": {
"url": SETTINGS.webdav.url,
"cache_ttl": SETTINGS.webdav.cache_ttl,
"config_file": SETTINGS.webdav.config_filename,
},
}
)