Moved functions to Event class

This commit is contained in:
Edgar P. Burkhart 2022-01-29 09:07:29 +01:00
parent 2492438164
commit a20e88dbed
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 51 additions and 45 deletions

View file

@ -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)

View file

@ -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

View file

@ -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>