Compare commits

...

2 commits

5 changed files with 45 additions and 39 deletions

View file

@ -47,13 +47,13 @@ class CalDAV:
_logger.debug("calendars")
return [str(cal.name) for cal in cls.principal.calendars()]
@classmethod
@AsyncTTL(
time_to_live=SETTINGS.caldav.cache_ttl,
maxsize=SETTINGS.caldav.cache_size,
skip_args=1,
)
@asyncify
@classmethod
def get_calendar(cls, calendar_name: str) -> Calendar:
"""
Get a calendar by name using the CalDAV principal object.
@ -62,6 +62,12 @@ class CalDAV:
return cls.principal.calendar(calendar_name)
@classmethod
@AsyncTTL(
time_to_live=SETTINGS.caldav.cache_ttl,
maxsize=SETTINGS.caldav.cache_size,
skip_args=1,
)
@asyncify
def get_events(cls, calendar_name: str, cfg: Config) -> list[CalEvent]:
"""
Get a sorted list of events by CalDAV calendar name.

View file

@ -6,8 +6,7 @@ This file: Main API router definition.
from fastapi import APIRouter
# from . import aggregate, calendar, file, image, misc, text, ticker
from . import calendar, file, image, misc, text, ticker
from . import aggregate, calendar, file, image, misc, text, ticker
router = APIRouter(prefix="/api/v1")
@ -19,6 +18,6 @@ router.include_router(image.router)
router.include_router(file.router)
router.include_router(calendar.router)
# router.include_router(aggregate.router)
router.include_router(aggregate.router)
__all__ = ["router"]

View file

@ -11,7 +11,7 @@ from fastapi import Depends, HTTPException, params, status
from webdav3.exceptions import RemoteResourceNotFound
from ...core.caldav import CalDAV
from ...core.config import get_config
from ...core.config import Config, get_config
from ...core.webdav import WebDAV
_logger = getLogger(__name__)
@ -171,14 +171,17 @@ async def list_calendar_names() -> list[str]:
LM_CALENDARS = ListManager.from_lister_fn(list_calendar_names)
# async def list_aggregate_names(
# cfg: Config = Depends(get_config),
# ) -> list[str]:
# """
# List aggregate calendar names
# """
async def list_aggregate_names(
cfg: Config = Depends(get_config),
) -> list[str]:
"""
List aggregate calendar names
"""
# return list(cfg.calendar.aggregates.keys())
if isinstance(cfg, params.Depends):
cfg = await get_config()
return list(cfg.calendar.aggregates.keys())
# LM_AGGREGATES = ListManager.from_lister_fn(list_aggregate_names)
LM_AGGREGATES = ListManager.from_lister_fn(list_aggregate_names)

View file

@ -13,53 +13,51 @@ from fastapi import APIRouter, Depends
from ...core.caldav import CalDAV
from ...core.calevent import CalEvent
from ...core.config import Config
from ._common import AggregateNameLister, PrefixFinder, PrefixUnique
from .calendar import calendar_unique
from ...core.config import Config, get_config
from ._common import LM_AGGREGATES, LM_CALENDARS
_logger = getLogger(__name__)
router = APIRouter(prefix="/aggregate", tags=["calendar"])
aggregate_lister = AggregateNameLister()
aggregate_finder = PrefixFinder(aggregate_lister)
aggregate_unique = PrefixUnique(aggregate_finder)
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
@router.get("/list", response_model=list[str])
@router.get(
"/list",
responses=LM_AGGREGATES.lister.responses,
)
async def list_aggregate_calendars(
names: Iterator[str] = Depends(aggregate_lister),
names: Iterator[str] = Depends(LM_AGGREGATES.lister.func),
) -> list[str]:
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
@router.get(
"/find/{prefix}",
responses=LM_AGGREGATES.filter.responses,
)
async def find_aggregate_calendars(
names: Iterator[str] = Depends(aggregate_finder),
names: Iterator[str] = Depends(LM_AGGREGATES.filter.func),
) -> list[str]:
return list(names)
@router.get("/get/{prefix}", response_model=list[CalEvent])
@router.get(
"/get/{prefix}",
responses=LM_AGGREGATES.getter.responses,
)
async def get_aggregate_calendar(
name: str = Depends(aggregate_unique),
cfg: Config = Depends(get_config),
name: str = Depends(LM_AGGREGATES.getter.func),
) -> list[CalEvent]:
cfg = await Config.get()
aggregate = cfg.calendar.aggregates[name]
events: list[CalEvent] = []
calendars = (
DavCalendar(await calendar_unique(cal_prefix)) for cal_prefix in aggregate
)
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(
[
event
async for calendar in calendars # type: ignore
for event in (await calendar.events)
]
)
return sorted(events)

View file

@ -52,7 +52,7 @@ async def get_calendar(
name: str = Depends(LM_CALENDARS.getter.func),
cfg: Config = Depends(get_config),
) -> list[CalEvent]:
return CalDAV.get_events(name, cfg)
return await CalDAV.get_events(name, cfg)
@router.get("/config")