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 .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 shuffle_solution(cfg)
|
|
print(shuffled_solution)
|
|
|
|
|
|
@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("/part/{day}")
|
|
async def get_part_for_day(part: str = Depends(get_part)) -> str:
|
|
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),
|
|
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)
|