refac: fix aggregate router

This commit is contained in:
Jörn-Michael Miehe 2023-10-26 17:58:54 +02:00
parent c92398118b
commit cc96889bc4
3 changed files with 37 additions and 37 deletions

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(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)