2023-09-11 19:39:02 +00:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config
|
2023-09-11 19:39:02 +00:00
|
|
|
from ..core.config import Config, get_config
|
2023-09-12 13:50:02 +00:00
|
|
|
from ..core.depends import get_day_parts
|
2023-09-11 19:39:02 +00:00
|
|
|
from ..core.settings import SETTINGS
|
2023-09-11 23:36:36 +00:00
|
|
|
from ._security import require_admin, user_is_admin
|
2023-09-11 19:39:02 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/admin", tags=["admin"])
|
|
|
|
|
|
|
|
|
2023-09-11 23:10:17 +00:00
|
|
|
@router.get("/is_admin")
|
|
|
|
async def is_admin(
|
|
|
|
is_admin: bool = Depends(user_is_admin),
|
2023-09-11 19:39:02 +00:00
|
|
|
) -> bool:
|
2023-09-11 23:10:17 +00:00
|
|
|
return is_admin
|
2023-09-11 19:39:02 +00:00
|
|
|
|
|
|
|
|
2023-09-11 22:24:01 +00:00
|
|
|
class ConfigModel(BaseModel):
|
|
|
|
class __Puzzle(BaseModel):
|
2023-09-12 07:27:59 +00:00
|
|
|
class __Part(BaseModel):
|
|
|
|
day: int
|
|
|
|
part: str
|
|
|
|
|
2023-09-11 22:24:01 +00:00
|
|
|
solution: str
|
2023-09-12 07:27:59 +00:00
|
|
|
day_parts: list[__Part]
|
2023-09-11 22:24:01 +00:00
|
|
|
begin: date
|
|
|
|
end: date
|
|
|
|
closing: date
|
|
|
|
seed: str
|
|
|
|
|
|
|
|
class __Calendar(BaseModel):
|
|
|
|
config_file: str
|
|
|
|
background: str
|
|
|
|
|
|
|
|
class __Image(BaseModel):
|
2023-09-11 22:50:11 +00:00
|
|
|
class __Font(BaseModel):
|
|
|
|
file: str
|
|
|
|
size: int
|
|
|
|
|
2023-09-11 22:24:01 +00:00
|
|
|
size: int
|
|
|
|
border: int
|
2023-09-11 22:50:11 +00:00
|
|
|
fonts: list[__Font]
|
2023-09-11 22:24:01 +00:00
|
|
|
|
|
|
|
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(
|
2023-09-11 19:39:02 +00:00
|
|
|
_: None = Depends(require_admin),
|
|
|
|
cfg: Config = Depends(get_config),
|
|
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
2023-09-12 13:50:02 +00:00
|
|
|
day_parts: dict[int, str] = Depends(get_day_parts),
|
2023-09-11 22:24:01 +00:00
|
|
|
) -> ConfigModel:
|
|
|
|
return ConfigModel.model_validate(
|
|
|
|
{
|
|
|
|
"puzzle": {
|
|
|
|
"solution": cfg.puzzle.solution,
|
2023-09-12 07:27:59 +00:00
|
|
|
"day_parts": [
|
|
|
|
{"day": day, "part": part}
|
|
|
|
for day, part in sorted(day_parts.items())
|
|
|
|
],
|
2023-09-11 22:50:11 +00:00
|
|
|
"begin": date.today(), # TODO
|
|
|
|
"end": date.today(), # TODO
|
|
|
|
"closing": date.today(), # TODO
|
2023-09-11 22:24:01 +00:00
|
|
|
"seed": cfg.puzzle.random_seed,
|
|
|
|
},
|
|
|
|
"calendar": {
|
|
|
|
"config_file": cfg.puzzle.calendar,
|
|
|
|
"background": cal_cfg.background,
|
|
|
|
},
|
|
|
|
"image": {
|
2023-09-11 22:50:11 +00:00
|
|
|
"size": 500, # TODO
|
|
|
|
"border": 30, # TODO
|
|
|
|
"fonts": [{"file": cfg.server.font, "size": 50}],
|
2023-09-11 22:24:01 +00:00
|
|
|
},
|
|
|
|
"webdav": {
|
|
|
|
"url": SETTINGS.webdav.url,
|
|
|
|
"cache_ttl": SETTINGS.webdav.cache_ttl,
|
|
|
|
"config_file": SETTINGS.webdav.config_filename,
|
|
|
|
},
|
|
|
|
}
|
2023-09-11 19:39:02 +00:00
|
|
|
)
|
2023-09-12 13:50:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/doors")
|
|
|
|
async def get_doors(
|
|
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
|
|
|
) -> DoorsSaved:
|
|
|
|
"""
|
|
|
|
Türchen lesen
|
|
|
|
"""
|
|
|
|
|
|
|
|
return cal_cfg.doors
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/doors")
|
|
|
|
async def put_doors(
|
|
|
|
doors: DoorsSaved,
|
|
|
|
cfg: Config = Depends(get_config),
|
|
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Türchen ändern
|
|
|
|
"""
|
|
|
|
|
|
|
|
cal_cfg.doors = sorted(
|
|
|
|
doors,
|
|
|
|
key=lambda door: door.day,
|
|
|
|
)
|
|
|
|
await cal_cfg.change(cfg)
|