2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Router "calendar" provides:
|
|
|
|
|
|
|
|
- listing calendars
|
|
|
|
- finding calendars by name prefix
|
|
|
|
- getting calendar events by calendar name prefix
|
|
|
|
"""
|
|
|
|
|
2022-09-08 14:07:33 +00:00
|
|
|
from logging import getLogger
|
2022-09-03 02:00:07 +00:00
|
|
|
from typing import Iterator
|
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
2022-09-02 19:21:15 +00:00
|
|
|
|
2022-09-17 19:41:41 +00:00
|
|
|
from ...config import CalendarUIConfig, Config
|
|
|
|
from ...dav_calendar import CalEvent, DavCalendar
|
2022-09-04 14:14:22 +00:00
|
|
|
from ._common import CalendarNameLister, PrefixFinder, PrefixUnique
|
2022-09-02 19:21:15 +00:00
|
|
|
|
2022-09-08 14:07:33 +00:00
|
|
|
_logger = getLogger(__name__)
|
|
|
|
|
2022-09-02 19:21:15 +00:00
|
|
|
router = APIRouter(prefix="/calendar", tags=["calendar"])
|
|
|
|
|
2022-09-06 23:50:42 +00:00
|
|
|
calendar_lister = CalendarNameLister()
|
|
|
|
calendar_finder = PrefixFinder(calendar_lister)
|
|
|
|
calendar_unique = PrefixUnique(calendar_finder)
|
2022-09-03 02:00:07 +00:00
|
|
|
|
|
|
|
|
2022-09-08 14:07:33 +00:00
|
|
|
@router.on_event("startup")
|
|
|
|
async def start_router() -> None:
|
|
|
|
_logger.debug(f"{router.prefix} router starting.")
|
|
|
|
|
|
|
|
|
2022-09-03 02:00:07 +00:00
|
|
|
@router.get("/list", response_model=list[str])
|
|
|
|
async def list_calendars(
|
2022-09-06 23:50:42 +00:00
|
|
|
names: Iterator[str] = Depends(calendar_lister),
|
2022-09-03 02:00:07 +00:00
|
|
|
) -> list[str]:
|
2022-09-04 14:14:22 +00:00
|
|
|
return list(names)
|
2022-09-03 02:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/find/{prefix}", response_model=list[str])
|
|
|
|
async def find_calendars(
|
2022-09-06 23:50:42 +00:00
|
|
|
names: Iterator[str] = Depends(calendar_finder),
|
2022-09-03 02:00:07 +00:00
|
|
|
) -> list[str]:
|
2022-09-04 14:14:22 +00:00
|
|
|
return list(names)
|
2022-09-03 02:00:07 +00:00
|
|
|
|
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
@router.get("/get/{prefix}", response_model=list[CalEvent])
|
2022-09-03 02:00:07 +00:00
|
|
|
async def get_calendar(
|
2022-09-06 23:50:42 +00:00
|
|
|
name: str = Depends(calendar_unique),
|
2022-09-04 14:14:22 +00:00
|
|
|
) -> list[CalEvent]:
|
2022-09-04 18:48:46 +00:00
|
|
|
return list(await DavCalendar(name).events)
|
2022-09-15 22:32:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
"/config",
|
|
|
|
response_model=CalendarUIConfig,
|
|
|
|
)
|
|
|
|
async def get_ui_config(
|
|
|
|
cfg: Config = Depends(Config.get),
|
|
|
|
) -> CalendarUIConfig:
|
|
|
|
return cfg.calendar
|