summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2015-02-10 11:49:06 +0100
committerThomas Bruederli <bruederli@kolabsys.com>2015-02-10 11:49:06 +0100
commitc41acdb7336bbd38c8721a0ecce2fbabe03502ed (patch)
treeb145bec471f2552ad138a87b064e71ff30004a79
parenta214f29290f4d5e5a2d62a8a549679c8a4c0fe51 (diff)
downloadpykolab-c41acdb7336bbd38c8721a0ecce2fbabe03502ed.tar.gz
Import RRULE properties from iCal VTODO objects
-rw-r--r--pykolab/xml/event.py1
-rw-r--r--pykolab/xml/recurrence_rule.py70
-rw-r--r--pykolab/xml/todo.py7
-rw-r--r--tests/unit/test-016-todo.py8
4 files changed, 86 insertions, 0 deletions
diff --git a/pykolab/xml/event.py b/pykolab/xml/event.py
index cb2b876..6242eaf 100644
--- a/pykolab/xml/event.py
+++ b/pykolab/xml/event.py
@@ -60,6 +60,7 @@ class Event(object):
type = 'event'
status_map = {
+ None: kolabformat.StatusUndefined,
"TENTATIVE": kolabformat.StatusTentative,
"CONFIRMED": kolabformat.StatusConfirmed,
"CANCELLED": kolabformat.StatusCancelled,
diff --git a/pykolab/xml/recurrence_rule.py b/pykolab/xml/recurrence_rule.py
index 13e8d67..a82fec7 100644
--- a/pykolab/xml/recurrence_rule.py
+++ b/pykolab/xml/recurrence_rule.py
@@ -1,3 +1,5 @@
+import icalendar
+import datetime
import kolabformat
from pykolab.xml import utils as xmlutils
@@ -91,18 +93,86 @@ class RecurrenceRule(kolabformat.RecurrenceRule):
else:
kolabformat.RecurrenceRule.__init__(self, rrule)
+ def from_ical(self, vrecur):
+ vectorimap = {
+ 'BYSECOND': 'setBysecond',
+ 'BYMINUTE': 'setByminute',
+ 'BYHOUR': 'setByhour',
+ 'BYMONTHDAY': 'setBymonthday',
+ 'BYYEARDAY': 'setByyearday',
+ 'BYMONTH': 'setBymonth',
+ }
+
+ settermap = {
+ 'FREQ': 'set_frequency',
+ 'INTERVAL': 'set_interval',
+ 'COUNT': 'set_count',
+ 'UNTIL': 'set_until',
+ 'WKST': 'set_weekstart',
+ 'BYDAY': 'set_byday',
+ }
+
+ for prop,setter in vectorimap.items():
+ if vrecur.has_key(prop):
+ getattr(self, setter)([int(v) for v in vrecur[prop]])
+
+ for prop,setter in settermap.items():
+ if vrecur.has_key(prop):
+ getattr(self, setter)(vrecur[prop])
+
+ def set_count(self, count):
+ if isinstance(count, list):
+ count = count[0]
+ self.setCount(int(count))
+
+ def set_interval(self, val):
+ if isinstance(val, list):
+ val = val[0]
+ self.setInterval(int(val))
+
+ def set_frequency(self, freq):
+ self._set_map_value(freq, self.frequency_map, 'setFrequency')
+
def get_frequency(self, translated=False):
freq = self.frequency()
if translated:
return self._translate_value(freq, self.frequency_map)
return freq
+ def set_byday(self, bdays):
+ daypos = kolabformat.vectordaypos()
+ for wday in bdays:
+ weekday = str(wday)[-2:]
+ occurrence = int(wday.relative)
+ if str(wday)[0] == '-':
+ occurrence = occurrence * -1
+ if self.weekday_map.has_key(weekday):
+ daypos.append(kolabformat.DayPos(occurrence, self.weekday_map[weekday]))
+ self.setByday(daypos)
+
+ def set_weekstart(self, wkst):
+ self._set_map_value(wkst, self.weekday_map, 'setWeekStart')
+
def get_weekstart(self, translated=False):
wkst = self.weekStart()
if translated:
return self._translate_value(wkst, self.weekday_map)
return wkst
+ def set_until(self, until):
+ if isinstance(until, list):
+ until = until[0]
+ if isinstance(until, datetime.datetime) or isinstance(until, datetime.date):
+ self.setEnd(xmlutils.to_cdatetime(until, True))
+
+ def _set_map_value(self, val, pmap, setter):
+ if isinstance(val, list):
+ val = val[0]
+ if val in pmap.keys():
+ getattr(self, setter)(pmap[val])
+ elif val in pmap.values():
+ getattr(self, setter)(val)
+
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'
diff --git a/pykolab/xml/todo.py b/pykolab/xml/todo.py
index 5859cb2..303dd63 100644
--- a/pykolab/xml/todo.py
+++ b/pykolab/xml/todo.py
@@ -7,6 +7,7 @@ import base64
import pykolab
from pykolab import constants
from pykolab.xml import Event
+from pykolab.xml import RecurrenceRule
from pykolab.xml import utils as xmlutils
from pykolab.xml.event import InvalidEventDateError
from pykolab.translate import _
@@ -131,6 +132,12 @@ class Todo(Event):
vattach.append(_attachment)
self.event.setAttachments(vattach)
+ def set_ical_rrule(self, rrule):
+ _rrule = RecurrenceRule()
+ _rrule.from_ical(rrule)
+ if _rrule.isValid():
+ self.event.setRecurrenceRule(_rrule)
+
def set_ical_due(self, due):
self.set_due(due)
diff --git a/tests/unit/test-016-todo.py b/tests/unit/test-016-todo.py
index dcb89c5..91562c3 100644
--- a/tests/unit/test-016-todo.py
+++ b/tests/unit/test-016-todo.py
@@ -26,6 +26,7 @@ CREATED;VALUE=DATE-TIME:20140731T100704Z
LAST-MODIFIED;VALUE=DATE-TIME:20140820T101333Z
DTSTART;VALUE=DATE-TIME;TZID=Europe/London:20140818T180000
DUE;VALUE=DATE-TIME;TZID=Europe/London:20140822T133000
+RRULE:FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=2MO,-1WE;UNTIL=20150220T180000Z
SUMMARY:Sample Task assignment
DESCRIPTION:Summary: Sample Task assignment\\nDue Date: 08/11/14\\nDue Time:
\\n13:30 AM
@@ -241,6 +242,13 @@ METHOD:REQUEST
self.assertEqual(todo.get_percentcomplete(), 20)
#print str(todo)
+ data = todo.to_dict()
+ self.assertIsInstance(data['rrule'], dict)
+ self.assertEqual(data['rrule']['freq'], 'MONTHLY')
+ self.assertEqual(data['rrule']['interval'], 2)
+ self.assertEqual(data['rrule']['byday'], '2MO,-1WE')
+ self.assertIsInstance(data['rrule']['until'], datetime.datetime)
+
def test_021_as_string_itip(self):
self.todo.set_summary("test")
self.todo.set_start(datetime.datetime(2014, 9, 20, 11, 00, 00, tzinfo=pytz.timezone("Europe/London")))