mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 16:23:00 +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 PIL import Image
|
|
|
|
from ..core.config import 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_day, user_is_admin, user_visible_days
|
|
|
|
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 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_days()
|
|
|
|
|
|
@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_day),
|
|
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)
|