From f448ca79a65af97817201c194f00f6fc035a96a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 4 Sep 2022 14:14:22 +0000 Subject: [PATCH] calendar response model --- api/ovdashboard_api/routers/calendar.py | 40 ++++++++++++++----------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/api/ovdashboard_api/routers/calendar.py b/api/ovdashboard_api/routers/calendar.py index 55b1ece..60164df 100644 --- a/api/ovdashboard_api/routers/calendar.py +++ b/api/ovdashboard_api/routers/calendar.py @@ -2,6 +2,7 @@ from datetime import datetime, timedelta from typing import Iterator from fastapi import APIRouter, Depends, HTTPException, status +from pydantic import BaseModel from .. import caldav_principal from ._common import CalendarNameLister, PrefixFinder @@ -26,10 +27,17 @@ async def find_calendars( return list(calendar_names) -@router.get("/get/{prefix}", response_model=list[str]) +class CalEvent(BaseModel): + summary: str + description: str + dtstart: datetime + dtend: datetime + + +@router.get("/get/{prefix}", response_model=list[CalEvent]) async def get_calendar( calendar_names: Iterator[str] = Depends(_finder), -) -> list[str]: +) -> list[CalEvent]: calendar_names = list(calendar_names) if not (calendar_names): @@ -40,19 +48,17 @@ async def get_calendar( calendar = caldav_principal().calendar(name=calendar_names[0]) - events = [] - - search_results = calendar.date_search( - start=datetime.now(), - end=datetime.now() + timedelta(days=365), - expand=True, + return ( + CalEvent( + summary=vevent.summary.value, + description=vevent.description.value, + dtstart=vevent.dtstart.value, + dtend=vevent.dtend.value, + ) + for event in calendar.date_search( + start=datetime.now(), + end=datetime.now() + timedelta(days=365), + expand=True, + ) + for vevent in event.vobject_instance.contents["vevent"] ) - - for event in search_results: - for vevent in event.vobject_instance.contents["vevent"]: - events.append(vevent.summary.value) - events.append(vevent.description.value) - events.append(str(vevent.dtstart.value)) - events.append(str(vevent.dtend.value)) - - return events