From 9d1329d8da99ed2e61f9cd2bc01794ba94ed8169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 4 Sep 2022 20:59:45 +0000 Subject: [PATCH] sort calendar events --- api/ovdashboard_api/dav_calendar.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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)