advent22/api/advent22_api/routers/days.py
Jörn-Michael Miehe af00dafb6c router integration: stuck
apparently, a @staticmethod that Depends on another @staticmethod in the same class is bad
2023-09-08 19:08:13 +00:00

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 Config
from ..core.depends import AllTime, Today
from ..core.image_helpers import api_return_image
from .user import user_is_admin
router = APIRouter(prefix="/days", tags=["days"])
@router.on_event("startup")
async def startup() -> None:
cfg = await Config.get_config()
print(cfg.puzzle.solution)
shuffled_solution = await AllTime.shuffle_solution(cfg)
print(shuffled_solution)
@router.get("/part/{day}")
async def get_part(part: str = Depends(Today.get_part)) -> str:
return part
@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(
day: int,
) -> bool:
return day < await get_visible_days()
@router.get(
"/image/{day}",
response_class=StreamingResponse,
)
async def get_image_for_day(
image: Image.Image = Depends(Today.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)