ovdashboard/api/ovdashboard_api/routers/calendar.py

64 lines
1.7 KiB
Python

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
router = APIRouter(prefix="/calendar", tags=["calendar"])
_lister = CalendarNameLister()
_finder = PrefixFinder(_lister)
@router.get("/list", response_model=list[str])
async def list_calendars(
calendar_names: Iterator[str] = Depends(_lister),
) -> list[str]:
return list(calendar_names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_calendars(
calendar_names: Iterator[str] = Depends(_finder),
) -> list[str]:
return list(calendar_names)
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[CalEvent]:
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])
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"]
)