advent22/api/advent22_api/routers/days.py

69 lines
1.5 KiB
Python
Raw Normal View History

2022-11-15 23:43:13 +00:00
from datetime import date
2022-11-15 23:43:13 +00:00
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse
from PIL import Image
2022-10-10 23:46:04 +00:00
2023-09-12 07:27:59 +00:00
from ..core import depends
from ..core.config import get_config
from ..core.image_helpers import api_return_image
2023-09-08 19:44:41 +00:00
from ._security import user_can_view_door, user_is_admin, user_visible_doors
router = APIRouter(prefix="/days", tags=["days"])
@router.on_event("startup")
2022-10-14 21:42:05 +00:00
async def startup() -> None:
cfg = await get_config()
2022-11-18 01:39:05 +00:00
print(cfg.puzzle.solution)
2022-11-15 23:53:30 +00:00
@router.get("/date")
async def get_date() -> str:
"""
Aktuelles Server-Datum
"""
2022-11-15 23:53:30 +00:00
return date.today().isoformat()
2022-11-04 20:00:43 +00:00
2022-12-21 23:38:33 +00:00
@router.get("/visible_days")
2023-09-09 21:41:30 +00:00
async def get_visible_days(
visible_doors: int = Depends(user_visible_doors),
) -> int:
"""
Sichtbare Türchen
"""
2022-12-14 02:48:35 +00:00
2023-09-09 21:41:30 +00:00
return visible_doors
2022-11-15 23:43:13 +00:00
@router.get("/part/{day}")
async def get_part_for_day(
2023-09-12 07:27:59 +00:00
part: str = Depends(depends.get_part_for_day),
) -> str:
"""
Heutiger Lösungsteil
"""
return part
@router.get(
"/image/{day}",
response_class=StreamingResponse,
)
2022-11-15 22:58:04 +00:00
async def get_image_for_day(
2023-09-12 07:27:59 +00:00
image: Image.Image = Depends(depends.get_image_for_day),
2023-09-08 19:44:41 +00:00
can_view: bool = Depends(user_can_view_door),
2022-11-15 23:43:13 +00:00
is_admin: bool = Depends(user_is_admin),
) -> StreamingResponse:
2022-10-10 20:22:56 +00:00
"""
Bild für einen Tag erstellen
"""
2022-11-15 23:43:13 +00:00
if not (can_view or is_admin):
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
return await api_return_image(image)