2023-09-15 16:54:45 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-09-12 21:06:52 +00:00
|
|
|
from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config
|
2023-09-15 16:54:45 +00:00
|
|
|
from ..core.depends import get_all_event_days, get_day_image
|
|
|
|
from ..core.helpers import EventDays, api_return_image, load_image
|
2023-09-12 13:50:02 +00:00
|
|
|
from ._security import user_can_view_day, user_is_admin
|
|
|
|
|
2023-09-12 20:51:26 +00:00
|
|
|
router = APIRouter(prefix="/user", tags=["user"])
|
2023-09-12 13:50:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
2023-09-12 20:51:26 +00:00
|
|
|
"/background_image",
|
2023-09-12 13:50:02 +00:00
|
|
|
response_class=StreamingResponse,
|
|
|
|
)
|
2023-09-12 20:51:26 +00:00
|
|
|
async def get_background_image(
|
2023-09-12 13:50:02 +00:00
|
|
|
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(
|
2023-09-12 20:51:26 +00:00
|
|
|
"/image_{day}",
|
2023-09-12 13:50:02 +00:00
|
|
|
response_class=StreamingResponse,
|
|
|
|
)
|
|
|
|
async def get_image_for_day(
|
|
|
|
image: Image.Image = Depends(get_day_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)
|
2023-09-12 21:06:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/doors")
|
|
|
|
async def get_doors(
|
|
|
|
cal_cfg: CalendarConfig = Depends(get_calendar_config),
|
|
|
|
) -> DoorsSaved:
|
|
|
|
"""
|
|
|
|
User-sichtbare Türchen lesen
|
|
|
|
"""
|
|
|
|
|
|
|
|
return [door for door in cal_cfg.doors if await user_can_view_day(door.day)]
|
2023-09-15 16:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/next_door")
|
|
|
|
async def get_next_door(
|
|
|
|
event_days: EventDays = Depends(get_all_event_days),
|
|
|
|
) -> int | None:
|
|
|
|
"""
|
|
|
|
Zeit in ms, bis das nächste Türchen öffnet
|
|
|
|
"""
|
|
|
|
|
|
|
|
if event_days.next is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
dt = datetime.combine(event_days.next, datetime.min.time())
|
|
|
|
td = dt - datetime.now()
|
|
|
|
|
|
|
|
return int(td.total_seconds() * 1000)
|