diff --git a/api/ovdashboard_api/dav_calendar.py b/api/ovdashboard_api/dav_calendar.py index fbc07af..ec82d5b 100644 --- a/api/ovdashboard_api/dav_calendar.py +++ b/api/ovdashboard_api/dav_calendar.py @@ -1,6 +1,7 @@ import logging from dataclasses import dataclass from datetime import datetime, timedelta +from functools import total_ordering from typing import Iterator from caldav import Calendar @@ -12,12 +13,19 @@ from . import caldav_principal, get_ttl_hash, run_in_executor, timed_alru_cache _logger = logging.getLogger(__name__) +@total_ordering class CalEvent(BaseModel): summary: str description: str dtstart: datetime dtend: datetime + def __lt__(self, other: "CalEvent") -> bool: + return self.dtstart < other.dtstart + + def __eq__(self, other: "CalEvent") -> bool: + return self.dict() == other.dict() + @timed_alru_cache(maxsize=20) async def _get_calendar( @@ -52,7 +60,7 @@ async def _get_calendar_events( for vevent in event.vobject_instance.contents["vevent"] ) - return [ + return sorted([ CalEvent( summary=vevent.summary.value, description=vevent.description.value, @@ -60,7 +68,7 @@ async def _get_calendar_events( dtend=vevent.dtend.value, ) for vevent in await _inner() - ] + ]) @dataclass(frozen=True)