advent22/api/advent22_api/routers/general.py

60 lines
1.2 KiB
Python
Raw Normal View History

2023-09-04 21:23:27 +00:00
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from PIL import Image
2023-09-08 00:56:14 +00:00
from ..calendar_config import (
CalendarConfig,
DoorsSaved,
get_calendar_config,
set_calendar_config,
)
2023-09-08 19:59:26 +00:00
from ..config import Config, get_config
2023-09-04 21:23:27 +00:00
from ..dav_common import dav_get_file
from ._misc import api_return_image
2022-10-14 22:47:48 +00:00
router = APIRouter(prefix="/general", tags=["general"])
2023-09-04 21:23:27 +00:00
@router.get(
"/background",
response_class=StreamingResponse,
)
async def get_image_for_day(
2023-09-08 00:56:14 +00:00
cal_cfg: CalendarConfig = Depends(get_calendar_config),
2023-09-04 21:23:27 +00:00
) -> StreamingResponse:
"""
Hintergrundbild laden
"""
return await api_return_image(
2023-09-08 00:56:14 +00:00
Image.open(await dav_get_file(f"files/{cal_cfg.background}"))
2023-09-04 21:23:27 +00:00
)
2023-09-07 16:44:44 +00:00
@router.get("/doors")
async def get_doors(
2023-09-08 00:56:14 +00:00
cal_cfg: CalendarConfig = Depends(get_calendar_config),
2023-09-07 19:34:11 +00:00
) -> DoorsSaved:
2023-09-07 16:44:44 +00:00
"""
Türchen lesen
"""
2023-09-08 00:56:14 +00:00
return cal_cfg.doors
2023-09-07 16:44:44 +00:00
@router.put("/doors")
async def put_doors(
2023-09-07 19:34:11 +00:00
doors: DoorsSaved,
2023-09-08 19:59:26 +00:00
cfg: Config = Depends(get_config),
2023-09-08 00:56:14 +00:00
cal_cfg: CalendarConfig = Depends(get_calendar_config),
2023-09-07 16:44:44 +00:00
) -> None:
"""
Türchen setzen
"""
2023-09-08 00:56:14 +00:00
cal_cfg.doors = sorted(
2023-09-07 21:18:08 +00:00
doors,
key=lambda door: door.day,
)
2023-09-08 19:59:26 +00:00
await set_calendar_config(cfg, cal_cfg)