diff options
author | Thomas Bruederli <bruederli@kolabsys.com> | 2014-07-07 17:40:54 -0400 |
---|---|---|
committer | Thomas Bruederli <bruederli@kolabsys.com> | 2014-07-07 17:40:54 -0400 |
commit | 5de26915e7b66db1dd139b1b6d33687f7e9dfc66 (patch) | |
tree | 0b1ef3403441d87ce64ed4691d7bb9ffec57faea /pykolab | |
parent | 0438fc64173d5e68822b7cf922ba807e1cabf95a (diff) | |
download | pykolab-5de26915e7b66db1dd139b1b6d33687f7e9dfc66.tar.gz |
Fix event object reading and writing: store attachments as separate MIME parts and forward them to the new MIME message written
Diffstat (limited to 'pykolab')
-rw-r--r-- | pykolab/xml/__init__.py | 1 | ||||
-rw-r--r-- | pykolab/xml/event.py | 57 |
2 files changed, 56 insertions, 2 deletions
diff --git a/pykolab/xml/__init__.py b/pykolab/xml/__init__.py index 5ca2837..64b06ae 100644 --- a/pykolab/xml/__init__.py +++ b/pykolab/xml/__init__.py @@ -9,6 +9,7 @@ from event import EventIntegrityError from event import InvalidEventDateError from event import event_from_ical from event import event_from_string +from event import event_from_message from utils import to_dt diff --git a/pykolab/xml/event.py b/pykolab/xml/event.py index fcb3a17..39034f6 100644 --- a/pykolab/xml/event.py +++ b/pykolab/xml/event.py @@ -6,6 +6,7 @@ import kolabformat import pytz import time import uuid +import base64 import pykolab from pykolab import constants @@ -13,6 +14,7 @@ from pykolab import utils from pykolab.xml import utils as xmlutils from pykolab.translate import _ +from os import path from attendee import Attendee from contact_reference import ContactReference @@ -24,6 +26,21 @@ def event_from_ical(string): def event_from_string(string): return Event(from_string=string) +def event_from_message(message): + event = None + if message.is_multipart(): + for part in message.walk(): + if part.get_content_type() == "application/calendar+xml": + payload = part.get_payload(decode=True) + event = event_from_string(payload) + + # append attachment parts to Event object + elif event and part.has_key('Content-ID'): + event._attachment_parts.append(part) + + return event + + class Event(object): status_map = { "TENTATIVE": kolabformat.StatusTentative, @@ -40,6 +57,7 @@ class Event(object): def __init__(self, from_ical="", from_string=""): self._attendees = [] self._categories = [] + self._attachment_parts = [] if from_ical == "": if from_string == "": @@ -751,15 +769,50 @@ class Event(object): msg["Subject"] = self.get_uid() - part.set_payload(str(self)) + # extract attachment data into separate MIME parts + vattach = self.event.attachments() + i = 0 + for attach in vattach: + if attach.uri(): + continue + + mimetype = attach.mimetype() + (primary, seconday) = mimetype.split('/') + name = attach.label() + if not name: + name = 'unknown.x' - # TODO: extract attachment data to separate MIME parts + (basename, suffix) = path.splitext(name) + t = datetime.datetime.now() + cid = "%s.%s.%s%s" % (basename, time.mktime(t.timetuple()), t.microsecond + len(self._attachment_parts), suffix) + + p = MIMEBase(primary, seconday) + p.add_header('Content-Disposition', 'attachment', filename=name) + p.add_header('Content-Transfer-Encoding', 'base64') + p.add_header('Content-ID', '<' + cid + '>') + p.set_payload(base64.b64encode(attach.data())) + + self._attachment_parts.append(p) + + # modify attachment object + attach.setData('', mimetype) + attach.setUri('cid:' + cid, mimetype) + vattach[i] = attach + i += 1 + + self.event.setAttachments(vattach) + + part.set_payload(str(self)) part.add_header('Content-Disposition', 'attachment; filename="kolab.xml"') part.replace_header('Content-Transfer-Encoding', '8bit') msg.attach(part) + # append attachment parts + for p in self._attachment_parts: + msg.attach(p) + return msg def to_message_itip(self, from_address, method="REQUEST", participant_status="ACCEPTED", subject=None, message_text=None): |