advent22/api/advent22_api/routers/user.py

110 lines
2.7 KiB
Python
Raw Permalink Normal View History

2023-09-15 16:54:45 +00:00
from datetime import datetime
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-11-03 14:40:44 +00:00
from ..core.config import Config, Site, get_config
from ..core.depends import get_all_event_dates, get_day_image
2023-11-02 12:48:52 +00:00
from ..core.helpers import EventDates, api_return_ico, api_return_jpeg, load_image
2023-09-21 11:26:02 +00:00
from ._security import user_can_view_day, user_is_admin, user_visible_days
2023-09-12 20:51:26 +00:00
router = APIRouter(prefix="/user", tags=["user"])
@router.get(
2023-09-12 20:51:26 +00:00
"/background_image",
response_class=StreamingResponse,
)
2023-09-12 20:51:26 +00:00
async def get_background_image(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> StreamingResponse:
"""
Hintergrundbild laden
"""
2023-11-02 12:48:52 +00:00
return await api_return_jpeg(await load_image(f"files/{cal_cfg.background}"))
@router.get(
"/favicon",
response_class=StreamingResponse,
)
async def get_favicon(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> StreamingResponse:
"""
Favicon laden
"""
try:
return await api_return_ico(await load_image(f"files/{cal_cfg.favicon}"))
except RuntimeError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
2023-11-03 14:40:44 +00:00
@router.get("/site_config")
async def get_site_config(
2023-10-31 20:07:09 +00:00
cfg: Config = Depends(get_config),
2023-11-03 14:40:44 +00:00
) -> Site:
2023-10-31 20:07:09 +00:00
"""
2023-11-03 14:40:44 +00:00
Seiteninhalt
2023-10-31 20:07:09 +00:00
"""
2023-11-03 14:40:44 +00:00
return cfg.site
2023-10-31 20:07:09 +00:00
2023-09-21 12:25:23 +00:00
@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(
2023-09-12 20:51:26 +00:00
"/image_{day}",
response_class=StreamingResponse,
)
async def get_image_for_day(
2023-09-21 11:26:02 +00:00
user_can_view: bool = Depends(user_can_view_day),
is_admin: bool = Depends(user_is_admin),
2023-09-21 11:26:02 +00:00
image: Image.Image | None = Depends(get_day_image),
) -> StreamingResponse:
"""
Bild für einen Tag erstellen
"""
2023-09-21 11:26:02 +00:00
if not (user_can_view or is_admin):
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
2023-09-21 11:26:02 +00:00
if image is None:
raise HTTPException(
status.HTTP_404_NOT_FOUND, "Ich habe heute leider kein Foto für dich."
)
2023-11-02 12:48:52 +00:00
return await api_return_jpeg(image)
2023-09-12 21:06:52 +00:00
2023-09-15 16:54:45 +00:00
@router.get("/next_door")
async def get_next_door(
event_dates: EventDates = Depends(get_all_event_dates),
2023-09-15 16:54:45 +00:00
) -> int | None:
"""
Zeit in ms, bis das nächste Türchen öffnet
"""
if event_dates.next is None:
2023-09-15 16:54:45 +00:00
return None
dt = datetime.combine(event_dates.next, datetime.min.time())
2023-09-15 16:54:45 +00:00
td = dt - datetime.now()
return int(td.total_seconds() * 1000)