advent22/api/advent22_api/routers/general.py

59 lines
1.2 KiB
Python

from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from PIL import Image
from ..calendar_config import (
CalendarConfig,
DoorsSaved,
get_calendar_config,
set_calendar_config,
)
from ..config import Config, get_config
from ..dav_common import dav_get_file
from ._misc import api_return_image
router = APIRouter(prefix="/general", tags=["general"])
@router.get(
"/background",
response_class=StreamingResponse,
)
async def get_image_for_day(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> StreamingResponse:
"""
Hintergrundbild laden
"""
return await api_return_image(
Image.open(await dav_get_file(f"files/{cal_cfg.background}"))
)
@router.get("/doors")
async def get_doors(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> DoorsSaved:
"""
Türchen lesen
"""
return cal_cfg.doors
@router.put("/doors")
async def put_doors(
doors: DoorsSaved,
cfg: Config = Depends(get_config),
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> None:
"""
Türchen setzen
"""
cal_cfg.doors = sorted(
doors,
key=lambda door: door.day,
)
await set_calendar_config(cfg, cal_cfg)