ovdashboard/api/ovdashboard_api/routers/calendar.py

58 lines
1.5 KiB
Python
Raw Normal View History

2022-09-03 02:00:07 +00:00
from datetime import datetime, timedelta
from typing import Iterator
2022-09-04 14:14:22 +00:00
from fastapi import APIRouter, Depends
2022-09-04 14:14:22 +00:00
from pydantic import BaseModel
2022-09-02 19:21:15 +00:00
from .. import caldav_principal
2022-09-04 14:14:22 +00:00
from ._common import CalendarNameLister, PrefixFinder, PrefixUnique
2022-09-02 19:21:15 +00:00
router = APIRouter(prefix="/calendar", tags=["calendar"])
_lister = CalendarNameLister()
_finder = PrefixFinder(_lister)
2022-09-04 14:14:22 +00:00
_unique = PrefixUnique(_finder)
2022-09-03 02:00:07 +00:00
@router.get("/list", response_model=list[str])
async def list_calendars(
2022-09-04 14:14:22 +00:00
names: Iterator[str] = Depends(_lister),
2022-09-03 02:00:07 +00:00
) -> list[str]:
2022-09-04 14:14:22 +00:00
return list(names)
2022-09-03 02:00:07 +00:00
@router.get("/find/{prefix}", response_model=list[str])
async def find_calendars(
2022-09-04 14:14:22 +00:00
names: Iterator[str] = Depends(_finder),
2022-09-03 02:00:07 +00:00
) -> list[str]:
2022-09-04 14:14:22 +00:00
return list(names)
2022-09-03 02:00:07 +00:00
2022-09-04 14:14:22 +00:00
class CalEvent(BaseModel):
summary: str
description: str
dtstart: datetime
dtend: datetime
@router.get("/get/{prefix}", response_model=list[CalEvent])
2022-09-03 02:00:07 +00:00
async def get_calendar(
2022-09-04 14:14:22 +00:00
name: Iterator[str] = Depends(_unique),
2022-09-04 14:14:22 +00:00
) -> list[CalEvent]:
2022-09-04 14:14:22 +00:00
calendar = caldav_principal().calendar(name=name)
2022-09-03 02:00:07 +00:00
2022-09-04 14:14:22 +00:00
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"]
2022-09-03 02:00:07 +00:00
)