"admin" router with /private_config route

This commit is contained in:
Jörn-Michael Miehe 2023-09-11 19:39:02 +00:00
parent 02cb654022
commit 91c2589045
3 changed files with 95 additions and 1 deletions

View file

@ -1,9 +1,10 @@
from fastapi import APIRouter
from . import days, general, user
from . import admin, days, general, user
router = APIRouter(prefix="/api")
router.include_router(admin.router)
router.include_router(days.router)
router.include_router(general.router)
router.include_router(user.router)

View file

@ -0,0 +1,90 @@
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),
) -> PrivateConfig:
shuffled_solution = await shuffle_solution(cfg)
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,
),
)

View file

@ -6,6 +6,9 @@
<div class="content">
<h4>Rätsel</h4>
<dl>
<dt>Titel</dt>
<dd>Adventskalender 2023</dd>
<dt>Lösung</dt>
<dd>ABCDEFGHIJKLMNOPQRSTUVWX</dd>