From 37b1a1ee68448bff4a6d775b372b8bf8bf8a14c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 3 Sep 2022 02:00:07 +0000 Subject: [PATCH] quick and dirty vobject traversal --- api/ovdashboard_api/routers/calendar.py | 62 +++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/api/ovdashboard_api/routers/calendar.py b/api/ovdashboard_api/routers/calendar.py index c2e330c..00e545a 100644 --- a/api/ovdashboard_api/routers/calendar.py +++ b/api/ovdashboard_api/routers/calendar.py @@ -1,14 +1,68 @@ -from fastapi import APIRouter +from datetime import datetime, timedelta +from typing import Iterator + +from fastapi import APIRouter, Depends, HTTPException, status from .. import caldav_principal router = APIRouter(prefix="/calendar", tags=["calendar"]) -@router.get("/list", response_model=list[str]) -async def list_calendars() -> list[str]: +async def get_calendar_names() -> Iterator[str]: principal = caldav_principal() - return list( + return ( cal.name for cal in principal.calendars() ) + + +@router.get("/list", response_model=list[str]) +async def list_calendars( + calendar_names: Iterator[str] = Depends(get_calendar_names), +) -> list[str]: + return list(calendar_names) + + +async def find_calendars_by_prefix( + prefix: str, + calendar_names: Iterator[str] = Depends(get_calendar_names), +) -> Iterator[str]: + return ( + name + for name in calendar_names + if name.lower().startswith(prefix.lower()) + ) + + +@router.get("/find/{prefix}", response_model=list[str]) +async def find_calendars( + calendar_names: Iterator[str] = Depends(find_calendars_by_prefix), +) -> list[str]: + return list(calendar_names) + + +@router.get("/get/{prefix}", response_model=list[str]) +async def get_calendar( + calendar_names: Iterator[str] = Depends(find_calendars_by_prefix), +) -> list[str]: + calendar_names = list(calendar_names) + + if not (calendar_names): + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + + elif len(calendar_names) > 1: + raise HTTPException(status_code=status.HTTP_409_CONFLICT) + + calendar = caldav_principal().calendar(name=calendar_names[0]) + + events = calendar.date_search( + start=datetime.now(), + end=datetime.now() + timedelta(days=365), + expand=True, + ) + + return list( + str(child.contents['summary'][0].value) + for event in events + for child in event.vobject_instance.contents['vevent'] + )