summaryrefslogtreecommitdiffstats
path: root/pykolab
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2015-03-26 10:35:51 +0100
committerThomas Bruederli <bruederli@kolabsys.com>2015-03-26 10:35:51 +0100
commit85974d137dd2b37a7d78bffc1d8980cb775f1386 (patch)
tree5c9c7e6139fff60c46fd38f34854137399304463 /pykolab
parent035978ff20b1336ec2db06e3f73a4449ea9ddb72 (diff)
downloadpykolab-85974d137dd2b37a7d78bffc1d8980cb775f1386.tar.gz
Add wrapper class for kolabformat.Note (#4908)
Diffstat (limited to 'pykolab')
-rw-r--r--pykolab/xml/__init__.py9
-rw-r--r--pykolab/xml/note.py136
2 files changed, 145 insertions, 0 deletions
diff --git a/pykolab/xml/__init__.py b/pykolab/xml/__init__.py
index 00edf06..20e4763 100644
--- a/pykolab/xml/__init__.py
+++ b/pykolab/xml/__init__.py
@@ -20,6 +20,11 @@ from todo import todo_from_ical
from todo import todo_from_string
from todo import todo_from_message
+from note import Note
+from note import NoteIntegrityError
+from note import note_from_string
+from note import note_from_message
+
from utils import property_label
from utils import property_to_string
from utils import compute_diff
@@ -31,6 +36,7 @@ __all__ = [
"ContactReference",
"Event",
"Todo",
+ "Note",
"RecurrenceRule",
"event_from_ical",
"event_from_string",
@@ -38,6 +44,8 @@ __all__ = [
"todo_from_ical",
"todo_from_string",
"todo_from_message",
+ "note_from_string",
+ "note_from_message",
"property_label",
"property_to_string",
"compute_diff",
@@ -49,6 +57,7 @@ errors = [
"InvalidEventDateError",
"InvalidAttendeeParticipantStatusError",
"TodoIntegrityError",
+ "NoteIntegrityError",
]
__all__.extend(errors)
diff --git a/pykolab/xml/note.py b/pykolab/xml/note.py
new file mode 100644
index 0000000..e46c41c
--- /dev/null
+++ b/pykolab/xml/note.py
@@ -0,0 +1,136 @@
+import pytz
+import datetime
+import kolabformat
+from pykolab.xml import utils as xmlutils
+from pykolab.xml.utils import ustr
+
+def note_from_string(string):
+ _xml = kolabformat.readNote(string, False)
+ return Note(_xml)
+
+def note_from_message(message):
+ note = None
+ if message.is_multipart():
+ for part in message.walk():
+ if part.get_content_type() == "application/vnd.kolab+xml":
+ payload = part.get_payload(decode=True)
+ note = note_from_string(payload)
+
+ # append attachment parts to Note object
+ elif note and part.has_key('Content-ID'):
+ note._attachment_parts.append(part)
+
+ return todo
+
+
+class Note(kolabformat.Note):
+ type = 'note'
+
+ classification_map = {
+ 'PUBLIC': kolabformat.ClassPublic,
+ 'PRIVATE': kolabformat.ClassPrivate,
+ 'CONFIDENTIAL': kolabformat.ClassConfidential,
+ }
+
+ properties_map = {
+ 'uid': 'get_uid',
+ 'summary': 'summary',
+ 'description': 'description',
+ 'created': 'get_created',
+ 'lastmodified-date': 'get_lastmodified',
+ 'classification': 'get_classification',
+ 'categories': 'categories',
+ 'color': 'color',
+ }
+
+ def __init__(self, *args, **kw):
+ self._attachment_parts = []
+ kolabformat.Note.__init__(self, *args, **kw)
+
+ def get_uid(self):
+ uid = self.uid()
+ if not uid == '':
+ return uid
+ else:
+ self.__str__()
+ return kolabformat.getSerializedUID()
+
+ def get_created(self):
+ try:
+ return xmlutils.from_cdatetime(self.created(), True)
+ except ValueError:
+ return datetime.datetime.now()
+
+ def get_lastmodified(self):
+ try:
+ _datetime = self.lastModified()
+ if _datetime == None or not _datetime.isValid():
+ self.__str__()
+ except:
+ return datetime.datetime.now(pytz.utc)
+
+ return xmlutils.from_cdatetime(self.lastModified(), True)
+
+ def set_summary(self, summary):
+ self.setSummary(ustr(summary))
+
+ def set_description(self, description):
+ self.setDescription(ustr(description))
+
+ def get_classification(self, translated=True):
+ _class = self.classification()
+ if translated:
+ return self._translate_value(_class, self.classification_map)
+ return _class
+
+ def set_classification(self, classification):
+ if classification in self.classification_map.keys():
+ self.setClassification(self.classification_map[classification])
+ elif classification in self.classification_map.values():
+ self.setClassification(status)
+ else:
+ raise ValueError, _("Invalid classification %r") % (classification)
+
+ def add_category(self, category):
+ _categories = self.categories()
+ _categories.append(ustr(category))
+ self.setCategories(_categories)
+
+ 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_dict(self):
+ if not self.isValid():
+ return None
+
+ data = dict()
+
+ for p, getter in self.properties_map.iteritems():
+ val = None
+ if hasattr(self, getter):
+ val = getattr(self, getter)()
+ if isinstance(val, kolabformat.cDateTime):
+ val = xmlutils.from_cdatetime(val, True)
+ elif isinstance(val, kolabformat.vectori):
+ val = [int(x) for x in val]
+ elif isinstance(val, kolabformat.vectors):
+ val = [str(x) for x in val]
+
+ if val is not None:
+ data[p] = val
+
+ return data
+
+ def __str__(self):
+ xml = kolabformat.writeNote(self)
+ error = kolabformat.error()
+
+ if error == None or not error:
+ return xml
+ else:
+ raise NoteIntegrityError, kolabformat.errorMessage()
+
+class NoteIntegrityError(Exception):
+ def __init__(self, message):
+ Exception.__init__(self, message)