From a20e88dbed93afb9abeceb9d76e8d6ab6b3356fe Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Sat, 29 Jan 2022 09:07:29 +0100
Subject: [PATCH] Moved functions to Event class

---
 saturn/calendar/__init__.py        | 15 +-----
 saturn/calendar/event.py           | 75 ++++++++++++++++++------------
 saturn/server/templates/index.html |  6 +--
 3 files changed, 51 insertions(+), 45 deletions(-)

diff --git a/saturn/calendar/__init__.py b/saturn/calendar/__init__.py
index 77e0cfd..b6c8e70 100644
--- a/saturn/calendar/__init__.py
+++ b/saturn/calendar/__init__.py
@@ -13,24 +13,13 @@ def get_davclient(config):
         password=config.get('password', fallback=None),
     )
 
-def get_comparable_dt(event):
-    if type(event.dtstart.value) == date:
-        return datetime.combine(
-            event.dtstart.value,
-            time.min,
-        ).timestamp()
-    else:
-        return event.dtstart.value.timestamp()
-
 def get_events(davclient):
     pri = davclient.principal()
 
     events = []
-    events_day = []
 
     for cal in pri.calendars():
-        events += [event.instance.vevent
+        events += [Event(event)
             for event in cal.date_search(start=date.today())]
-    events.sort(key=get_comparable_dt)
     
-    return [Event(event) for event in events]
+    return sorted(events)
diff --git a/saturn/calendar/event.py b/saturn/calendar/event.py
index ee2e240..c022b2d 100644
--- a/saturn/calendar/event.py
+++ b/saturn/calendar/event.py
@@ -1,65 +1,82 @@
 import re
 import datetime
+import html
 
 
 class Event:
     def __init__(
                 self,
-                vobject,
+                event,
             ):
-        for attr in [
-                    'dtstart',
-                    'dtend',
-                    'summary',
-                    'description',
-                    'location',
-                    'rrule',
-                ]:
-            setattr(self, f'_{attr}',
-                    getattr(getattr(vobject, attr, None), 'value', None))
+        self._v = event.instance.vevent
 
     @property
     def dtstart(self):
-        return self._dtstart
+        return self._v.dtstart.value
 
     @property
     def dtend(self):
-        return self._dtend
+        return self._v.dtend.value
 
     @property
     def summary(self):
-        return self._summary
+        return self._v.summary.value
     
     @property
     def description(self):
-        if self._description is None: return None
-        return re.sub(
-            r'(https?://\S+)',
-            r'<a href="\1">\1</a>',
-            self._description,
-        )
+        if hasattr(self._v, 'description'):
+            return self._v.description.value
+        return None
 
     @property
     def location(self):
-        return self._location
+        if hasattr(self._v, 'location'):
+            return self._v.location.value
+        return None
 
     @property
+    def rrule(self):
+        if hasattr(self._v, 'rrule'):
+            return self._v.rrule.value
+        return None
+
+
+    def fdescription(self):
+        if self.description is None: return None
+        return re.sub(
+            r'(https?://\S+)',
+            r'<a href="\1">\1</a>',
+            html.escape(self.description),
+        )
+
     def freq(self):
-        if self._rrule is None: return None
+        if self.rrule is None: return None
         return re.search(
             r'FREQ=([A-Z]+?);',
-            self._rrule,
+            self.rrule,
         ).group(1)
 
+
     def is_full_day(self):
-        return type(self._dtstart) is datetime.date
+        return type(self.dtstart) is datetime.date
 
     def is_one_day(self):
         if self.is_full_day():
-            return self._dtend - self._dtstart <= datetime.timedelta(days=1)
-        return (self._dtend.time() == datetime.time.min \
-            and self._dtend - self._dtstart <= datetime.timedelta(days=1)) \
-            or self._dtend.date() == self._dtstart.date()
+            return self.dtend - self.dtstart <= datetime.timedelta(days=1)
+        return (self.dtend.time() == datetime.time.min \
+            and self.dtend - self.dtstart <= datetime.timedelta(days=1)) \
+            or self.dtend.date() == self.dtstart.date()
 
     def is_one_month(self):
-        return self._dtend.month == self._dtstart.month
+        return self.dtend.month == self.dtstart.month
+
+    def __lt__(self, other):
+        if self.is_full_day() == other.is_full_day():
+            return self.dtstart < other.dtstart
+        if other.is_full_day():
+            return not other.__lt__(self)
+        return datetime.datetime.combine(
+            self.dtstart,
+            datetime.time.min,
+            tzinfo=other.dtstart.tzinfo
+        ) < other.dtstart
diff --git a/saturn/server/templates/index.html b/saturn/server/templates/index.html
index 240905a..3648ad2 100644
--- a/saturn/server/templates/index.html
+++ b/saturn/server/templates/index.html
@@ -61,7 +61,7 @@
 		<div class="info">
 			<div class="description">
 				{% if event.description %}
-				{{ event.description | safe }}
+				{{ event.fdescription() | safe }}
 				{% endif %}
 			</div>
 			<div class="other">
@@ -71,10 +71,10 @@
 					{{ event.location }}
 				</div>
 				{% endif %}
-				{% if event.freq %}
+				{% if event.rrule %}
 				<div>
 					<i class="ri-repeat-line ri-fw ri-sm"></i>
-					{{ event.freq}}
+					{{ event.freq() }}
 				</div>
 				{% endif %}
 			</div>
