mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-27 01:43:08 +00:00
69 lines
1.5 KiB
Python
69 lines
1.5 KiB
Python
from datetime import date
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from ..config import Config, get_config
|
|
from ._image import AdventImage
|
|
from ._misc import api_return_image, get_image, shuffle
|
|
from .user import user_is_admin
|
|
|
|
router = APIRouter(prefix="/days", tags=["days"])
|
|
|
|
|
|
@router.on_event("startup")
|
|
async def startup() -> None:
|
|
cfg = await get_config()
|
|
print(cfg.puzzle.solution)
|
|
print("".join(await shuffle(cfg.puzzle.solution)))
|
|
|
|
|
|
@router.get("/letter/{index}")
|
|
async def get_letter(
|
|
index: int,
|
|
cfg: Config = Depends(get_config),
|
|
) -> str:
|
|
return (await shuffle(cfg.puzzle.solution))[index]
|
|
|
|
|
|
@router.get("/date")
|
|
async def get_date() -> str:
|
|
return date.today().isoformat()
|
|
|
|
|
|
@router.get("/visible_days")
|
|
async def get_visible_days() -> int:
|
|
today = date.today()
|
|
|
|
if today.month == 12:
|
|
return today.day
|
|
|
|
if today.month in (1, 2, 3):
|
|
return 24
|
|
|
|
return 0
|
|
|
|
|
|
async def user_can_view(
|
|
index: int,
|
|
) -> bool:
|
|
return index < await get_visible_days()
|
|
|
|
|
|
@router.get(
|
|
"/image/{index}",
|
|
response_class=StreamingResponse,
|
|
)
|
|
async def get_image_for_day(
|
|
image: AdventImage = Depends(get_image),
|
|
can_view: bool = Depends(user_can_view),
|
|
is_admin: bool = Depends(user_is_admin),
|
|
) -> StreamingResponse:
|
|
"""
|
|
Bild für einen Tag erstellen
|
|
"""
|
|
|
|
if not (can_view or is_admin):
|
|
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
|
|
|
|
return await api_return_image(image.img)
|