2022-11-15 23:43:13 +00:00
|
|
|
from datetime import date
|
2022-10-10 00:09:09 +00:00
|
|
|
|
2022-11-15 23:43:13 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
2022-10-10 00:09:09 +00:00
|
|
|
from fastapi.responses import StreamingResponse
|
2022-10-10 23:46:04 +00:00
|
|
|
|
2022-11-04 18:49:31 +00:00
|
|
|
from ..config import Config, get_config
|
2022-10-14 22:09:23 +00:00
|
|
|
from ._image import AdventImage
|
2023-09-04 21:23:27 +00:00
|
|
|
from ._misc import api_return_image, get_image, shuffle
|
2022-11-15 23:43:13 +00:00
|
|
|
from .user import user_is_admin
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/days", tags=["days"])
|
|
|
|
|
|
|
|
|
|
|
|
@router.on_event("startup")
|
2022-10-14 21:42:05 +00:00
|
|
|
async def startup() -> None:
|
2022-11-04 18:49:31 +00:00
|
|
|
cfg = await get_config()
|
2022-11-18 01:39:05 +00:00
|
|
|
print(cfg.puzzle.solution)
|
|
|
|
print("".join(await shuffle(cfg.puzzle.solution)))
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/letter/{index}")
|
|
|
|
async def get_letter(
|
|
|
|
index: int,
|
2022-11-04 18:49:31 +00:00
|
|
|
cfg: Config = Depends(get_config),
|
2022-10-10 00:09:09 +00:00
|
|
|
) -> str:
|
2022-11-18 01:39:05 +00:00
|
|
|
return (await shuffle(cfg.puzzle.solution))[index]
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
|
2022-11-15 23:53:30 +00:00
|
|
|
@router.get("/date")
|
|
|
|
async def get_date() -> str:
|
|
|
|
return date.today().isoformat()
|
2022-10-10 00:09:09 +00:00
|
|
|
|
2022-11-04 20:00:43 +00:00
|
|
|
|
2022-12-21 23:38:33 +00:00
|
|
|
@router.get("/visible_days")
|
|
|
|
async def get_visible_days() -> int:
|
2022-11-15 23:43:13 +00:00
|
|
|
today = date.today()
|
|
|
|
|
2022-12-14 02:48:35 +00:00
|
|
|
if today.month == 12:
|
|
|
|
return today.day
|
|
|
|
|
2022-11-15 23:43:13 +00:00
|
|
|
if today.month in (1, 2, 3):
|
2022-12-14 02:48:35 +00:00
|
|
|
return 24
|
2022-11-15 23:43:13 +00:00
|
|
|
|
2022-12-14 02:48:35 +00:00
|
|
|
return 0
|
2022-11-15 23:43:13 +00:00
|
|
|
|
2022-12-14 02:48:35 +00:00
|
|
|
|
|
|
|
async def user_can_view(
|
|
|
|
index: int,
|
|
|
|
) -> bool:
|
2022-12-21 23:38:33 +00:00
|
|
|
return index < await get_visible_days()
|
2022-11-15 23:43:13 +00:00
|
|
|
|
|
|
|
|
2022-10-10 00:09:09 +00:00
|
|
|
@router.get(
|
2022-11-15 23:43:13 +00:00
|
|
|
"/image/{index}",
|
2022-10-10 00:09:09 +00:00
|
|
|
response_class=StreamingResponse,
|
|
|
|
)
|
2022-11-15 22:58:04 +00:00
|
|
|
async def get_image_for_day(
|
|
|
|
image: AdventImage = Depends(get_image),
|
2022-11-15 23:43:13 +00:00
|
|
|
can_view: bool = Depends(user_can_view),
|
|
|
|
is_admin: bool = Depends(user_is_admin),
|
2022-10-10 00:09:09 +00:00
|
|
|
) -> 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!!!")
|
|
|
|
|
2023-09-04 21:23:27 +00:00
|
|
|
return await api_return_image(image.img)
|