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.caldav import CalDAV
|
|
|
|
from ...core.calevent import CalEvent
|
2023-10-26 16:26:51 +00:00
|
|
|
from ...core.config import Config
|
|
|
|
from ._common import LM_AGGREGATES, LM_CALENDARS, 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",
|
|
|
|
responses=LM_AGGREGATES.lister.responses,
|
|
|
|
)
|
2022-09-06 22:42:28 +00:00
|
|
|
async def list_aggregate_calendars(
|
2023-10-26 16:21:07 +00:00
|
|
|
names: list[str] = Depends(LM_AGGREGATES.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}",
|
|
|
|
responses=LM_AGGREGATES.filter.responses,
|
|
|
|
)
|
2022-09-06 22:42:28 +00:00
|
|
|
async def find_aggregate_calendars(
|
2023-10-26 16:21:07 +00:00
|
|
|
names: list[str] = Depends(LM_AGGREGATES.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}",
|
|
|
|
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)
|