Moved functions to Event class
This commit is contained in:
parent
2492438164
commit
a20e88dbed
3 changed files with 51 additions and 45 deletions
|
@ -13,24 +13,13 @@ def get_davclient(config):
|
||||||
password=config.get('password', fallback=None),
|
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):
|
def get_events(davclient):
|
||||||
pri = davclient.principal()
|
pri = davclient.principal()
|
||||||
|
|
||||||
events = []
|
events = []
|
||||||
events_day = []
|
|
||||||
|
|
||||||
for cal in pri.calendars():
|
for cal in pri.calendars():
|
||||||
events += [event.instance.vevent
|
events += [Event(event)
|
||||||
for event in cal.date_search(start=date.today())]
|
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)
|
||||||
|
|
|
@ -1,65 +1,82 @@
|
||||||
import re
|
import re
|
||||||
import datetime
|
import datetime
|
||||||
|
import html
|
||||||
|
|
||||||
|
|
||||||
class Event:
|
class Event:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
vobject,
|
event,
|
||||||
):
|
):
|
||||||
for attr in [
|
self._v = event.instance.vevent
|
||||||
'dtstart',
|
|
||||||
'dtend',
|
|
||||||
'summary',
|
|
||||||
'description',
|
|
||||||
'location',
|
|
||||||
'rrule',
|
|
||||||
]:
|
|
||||||
setattr(self, f'_{attr}',
|
|
||||||
getattr(getattr(vobject, attr, None), 'value', None))
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dtstart(self):
|
def dtstart(self):
|
||||||
return self._dtstart
|
return self._v.dtstart.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dtend(self):
|
def dtend(self):
|
||||||
return self._dtend
|
return self._v.dtend.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def summary(self):
|
def summary(self):
|
||||||
return self._summary
|
return self._v.summary.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def description(self):
|
def description(self):
|
||||||
if self._description is None: return None
|
if hasattr(self._v, 'description'):
|
||||||
return re.sub(
|
return self._v.description.value
|
||||||
r'(https?://\S+)',
|
return None
|
||||||
r'<a href="\1">\1</a>',
|
|
||||||
self._description,
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def location(self):
|
def location(self):
|
||||||
return self._location
|
if hasattr(self._v, 'location'):
|
||||||
|
return self._v.location.value
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@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):
|
def freq(self):
|
||||||
if self._rrule is None: return None
|
if self.rrule is None: return None
|
||||||
return re.search(
|
return re.search(
|
||||||
r'FREQ=([A-Z]+?);',
|
r'FREQ=([A-Z]+?);',
|
||||||
self._rrule,
|
self.rrule,
|
||||||
).group(1)
|
).group(1)
|
||||||
|
|
||||||
|
|
||||||
def is_full_day(self):
|
def is_full_day(self):
|
||||||
return type(self._dtstart) is datetime.date
|
return type(self.dtstart) is datetime.date
|
||||||
|
|
||||||
def is_one_day(self):
|
def is_one_day(self):
|
||||||
if self.is_full_day():
|
if self.is_full_day():
|
||||||
return self._dtend - self._dtstart <= datetime.timedelta(days=1)
|
return self.dtend - self.dtstart <= datetime.timedelta(days=1)
|
||||||
return (self._dtend.time() == datetime.time.min \
|
return (self.dtend.time() == datetime.time.min \
|
||||||
and self._dtend - self._dtstart <= datetime.timedelta(days=1)) \
|
and self.dtend - self.dtstart <= datetime.timedelta(days=1)) \
|
||||||
or self._dtend.date() == self._dtstart.date()
|
or self.dtend.date() == self.dtstart.date()
|
||||||
|
|
||||||
def is_one_month(self):
|
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
|
||||||
|
|
|
@ -61,7 +61,7 @@
|
||||||
<div class="info">
|
<div class="info">
|
||||||
<div class="description">
|
<div class="description">
|
||||||
{% if event.description %}
|
{% if event.description %}
|
||||||
{{ event.description | safe }}
|
{{ event.fdescription() | safe }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<div class="other">
|
<div class="other">
|
||||||
|
@ -71,10 +71,10 @@
|
||||||
{{ event.location }}
|
{{ event.location }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if event.freq %}
|
{% if event.rrule %}
|
||||||
<div>
|
<div>
|
||||||
<i class="ri-repeat-line ri-fw ri-sm"></i>
|
<i class="ri-repeat-line ri-fw ri-sm"></i>
|
||||||
{{ event.freq}}
|
{{ event.freq() }}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
Reference in a new issue