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

62 lines
1.5 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, get_config
from ._common import LM_AGGREGATES, LM_CALENDARS
_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_AGGREGATES.lister.responses,
)
async def list_aggregate_calendars(
names: list[str] = Depends(LM_AGGREGATES.lister.func),
) -> list[str]:
return names
@router.get(
"/find/{prefix}",
responses=LM_AGGREGATES.filter.responses,
)
async def find_aggregate_calendars(
names: list[str] = Depends(LM_AGGREGATES.filter.func),
) -> list[str]:
return names
@router.get(
"/get/{prefix}",
responses=LM_AGGREGATES.getter.responses,
)
async def get_aggregate_calendar(
cfg: Config = Depends(get_config),
name: str = Depends(LM_AGGREGATES.getter.func),
) -> list[CalEvent]:
events: list[CalEvent] = []
for cal_prefix in cfg.calendar.aggregates[name]:
cal_name = await LM_CALENDARS.getter.func(cal_prefix)
events.extend(await CalDAV.get_events(cal_name, cfg))
return sorted(events)