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

63 lines
1.5 KiB
Python
Raw Permalink 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
"""
2023-10-26 16:21:07 +00:00
import logging
2022-09-06 22:42:28 +00:00
from fastapi import APIRouter, Depends
2023-10-17 12:55:38 +00:00
2023-10-20 08:43:15 +00:00
from ...core.calevent import CalEvent
2023-10-26 16:26:51 +00:00
from ...core.config import Config
2023-11-09 11:13:48 +00:00
from ...core.dav.caldav import CalDAV
2023-10-26 17:17:16 +00:00
from ._common import LM_AGGREGATE, LM_CALENDAR, get_config
2022-09-06 22:42:28 +00:00
2023-10-26 16:21:07 +00:00
_logger = logging.getLogger(__name__)
2022-09-08 14:07:33 +00:00
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",
2023-10-26 17:17:16 +00:00
responses=LM_AGGREGATE.lister.responses,
2023-10-26 15:58:54 +00:00
)
2022-09-06 22:42:28 +00:00
async def list_aggregate_calendars(
2023-10-26 17:17:16 +00:00
names: list[str] = Depends(LM_AGGREGATE.lister.func),
2022-09-06 22:42:28 +00:00
) -> list[str]:
2023-10-26 16:21:07 +00:00
return names
2022-09-06 22:42:28 +00:00
2023-10-26 15:58:54 +00:00
@router.get(
"/find/{prefix}",
2023-10-26 17:17:16 +00:00
responses=LM_AGGREGATE.filter.responses,
2023-10-26 15:58:54 +00:00
)
2022-09-06 22:42:28 +00:00
async def find_aggregate_calendars(
2023-10-26 17:17:16 +00:00
names: list[str] = Depends(LM_AGGREGATE.filter.func),
2022-09-06 22:42:28 +00:00
) -> list[str]:
2023-10-26 16:21:07 +00:00
return names
2022-09-06 22:42:28 +00:00
2023-10-26 15:58:54 +00:00
@router.get(
"/get/{prefix}",
2023-10-26 17:17:16 +00:00
responses=LM_AGGREGATE.getter.responses,
2023-10-26 15:58:54 +00:00
)
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),
2023-10-26 17:17:16 +00:00
name: str = Depends(LM_AGGREGATE.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]:
2023-10-26 17:17:16 +00:00
cal_name = await LM_CALENDAR.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)