2022-09-03 02:00:07 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
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
|
2022-09-02 19:21:15 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/calendar", tags=["calendar"])
|
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
_lister = CalendarNameLister()
|
|
|
|
_finder = PrefixFinder(_lister)
|
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
|
|
|
calendar_names: Iterator[str] = Depends(_lister),
|
2022-09-03 02:00:07 +00:00
|
|
|
) -> list[str]:
|
|
|
|
return list(calendar_names)
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/find/{prefix}", response_model=list[str])
|
|
|
|
async def find_calendars(
|
2022-09-04 14:14:22 +00:00
|
|
|
calendar_names: Iterator[str] = Depends(_finder),
|
2022-09-03 02:00:07 +00:00
|
|
|
) -> list[str]:
|
|
|
|
return list(calendar_names)
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/get/{prefix}", response_model=list[str])
|
|
|
|
async def get_calendar(
|
2022-09-04 14:14:22 +00:00
|
|
|
calendar_names: Iterator[str] = Depends(_finder),
|
2022-09-03 02:00:07 +00:00
|
|
|
) -> list[str]:
|
|
|
|
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)
|
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
calendar = caldav_principal().calendar(name=calendar_names[0])
|
2022-09-03 02:00:07 +00:00
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
events = []
|
|
|
|
|
|
|
|
search_results = calendar.date_search(
|
2022-09-03 02:00:07 +00:00
|
|
|
start=datetime.now(),
|
|
|
|
end=datetime.now() + timedelta(days=365),
|
|
|
|
expand=True,
|
|
|
|
)
|
|
|
|
|
2022-09-04 14:14:22 +00:00
|
|
|
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
|