2023-09-11 19:39:02 +00:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2023-09-20 14:25:10 +00:00
|
|
|
from advent22_api.core.helpers import EventDates
|
2023-09-14 23:40:06 +00:00
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config
|
2023-09-21 11:49:28 +00:00
|
|
|
from ..core.config import Config, Image, get_config
|
2023-09-20 14:25:10 +00:00
|
|
|
from ..core.depends import get_all_event_dates, get_all_image_names, get_all_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):
|
|
|
|
solution: str
|
2023-09-14 23:40:06 +00:00
|
|
|
first: date
|
2023-09-16 00:16:18 +00:00
|
|
|
next: date | None
|
2023-09-14 23:40:06 +00:00
|
|
|
last: date
|
2023-09-11 22:24:01 +00:00
|
|
|
end: date
|
|
|
|
seed: str
|
|
|
|
|
|
|
|
class __Calendar(BaseModel):
|
|
|
|
config_file: str
|
|
|
|
background: str
|
|
|
|
|
|
|
|
class __WebDAV(BaseModel):
|
|
|
|
url: str
|
|
|
|
cache_ttl: int
|
|
|
|
config_file: str
|
|
|
|
|
|
|
|
puzzle: __Puzzle
|
|
|
|
calendar: __Calendar
|
2023-09-21 11:49:28 +00:00
|
|
|
image: Image
|
2023-09-11 22:24:01 +00:00
|
|
|
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-20 14:25:10 +00:00
|
|
|
event_dates: EventDates = Depends(get_all_event_dates),
|
2023-09-11 22:24:01 +00:00
|
|
|
) -> ConfigModel:
|
2023-09-12 17:16:02 +00:00
|
|
|
"""
|
|
|
|
Kombiniert aus privaten `settings`, `config` und `calendar_config`
|
|
|
|
"""
|
|
|
|
|
2023-09-11 22:24:01 +00:00
|
|
|
return ConfigModel.model_validate(
|
|
|
|
{
|
|
|
|
"puzzle": {
|
|
|
|
"solution": cfg.puzzle.solution,
|
2023-09-20 14:25:10 +00:00
|
|
|
"first": event_dates.first,
|
|
|
|
"next": event_dates.next,
|
|
|
|
"last": event_dates.last,
|
|
|
|
"end": event_dates.end,
|
2023-09-21 11:49:28 +00:00
|
|
|
"seed": cfg.random_seed,
|
2023-09-11 22:24:01 +00:00
|
|
|
},
|
|
|
|
"calendar": {
|
2023-09-21 11:49:28 +00:00
|
|
|
"config_file": cfg.calendar,
|
2023-09-11 22:24:01 +00:00
|
|
|
"background": cal_cfg.background,
|
|
|
|
},
|
2023-09-21 11:49:28 +00:00
|
|
|
"image": cfg.image,
|
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
|
|
|
|
|
|
|
|
2023-09-21 12:25:23 +00:00
|
|
|
@router.get("/day_image_names")
|
|
|
|
async def get_day_image_names(
|
2023-09-12 16:26:12 +00:00
|
|
|
_: None = Depends(require_admin),
|
2023-09-21 12:25:23 +00:00
|
|
|
image_names: dict[int, str] = Depends(get_all_image_names),
|
2023-09-21 00:45:57 +00:00
|
|
|
) -> dict[int, str]:
|
2023-09-12 17:15:44 +00:00
|
|
|
"""
|
2023-09-21 12:25:23 +00:00
|
|
|
Zuordnung der verwendeten Bilder zu den Tagen
|
2023-09-12 17:15:44 +00:00
|
|
|
"""
|
|
|
|
|
2023-09-21 12:25:23 +00:00
|
|
|
return image_names
|
2023-09-12 16:26:12 +00:00
|
|
|
|
|
|
|
|
2023-09-21 12:25:23 +00:00
|
|
|
@router.get("/day_parts")
|
|
|
|
async def get_day_parts(
|
2023-09-12 17:31:08 +00:00
|
|
|
_: None = Depends(require_admin),
|
2023-09-21 12:25:23 +00:00
|
|
|
parts: dict[int, str] = Depends(get_all_parts),
|
2023-09-21 00:45:57 +00:00
|
|
|
) -> dict[int, str]:
|
2023-09-12 17:31:08 +00:00
|
|
|
"""
|
2023-09-21 12:25:23 +00:00
|
|
|
Zuordnung der Lösungsteile zu den Tagen
|
2023-09-12 17:31:08 +00:00
|
|
|
"""
|
|
|
|
|
2023-09-21 12:25:23 +00:00
|
|
|
return parts
|
2023-09-12 17:31:08 +00:00
|
|
|
|
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
@router.get("/doors")
|
|
|
|
async def get_doors(
|
2023-09-12 16:26:12 +00:00
|
|
|
_: None = Depends(require_admin),
|
2023-09-12 13:50:02 +00:00
|
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
|
|
|
) -> DoorsSaved:
|
|
|
|
"""
|
|
|
|
Türchen lesen
|
|
|
|
"""
|
|
|
|
|
|
|
|
return cal_cfg.doors
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/doors")
|
|
|
|
async def put_doors(
|
|
|
|
doors: DoorsSaved,
|
2023-09-12 16:26:12 +00:00
|
|
|
_: None = Depends(require_admin),
|
2023-09-12 13:50:02 +00:00
|
|
|
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)
|
2023-09-12 22:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/dav_credentials")
|
|
|
|
async def get_dav_credentials(
|
|
|
|
_: None = Depends(require_admin),
|
|
|
|
) -> tuple[str, str]:
|
|
|
|
"""
|
|
|
|
Zugangsdaten für WebDAV
|
|
|
|
"""
|
|
|
|
|
|
|
|
return SETTINGS.webdav.username, SETTINGS.webdav.password
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/ui_credentials")
|
|
|
|
async def get_ui_credentials(
|
|
|
|
_: None = Depends(require_admin),
|
|
|
|
cfg: Config = Depends(get_config),
|
|
|
|
) -> tuple[str, str]:
|
|
|
|
"""
|
|
|
|
Zugangsdaten für Admin-UI
|
|
|
|
"""
|
|
|
|
|
|
|
|
return cfg.admin.name, cfg.admin.password
|