summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pykolab/xml/contact_reference.py3
-rw-r--r--pykolab/xml/event.py185
-rw-r--r--wallace/module_resources.py71
3 files changed, 155 insertions, 104 deletions
diff --git a/pykolab/xml/contact_reference.py b/pykolab/xml/contact_reference.py
index e3fbcb2..0d6dec5 100644
--- a/pykolab/xml/contact_reference.py
+++ b/pykolab/xml/contact_reference.py
@@ -23,6 +23,9 @@ class ContactReference(kolabformat.ContactReference):
def get_name(self):
return self.name()
+ def set_cn(self, value):
+ self.setName(value)
+
def set_email(self, email):
kolabformat.ContactReference.__init__(self, email)
diff --git a/pykolab/xml/event.py b/pykolab/xml/event.py
index f516ead..cc99c78 100644
--- a/pykolab/xml/event.py
+++ b/pykolab/xml/event.py
@@ -1,5 +1,7 @@
import datetime
import icalendar
+from icalendar import vDatetime
+from icalendar import vText
import kolabformat
import time
@@ -71,33 +73,45 @@ class Event(object):
for attr in list(set(event.singletons)):
if hasattr(self, 'get_ical_%s' % (attr.lower())):
exec("retval = self.get_ical_%s()" % (attr.lower()))
+
+ #print "as_string_itip()", attr, retval, type(retval)
+
if not retval == None and not retval == "":
+ print attr.lower()
event.add(attr.lower(), retval)
elif hasattr(self, 'get_%s' % (attr.lower())):
exec("retval = self.get_%s()" % (attr.lower()))
+
+ #print "as_string_itip()", attr, retval
+
if not retval == None and not retval == "":
- event.add(attr.lower(), retval)
+ event.add(attr.lower(), retval, encode=0)
#else:
- #print "no function for", attr.lower()
+ #print "(single) no function for", attr.lower()
# NOTE: Make sure to list(set()) or duplicates may arise
for attr in list(set(event.multiple)):
if hasattr(self, 'get_ical_%s' % (attr.lower())):
exec("retval = self.get_ical_%s()" % (attr.lower()))
+
+ print "as_string_itip()", attr, retval
+
if isinstance(retval, list) and not len(retval) == 0:
for _retval in retval:
- event.add(attr.lower(), _retval)
+ #print _retval.params
+ event.add(attr.lower(), _retval, encode=0)
elif hasattr(self, 'get_%s' % (attr.lower())):
exec("retval = self.get_%s()" % (attr.lower()))
+ print attr, retval
if isinstance(retval, list) and not len(retval) == 0:
for _retval in retval:
- event.add(attr.lower(), _retval)
+ event.add(attr.lower(), _retval, encode=0)
#else:
- #print "no function for", attr.lower()
+ #print "(multiple) no function for", attr.lower()
#event.add('attendee', self.get_attendees())
@@ -132,26 +146,20 @@ class Event(object):
# to fail substitution.
for attr in list(set(ical_event.required)):
if ical_event.has_key(attr):
- if hasattr(self, 'set_ical_%s' % (attr.lower())):
- exec("self.set_ical_%s(%r)" % (attr.lower(),ical_event.decoded(attr)))
- else:
- print attr, "exists but no function exists"
+ self.set_from_ical(attr.lower(), ical_event[attr])
# NOTE: Make sure to list(set()) or duplicates may arise
for attr in list(set(ical_event.singletons)):
if ical_event.has_key(attr):
- if hasattr(self, 'set_ical_%s' % (attr.lower())):
- exec("self.set_ical_%s(%r)" % (attr.lower(),ical_event.decoded(attr)))
- else:
- print attr, "exists but no function exists"
+ self.set_from_ical(attr.lower(), ical_event[attr])
# NOTE: Make sure to list(set()) or duplicates may arise
for attr in list(set(ical_event.multiple)):
if ical_event.has_key(attr):
- if hasattr(self, 'set_ical_%s' % (attr.lower())):
- exec("self.set_ical_%s(%r)" % (attr.lower(),ical_event.decoded(attr)))
- else:
- print attr, "exists but no function exists"
+ #if attr == "ATTENDEE":
+ #print ical_event.decoded(attr)
+
+ self.set_from_ical(attr.lower(), ical_event[attr])
def get_attendee_participant_status(self, attendee):
return attendee.get_participant_status()
@@ -251,13 +259,15 @@ class Event(object):
elif partstat == kolabformat.PartDelegated:
_partstat = "DELEGATED"
- _attendee = "RSVP=%s" % _rsvp
- _attendee += ";PARTSTAT=%s" % _partstat
- _attendee += ";ROLE=%s" % _role
- _attendee += ";MAILTO:%s" % contact.email()
+ _attendee = icalendar.vCalAddress("MAILTO:%s" % contact.email())
+ _attendee.params['RSVP'] = icalendar.vText(_rsvp)
+ _attendee.params['PARTSTAT'] = icalendar.vText(_partstat)
+ _attendee.params['ROLE'] = icalendar.vText(_role)
attendees.append(_attendee)
+ #print "get_ical_attendees()", attendees
+
return attendees
def get_ical_created(self):
@@ -279,20 +289,28 @@ class Event(object):
return self.get_start()
def get_ical_organizer(self):
- organizer = self.get_organizer()
- name = organizer.name()
+ contact = self.get_organizer()
+ organizer = icalendar.vCalAddress("MAILTO:%s" % contact.email())
+ name = contact.name()
- if not name:
- return "mailto:%s" % (organizer.email())
- else:
- return "CN=%s:mailto:%s" %(name, organizer.email())
+ if not name == None and not name == "":
+ organizer.params["CN"] = icalendar.vText(name)
+
+ return organizer
def get_ical_status(self):
status = self.event.status()
- for key in self.status_map.keys():
- if status == self.status_map[key]:
- return key
+ #print "get_ical_status()", status
+ #print self.status_map.keys()
+ #print self.status_map.values()
+ 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]
+
+ #print "get_ical_status()", status
def get_organizer(self):
organizer = self.event.organizer()
@@ -303,6 +321,7 @@ class Event(object):
return self.event.priority()
def get_start(self):
+ #print "get_start()"
_datetime = self.event.start()
(
@@ -416,42 +435,63 @@ class Event(object):
kolabformat.cDateTime(year, month, day, hour, minute, second)
)
+ def set_from_ical(self, attr, value):
+ if attr == "dtend":
+ self.set_ical_dtend(value.dt)
+ elif attr == "dtstart":
+ self.set_ical_dtstart(value.dt)
+ elif attr == "status":
+ self.set_ical_status(value)
+ elif attr == "summary":
+ self.set_ical_summary(value)
+ elif attr == "priority":
+ self.set_ical_priority(value)
+ elif attr == "attendee":
+ self.set_ical_attendee(value)
+ elif attr == "organizer":
+ self.set_ical_organizer(value)
+ elif attr == "uid":
+ self.set_ical_uid(value)
+
+ else:
+ print "WARNING, no function for", attr
+
def set_ical_attendee(self, _attendee):
log.debug(_("set attendees from ical: %r") % (_attendee), level=9)
+ if isinstance(_attendee, basestring):
+ _attendee = [_attendee]
+
if isinstance(_attendee, list):
for attendee in _attendee:
- #print attendee
- rsvp = False
- role = None
- cn = None
- partstat = None
- address = None
- for param in attendee.split(';'):
- if (len(param.split('=')) > 1):
- exec("%s = %r" % (param.split('=')[0].lower(), param.split('=')[1]))
- #print "%s = %r" % (param.split('=')[0].lower(), param.split('=')[1])
- if (len(attendee.split(':')) > 1):
- address = attendee.split(':')[-1]
- #print address
-
- self.add_attendee(address, name=cn, rsvp=rsvp, role=role, participant_status=partstat)
- else:
- #print attendee
- rsvp = False
- role = None
- cn = None
- partstat = None
- address = None
- for param in _attendee.split(';'):
- if (len(param.split('=')) > 1):
- exec("%s = %r" % (param.split('=')[0].lower(), param.split('=')[1]))
- #print "%s = %r" % (param.split('=')[0].lower(), param.split('=')[1])
- if (len(_attendee.split(':')) > 1):
- address = _attendee.split(':')[-1]
- #print address
-
- self.add_attendee(address, name=cn, rsvp=rsvp, role=role, participant_status=partstat)
+ address = str(attendee).split(':')[-1]
+
+ if hasattr(attendee, 'params'):
+ params = attendee.params
+ else:
+ params = {}
+
+ if params.has_key('CN'):
+ name = params['CN']
+ else:
+ name = None
+
+ if params.has_key('ROLE'):
+ role = params['ROLE']
+ else:
+ role = None
+
+ if params.has_key('PARTSTAT'):
+ partstat = params['PARTSTAT']
+ else:
+ partstat = None
+
+ if params.has_key('RSVP'):
+ rsvp = params['RSVP']
+ else:
+ rsvp = None
+
+ self.add_attendee(address, name=name, rsvp=rsvp, role=role, participant_status=partstat)
def set_ical_dtend(self, dtend):
self.set_end(dtend)
@@ -463,23 +503,26 @@ class Event(object):
self.set_start(dtstart)
def set_ical_organizer(self, organizer):
+ address = str(organizer).split(':')[-1]
+
cn = None
- address = None
- #print organizer
- for param in organizer.split(':'):
- if (len(param.split('=')) > 1):
- exec("%s = %r" % (param.split('=')[0].lower(), param.split('=')[1]))
- #print "%s = %r" % (param.split('=')[0].lower(), param.split('=')[1])
- if (len(organizer.split(':')) > 1):
- address = organizer.split(':')[-1]
- #print address
- self.set_organizer(address, name=cn)
+ if hasattr(organizer, 'params'):
+ params = organizer.params
+ else:
+ params = {}
+
+ if params.has_key('CN'):
+ cn = params['CN']
+
+ self.set_organizer(str(address), name=cn)
def set_ical_priority(self, priority):
self.set_priority(priority)
def set_ical_status(self, status):
+ #print "set_ical_status()", status
+
# TODO: See which ones are actually valid for iTip
if status == "UNDEFINED":
_status = kolabformat.StatusUndefined
diff --git a/wallace/module_resources.py b/wallace/module_resources.py
index c9be1f1..52156d4 100644
--- a/wallace/module_resources.py
+++ b/wallace/module_resources.py
@@ -120,7 +120,7 @@ def execute(*args, **kw):
if not len(itip_events) > 0:
log.info(
_("Message is not an iTip message or does not contain any " + \
- "iTip.")
+ "(valid) iTip.")
)
accept(filepath)
@@ -200,7 +200,7 @@ def execute(*args, **kw):
if event_message.is_multipart():
for part in event_message.walk():
if part.get_content_type() == "application/calendar+xml":
- payload = part.get_payload()
+ payload = part.get_payload(decode=True)
event = pykolab.xml.event_from_string(payload)
for itip in itip_events:
@@ -209,34 +209,36 @@ def execute(*args, **kw):
log.debug(_(" event %r end: %r") % (event.get_uid(),event.get_end()), level=9)
_es = event.get_start()
- _is = itip['start']
+ _is = itip['start'].dt
- if type(_es) == 'datetime.date':
+ if type(_es) == 'datetime.date' or not hasattr(_es, 'hour'):
log.debug(_("_es is datetime.date"))
- if type(_is) == 'datetime.datetime':
- _is = datetime.date(_is.year, _is.month, _is.day)
- else:
- pass
- else:
- log.debug(_("_es is datetime.datetime"))
- if type(_is) == 'datetime.date':
- log.debug(_("_is is datetime.date"))
- _es = datetime.date(_es.year, _es.month, _es.day)
+ _es = datetime.datetime(_es.year, _es.month, _es.day, 0, 0, 0)
+
+ if type(_is) == 'datetime.date' or not hasattr(_is, 'hour'):
+ log.debug(_("_is is datetime.date"))
+ _is = datetime.datetime(_is.year, _is.month, _is.day, 0, 0, 0)
_ee = event.get_end()
- _ie = itip['end']
- if type(_ee) == 'datetime.date':
- if type(_ie) == 'datetime.datetime':
- _ie = datetime.date(_ie.year, _ie.month, _ie.day)
- else:
- pass
- else:
- if type(_ie) == 'datetime.date':
- _ee = datetime.date(_ee.year, _ee.month, _ee.day)
+ _ie = itip['end'].dt
+
+ if type(_ee) == 'datetime.date' or not hasattr(_ee, 'hour'):
+ log.debug(_("_ee is datetime.date"))
+ _ee = datetime.datetime(_ee.year, _ee.month, _ee.day, 0, 0, 0)
+
+ if type(_ie) == 'datetime.date' or not hasattr(_ie, 'hour'):
+ log.debug(_("_ie is datetime.date"))
+ _ie = datetime.datetime(_ie.year, _ie.month, _ie.day, 0, 0, 0)
+
+ log.debug(_("Raw event and itip data:"))
+ log.debug(_("_es: %r") %(_es))
+ log.debug(_("_is: %r") %(_is))
+ log.debug(_("_ee: %r") %(_ee))
+ log.debug(_("_ie: %r") %(_ie))
if _es < _is:
if _es <= _ie:
- if _ie <= _is:
+ if _ee <= _is:
conflict = False
else:
log.debug(_("Event %r ends later than invitation") % (event.get_uid()), level=9)
@@ -373,9 +375,7 @@ def itip_events_from_message(message):
cal = icalendar.Calendar.from_string(itip_payload)
else:
log.error(_("Could not read iTip from message."))
- accept(filepath)
-
- return
+ return []
for c in cal.walk():
itip = {}
@@ -391,17 +391,22 @@ def itip_events_from_message(message):
# - TODO: recurrence rules (if any)
# Where are these stored actually?
#
- itip['start'] = c.decoded('dtstart')
if c.has_key('dtend'):
- itip['end'] = c.decoded('dtend')
+ itip['start'] = c['dtstart']
+ else:
+ log.error(_("iTip event without a start"))
+ return []
+
+ if c.has_key('dtend'):
+ itip['end'] = c['dtend']
if c.has_key('duration'):
- itip['duration'] = c.decoded('duration')
- itip['organizer'] = c.decoded('organizer')
- itip['attendees'] = c.decoded('attendee')
+ itip['duration'] = c['duration']
+ itip['organizer'] = c['organizer']
+ itip['attendees'] = c['attendee']
if c.has_key('resources'):
- itip['resources'] = c.decoded('resources')
+ itip['resources'] = c['resources']
itip['raw'] = itip_payload
- itip['xml'] = event_from_ical(c.__str__())
+ itip['xml'] = event_from_ical(c.to_ical())
itip_events.append(itip)
else:
log.error(