Changeset View
Changeset View
Standalone View
Standalone View
pykolab/xml/note.py
Show All 13 Lines | def note_from_message(message): | ||||
note = None | note = None | ||||
if message.is_multipart(): | if message.is_multipart(): | ||||
for part in message.walk(): | for part in message.walk(): | ||||
if part.get_content_type() == "application/vnd.kolab+xml": | if part.get_content_type() == "application/vnd.kolab+xml": | ||||
payload = part.get_payload(decode=True) | payload = part.get_payload(decode=True) | ||||
note = note_from_string(payload) | note = note_from_string(payload) | ||||
# append attachment parts to Note object | # append attachment parts to Note object | ||||
elif note and part.has_key('Content-ID'): | elif note and 'Content-ID' in part: | ||||
note._attachment_parts.append(part) | note._attachment_parts.append(part) | ||||
return note | return note | ||||
class Note(kolabformat.Note): | class Note(kolabformat.Note): | ||||
type = 'note' | type = 'note' | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | class Note(kolabformat.Note): | ||||
def get_classification(self, translated=True): | def get_classification(self, translated=True): | ||||
_class = self.classification() | _class = self.classification() | ||||
if translated: | if translated: | ||||
return self._translate_value(_class, self.classification_map) | return self._translate_value(_class, self.classification_map) | ||||
return _class | return _class | ||||
def set_classification(self, classification): | def set_classification(self, classification): | ||||
if classification in self.classification_map.keys(): | if classification in self.classification_map: | ||||
self.setClassification(self.classification_map[classification]) | self.setClassification(self.classification_map[classification]) | ||||
elif classification in self.classification_map.values(): | elif classification in self.classification_map.values(): | ||||
self.setClassification(status) | self.setClassification(status) | ||||
else: | else: | ||||
raise ValueError, _("Invalid classification %r") % (classification) | raise ValueError(_("Invalid classification %r") % (classification)) | ||||
def add_category(self, category): | def add_category(self, category): | ||||
_categories = self.categories() | _categories = self.categories() | ||||
_categories.append(ustr(category)) | _categories.append(ustr(category)) | ||||
self.setCategories(_categories) | self.setCategories(_categories) | ||||
def _translate_value(self, val, map): | def _translate_value(self, val, map): | ||||
name_map = dict([(v, k) for (k, v) in map.iteritems()]) | name_map = dict([(v, k) for (k, v) in map.items()]) | ||||
return name_map[val] if name_map.has_key(val) else 'UNKNOWN' | return name_map[val] if val in name_map else 'UNKNOWN' | ||||
def to_dict(self): | def to_dict(self): | ||||
if not self.isValid(): | if not self.isValid(): | ||||
return None | return None | ||||
data = dict() | data = dict() | ||||
for p, getter in self.properties_map.iteritems(): | for p, getter in self.properties_map.items(): | ||||
val = None | val = None | ||||
if hasattr(self, getter): | if hasattr(self, getter): | ||||
val = getattr(self, getter)() | val = getattr(self, getter)() | ||||
if isinstance(val, kolabformat.cDateTime): | if isinstance(val, kolabformat.cDateTime): | ||||
val = xmlutils.from_cdatetime(val, True) | val = xmlutils.from_cdatetime(val, True) | ||||
elif isinstance(val, kolabformat.vectori): | elif isinstance(val, kolabformat.vectori): | ||||
val = [int(x) for x in val] | val = [int(x) for x in val] | ||||
elif isinstance(val, kolabformat.vectors): | elif isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val] | val = [str(x) for x in val] | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
return data | return data | ||||
def __str__(self): | def __str__(self): | ||||
xml = kolabformat.writeNote(self) | xml = kolabformat.writeNote(self) | ||||
error = kolabformat.error() | error = kolabformat.error() | ||||
if error == None or not error: | if error == None or not error: | ||||
return xml | return xml | ||||
else: | else: | ||||
raise NoteIntegrityError, kolabformat.errorMessage() | raise NoteIntegrityError(kolabformat.errorMessage()) | ||||
class NoteIntegrityError(Exception): | class NoteIntegrityError(Exception): | ||||
def __init__(self, message): | def __init__(self, message): | ||||
Exception.__init__(self, message) | Exception.__init__(self, message) |