sort calendar events

This commit is contained in:
Jörn-Michael Miehe 2022-09-04 20:59:45 +00:00
parent 608f62b090
commit 9d1329d8da

View file

@ -1,6 +1,7 @@
import logging import logging
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import total_ordering
from typing import Iterator from typing import Iterator
from caldav import Calendar 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__) _logger = logging.getLogger(__name__)
@total_ordering
class CalEvent(BaseModel): class CalEvent(BaseModel):
summary: str summary: str
description: str description: str
dtstart: datetime dtstart: datetime
dtend: 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) @timed_alru_cache(maxsize=20)
async def _get_calendar( async def _get_calendar(
@ -52,7 +60,7 @@ async def _get_calendar_events(
for vevent in event.vobject_instance.contents["vevent"] for vevent in event.vobject_instance.contents["vevent"]
) )
return [ return sorted([
CalEvent( CalEvent(
summary=vevent.summary.value, summary=vevent.summary.value,
description=vevent.description.value, description=vevent.description.value,
@ -60,7 +68,7 @@ async def _get_calendar_events(
dtend=vevent.dtend.value, dtend=vevent.dtend.value,
) )
for vevent in await _inner() for vevent in await _inner()
] ])
@dataclass(frozen=True) @dataclass(frozen=True)