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

64 lines
1.5 KiB
Python
Raw Normal View History

2022-09-06 22:42:28 +00:00
"""
2022-09-08 23:39:42 +00:00
Router "aggregate" provides:
2022-09-06 22:42:28 +00:00
- listing aggregate calendars
- finding aggregate calendars by name prefix
- getting aggregate calendar events by name prefix
"""
2022-09-08 14:07:33 +00:00
from logging import getLogger
2022-09-06 22:42:28 +00:00
from typing import Iterator
from fastapi import APIRouter, Depends
2023-10-17 12:55:38 +00:00
2023-10-20 08:43:15 +00:00
from ...core.caldav import CalDAV
from ...core.calevent import CalEvent
2023-10-26 15:58:54 +00:00
from ...core.config import Config, get_config
from ._common import LM_AGGREGATES, LM_CALENDARS
2022-09-06 22:42:28 +00:00
2022-09-08 14:07:33 +00:00
_logger = getLogger(__name__)
2022-09-06 22:42:28 +00:00
router = APIRouter(prefix="/aggregate", tags=["calendar"])
2022-09-08 14:07:33 +00:00
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
2023-10-26 15:58:54 +00:00
@router.get(
"/list",
responses=LM_AGGREGATES.lister.responses,
)
2022-09-06 22:42:28 +00:00
async def list_aggregate_calendars(
2023-10-26 15:58:54 +00:00
names: Iterator[str] = Depends(LM_AGGREGATES.lister.func),
2022-09-06 22:42:28 +00:00
) -> list[str]:
return list(names)
2023-10-26 15:58:54 +00:00
@router.get(
"/find/{prefix}",
responses=LM_AGGREGATES.filter.responses,
)
2022-09-06 22:42:28 +00:00
async def find_aggregate_calendars(
2023-10-26 15:58:54 +00:00
names: Iterator[str] = Depends(LM_AGGREGATES.filter.func),
2022-09-06 22:42:28 +00:00
) -> list[str]:
return list(names)
2023-10-26 15:58:54 +00:00
@router.get(
"/get/{prefix}",
responses=LM_AGGREGATES.getter.responses,
)
2022-09-06 22:42:28 +00:00
async def get_aggregate_calendar(
2023-10-26 15:58:54 +00:00
cfg: Config = Depends(get_config),
name: str = Depends(LM_AGGREGATES.getter.func),
2022-09-06 22:42:28 +00:00
) -> list[CalEvent]:
2023-10-26 15:58:54 +00:00
events: list[CalEvent] = []
for cal_prefix in cfg.calendar.aggregates[name]:
cal_name = await LM_CALENDARS.getter.func(cal_prefix)
2023-10-26 16:04:03 +00:00
events.extend(await CalDAV.get_events(cal_name, cfg))
2023-10-26 15:58:54 +00:00
return sorted(events)