calendar response model

This commit is contained in:
Jörn-Michael Miehe 2022-09-04 14:14:22 +00:00
parent f6937b5075
commit f448ca79a6

View file

@ -2,6 +2,7 @@ from datetime import datetime, timedelta
from typing import Iterator from typing import Iterator
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
from .. import caldav_principal from .. import caldav_principal
from ._common import CalendarNameLister, PrefixFinder from ._common import CalendarNameLister, PrefixFinder
@ -26,10 +27,17 @@ async def find_calendars(
return list(calendar_names) 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( async def get_calendar(
calendar_names: Iterator[str] = Depends(_finder), calendar_names: Iterator[str] = Depends(_finder),
) -> list[str]: ) -> list[CalEvent]:
calendar_names = list(calendar_names) calendar_names = list(calendar_names)
if not (calendar_names): if not (calendar_names):
@ -40,19 +48,17 @@ async def get_calendar(
calendar = caldav_principal().calendar(name=calendar_names[0]) calendar = caldav_principal().calendar(name=calendar_names[0])
events = [] return (
CalEvent(
search_results = calendar.date_search( summary=vevent.summary.value,
start=datetime.now(), description=vevent.description.value,
end=datetime.now() + timedelta(days=365), dtstart=vevent.dtstart.value,
expand=True, 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