summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2014-07-25 23:09:54 -0400
committerThomas Bruederli <bruederli@kolabsys.com>2014-07-25 23:09:54 -0400
commita60e0128a79fa02add67c517538946a772deaf23 (patch)
tree439f435f73b0c6015574ff304203d3faf2029410
parentf29b67ff6973447de96eeeb465884fc1a51c49ef (diff)
downloadpykolab-a60e0128a79fa02add67c517538946a772deaf23.tar.gz
Finish dump of event alarms as dict
-rw-r--r--pykolab/xml/event.py47
-rw-r--r--tests/unit/test-003-event.py51
2 files changed, 95 insertions, 3 deletions
diff --git a/pykolab/xml/event.py b/pykolab/xml/event.py
index 8e41a92..98436d9 100644
--- a/pykolab/xml/event.py
+++ b/pykolab/xml/event.py
@@ -7,6 +7,7 @@ import pytz
import time
import uuid
import base64
+import re
import pykolab
from pykolab import constants
@@ -56,6 +57,17 @@ class Event(object):
"CONFIDENTIAL": kolabformat.ClassConfidential,
}
+ alarm_type_map = {
+ 'EMAIL': kolabformat.Alarm.EMailAlarm,
+ 'DISPLAY': kolabformat.Alarm.DisplayAlarm,
+ 'AUDIO': kolabformat.Alarm.AudioAlarm
+ }
+
+ related_map = {
+ 'START': kolabformat.Start,
+ 'END': kolabformat.End
+ }
+
properties_map = {
# property: getter
"uid": "get_uid",
@@ -494,8 +506,7 @@ class Event(object):
if status in self.status_map.keys():
return status
- if status in self.status_map.values():
- return [k for k, v in self.status_map.iteritems() if v == status][0]
+ return self._translate_value(status, self.status_map)
def get_ical_sequence(self):
return str(self.event.sequence()) if self.event.sequence() else None
@@ -839,7 +850,7 @@ class Event(object):
elif isinstance(val, kolabformat.vectorattachment):
val = [dict(fmttype=x.mimetype(), label=x.label(), uri=x.uri()) for x in val]
elif isinstance(val, kolabformat.vectoralarm):
- val = [dict(type=x.type()) for x in val]
+ val = [self._alarm_to_dict(x) for x in val]
elif isinstance(val, list):
val = [x.to_dict() for x in val if hasattr(x, 'to_dict')]
@@ -848,6 +859,36 @@ class Event(object):
return data
+ def _alarm_to_dict(self, alarm):
+ ret = dict(
+ action=self._translate_value(alarm.type(), self.alarm_type_map),
+ summary=alarm.summary(),
+ description=alarm.description(),
+ trigger=None
+ )
+
+ start = alarm.start()
+ if start and start.isValid():
+ ret['trigger'] = xmlutils.from_cdatetime(start, True)
+ else:
+ ret['trigger'] = dict(related=self._translate_value(alarm.relativeTo(), self.related_map))
+ duration = alarm.relativeStart()
+ if duration and duration.isValid():
+ prefix = '-' if duration.isNegative() else '+'
+ value = prefix + "P%dW%dDT%dH%dM%dS" % (
+ duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds()
+ )
+ ret['trigger']['value'] = re.sub(r"T$", '', re.sub(r"0[WDHMS]", '', value))
+
+ if alarm.type() == kolabformat.Alarm.EMailAlarm:
+ ret['attendee'] = [ContactReference(a).to_dict() for a in alarm.attendees()]
+
+ return ret
+
+ def _translate_value(self, val, map):
+ name_map = dict([(v, k) for (k, v) in map.iteritems()])
+ return name_map[val] if name_map.has_key(val) else 'UNKNOWN'
+
def to_message(self):
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
diff --git a/tests/unit/test-003-event.py b/tests/unit/test-003-event.py
index 5017091..f069be3 100644
--- a/tests/unit/test-003-event.py
+++ b/tests/unit/test-003-event.py
@@ -190,6 +190,50 @@ xml_event = """
<value>BUSY</value>
</x-custom>
</properties>
+ <components>
+ <valarm>
+ <properties>
+ <action>
+ <text>DISPLAY</text>
+ </action>
+ <description>
+ <text>alarm 1</text>
+ </description>
+ <trigger>
+ <parameters>
+ <related>
+ <text>START</text>
+ </related>
+ </parameters>
+ <duration>-PT2H</duration>
+ </trigger>
+ </properties>
+ </valarm>
+ <valarm>
+ <properties>
+ <action>
+ <text>EMAIL</text>
+ </action>
+ <summary>
+ <text>test</text>
+ </summary>
+ <description>
+ <text>alarm 2</text>
+ </description>
+ <attendee>
+ <cal-address>mailto:%3Cjohn.die%40example.org%3E</cal-address>
+ </attendee>
+ <trigger>
+ <parameters>
+ <related>
+ <text>START</text>
+ </related>
+ </parameters>
+ <duration>-P1D</duration>
+ </trigger>
+ </properties>
+ </valarm>
+ </components>
</vevent>
</components>
</vcalendar>
@@ -520,6 +564,13 @@ END:VEVENT
self.assertEqual(data['rrule']['wkst'], 'MO')
self.assertIsInstance(data['rrule']['until'], datetime.date)
+ self.assertIsInstance(data['alarm'], list)
+ self.assertEqual(len(data['alarm']), 2)
+ self.assertEqual(data['alarm'][0]['action'], 'DISPLAY')
+ self.assertEqual(data['alarm'][1]['action'], 'EMAIL')
+ self.assertEqual(data['alarm'][1]['trigger']['value'], '-P1D')
+ self.assertEqual(len(data['alarm'][1]['attendee']), 1)
+
if __name__ == '__main__':
unittest.main()