mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-25 22:02:59 +00:00
91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
from datetime import datetime
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.responses import StreamingResponse
|
|
from PIL import Image
|
|
|
|
from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config
|
|
from ..core.config import Config, get_config
|
|
from ..core.depends import get_all_event_dates, get_day_image
|
|
from ..core.helpers import EventDates, api_return_image, load_image
|
|
from ._security import user_can_view_day, user_is_admin, user_visible_days
|
|
|
|
router = APIRouter(prefix="/user", tags=["user"])
|
|
|
|
|
|
@router.get(
|
|
"/background_image",
|
|
response_class=StreamingResponse,
|
|
)
|
|
async def get_background_image(
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
|
) -> StreamingResponse:
|
|
"""
|
|
Hintergrundbild laden
|
|
"""
|
|
|
|
return await api_return_image(await load_image(f"files/{cal_cfg.background}"))
|
|
|
|
|
|
@router.get("/doors")
|
|
async def get_doors(
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
|
visible_days: list[int] = Depends(user_visible_days),
|
|
) -> DoorsSaved:
|
|
"""
|
|
User-sichtbare Türchen lesen
|
|
"""
|
|
|
|
return [door for door in cal_cfg.doors if door.day in visible_days]
|
|
|
|
|
|
@router.get("/footer")
|
|
async def get_footer(
|
|
cfg: Config = Depends(get_config),
|
|
) -> str:
|
|
"""
|
|
Seiten-Fußzeile lesen
|
|
"""
|
|
|
|
return cfg.footer
|
|
|
|
|
|
@router.get(
|
|
"/image_{day}",
|
|
response_class=StreamingResponse,
|
|
)
|
|
async def get_image_for_day(
|
|
user_can_view: bool = Depends(user_can_view_day),
|
|
is_admin: bool = Depends(user_is_admin),
|
|
image: Image.Image | None = Depends(get_day_image),
|
|
) -> StreamingResponse:
|
|
"""
|
|
Bild für einen Tag erstellen
|
|
"""
|
|
|
|
if not (user_can_view or is_admin):
|
|
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
|
|
|
|
if image is None:
|
|
raise HTTPException(
|
|
status.HTTP_404_NOT_FOUND, "Ich habe heute leider kein Foto für dich."
|
|
)
|
|
|
|
return await api_return_image(image)
|
|
|
|
|
|
@router.get("/next_door")
|
|
async def get_next_door(
|
|
event_dates: EventDates = Depends(get_all_event_dates),
|
|
) -> int | None:
|
|
"""
|
|
Zeit in ms, bis das nächste Türchen öffnet
|
|
"""
|
|
|
|
if event_dates.next is None:
|
|
return None
|
|
|
|
dt = datetime.combine(event_dates.next, datetime.min.time())
|
|
td = dt - datetime.now()
|
|
|
|
return int(td.total_seconds() * 1000)
|