advent22/api/advent22_api/routers/days.py

69 lines
1.5 KiB
Python

from datetime import date
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse
from PIL import Image
from ..core.config import get_config
from ..core.depends import get_image, get_part, shuffle_solution
from ..core.image_helpers import api_return_image
from ._security import user_can_view_door, user_is_admin, user_visible_doors
router = APIRouter(prefix="/days", tags=["days"])
@router.on_event("startup")
async def startup() -> None:
cfg = await get_config()
print(cfg.puzzle.solution)
shuffled_solution = await shuffle_solution(cfg)
print(shuffled_solution)
@router.get("/date")
async def get_date() -> str:
"""
Aktuelles Server-Datum
"""
return date.today().isoformat()
@router.get("/visible_days")
async def get_visible_days() -> int:
"""
Sichtbare Türchen
"""
return await user_visible_doors()
@router.get("/part/{day}")
async def get_part_for_day(
part: str = Depends(get_part),
) -> str:
"""
Heutiger Lösungsteil
"""
return part
@router.get(
"/image/{day}",
response_class=StreamingResponse,
)
async def get_image_for_day(
image: Image.Image = Depends(get_image),
can_view: bool = Depends(user_can_view_door),
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)