calendar response model
This commit is contained in:
parent
f6937b5075
commit
f448ca79a6
1 changed files with 23 additions and 17 deletions
|
@ -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(
|
||||
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 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
|
||||
for vevent in event.vobject_instance.contents["vevent"]
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue