ovdashboard/api/ovdashboard_api/routers/cal_aggregate.py

56 lines
1.5 KiB
Python
Raw Normal View History

2022-09-06 22:42:28 +00:00
"""
Router "cal_aggregate" provides:
- listing aggregate calendars
- finding aggregate calendars by name prefix
- getting aggregate calendar events by name prefix
"""
from typing import Iterator
from fastapi import APIRouter, Depends
from ovdashboard_api.config import Config
from ..dav_calendar import CalEvent, DavCalendar
from ._common import CalAggregateLister, PrefixFinder, PrefixUnique
2022-09-06 23:50:42 +00:00
from .calendar import calendar_unique
2022-09-06 22:42:28 +00:00
router = APIRouter(prefix="/aggregate", tags=["calendar"])
2022-09-06 23:50:42 +00:00
cal_aggregate_lister = CalAggregateLister()
cal_aggregate_finder = PrefixFinder(cal_aggregate_lister)
cal_aggregate_unique = PrefixUnique(cal_aggregate_finder)
2022-09-06 22:42:28 +00:00
@router.get("/list", response_model=list[str])
async def list_aggregate_calendars(
2022-09-06 23:50:42 +00:00
names: Iterator[str] = Depends(cal_aggregate_lister),
2022-09-06 22:42:28 +00:00
) -> list[str]:
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_aggregate_calendars(
2022-09-06 23:50:42 +00:00
names: Iterator[str] = Depends(cal_aggregate_finder),
2022-09-06 22:42:28 +00:00
) -> list[str]:
return list(names)
@router.get("/get/{prefix}", response_model=list[CalEvent])
async def get_aggregate_calendar(
2022-09-06 23:50:42 +00:00
name: str = Depends(cal_aggregate_unique),
2022-09-06 22:42:28 +00:00
) -> list[CalEvent]:
cfg = await Config.get()
aggregate = cfg.calendar.aggregate[name]
2022-09-06 23:50:42 +00:00
calendars = (
DavCalendar(await calendar_unique(cal_prefix))
for cal_prefix in aggregate
)
2022-09-06 22:42:28 +00:00
return sorted([
event
2022-09-06 23:50:42 +00:00
async for calendar in calendars
for event in (await calendar.events)
2022-09-06 22:42:28 +00:00
])