ovdashboard/api/ovdashboard_api/routers/v1/aggregate.py

62 lines
1.4 KiB
Python

"""
Router "aggregate" provides:
- listing aggregate calendars
- finding aggregate calendars by name prefix
- getting aggregate calendar events by name prefix
"""
import logging
from fastapi import APIRouter, Depends
from ...core.caldav import CalDAV
from ...core.calevent import CalEvent
from ...core.config import Config
from ._common import LM_AGGREGATE, LM_CALENDAR, get_config
_logger = logging.getLogger(__name__)
router = APIRouter(prefix="/aggregate", tags=["calendar"])
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
@router.get(
"/list",
responses=LM_AGGREGATE.lister.responses,
)
async def list_aggregate_calendars(
names: list[str] = Depends(LM_AGGREGATE.lister.func),
) -> list[str]:
return names
@router.get(
"/find/{prefix}",
responses=LM_AGGREGATE.filter.responses,
)
async def find_aggregate_calendars(
names: list[str] = Depends(LM_AGGREGATE.filter.func),
) -> list[str]:
return names
@router.get(
"/get/{prefix}",
responses=LM_AGGREGATE.getter.responses,
)
async def get_aggregate_calendar(
cfg: Config = Depends(get_config),
name: str = Depends(LM_AGGREGATE.getter.func),
) -> list[CalEvent]:
events: list[CalEvent] = []
for cal_prefix in cfg.calendar.aggregates[name]:
cal_name = await LM_CALENDAR.getter.func(cal_prefix)
events.extend(await CalDAV.get_events(cal_name, cfg))
return sorted(events)