Changeset View
Changeset View
Standalone View
Standalone View
pykolab/xml/contact.py
Show All 17 Lines | def contact_from_message(message): | ||||
contact = None | contact = 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/vcard+xml": | if part.get_content_type() == "application/vcard+xml": | ||||
payload = part.get_payload(decode=True) | payload = part.get_payload(decode=True) | ||||
contact = contact_from_string(payload) | contact = contact_from_string(payload) | ||||
# append attachment parts to Contact object | # append attachment parts to Contact object | ||||
elif contact and part.has_key('Content-ID'): | elif contact and 'Content-ID' in part: | ||||
contact._attachment_parts.append(part) | contact._attachment_parts.append(part) | ||||
return contact | return contact | ||||
class Contact(kolabformat.Contact): | class Contact(kolabformat.Contact): | ||||
type = 'contact' | type = 'contact' | ||||
▲ Show 20 Lines • Show All 120 Lines • ▼ Show 20 Lines | class Contact(kolabformat.Contact): | ||||
def get_gender(self, translated=True): | def get_gender(self, translated=True): | ||||
_gender = self.gender() | _gender = self.gender() | ||||
if translated: | if translated: | ||||
return self._translate_value(_gender, self.gender_map) | return self._translate_value(_gender, self.gender_map) | ||||
return _gender | return _gender | ||||
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 = self._names2dict(self.nameComponents()) | data = self._names2dict(self.nameComponents()) | ||||
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): | ||||
Show All 12 Lines | def to_dict(self): | ||||
val = [[x.latitude, x.longitude] for x in val] | val = [[x.latitude, x.longitude] for x in val] | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
affiliations = self.affiliations() | affiliations = self.affiliations() | ||||
if len(affiliations) > 0: | if len(affiliations) > 0: | ||||
_affiliation = self._affiliation2dict(affiliations[0]) | _affiliation = self._affiliation2dict(affiliations[0]) | ||||
if _affiliation.has_key('address'): | if 'address' in _affiliation: | ||||
data['address'].extend(_affiliation['address']) | data['address'].extend(_affiliation['address']) | ||||
_affiliation.pop('address', None) | _affiliation.pop('address', None) | ||||
data.update(_affiliation) | data.update(_affiliation) | ||||
data.update(self._relateds2dict(self.relateds())) | data.update(self._relateds2dict(self.relateds())) | ||||
if self.photoMimetype(): | if self.photoMimetype(): | ||||
data['photo'] = dict(mimetype=self.photoMimetype(), base64=base64.b64encode(self.photo())) | data['photo'] = dict(mimetype=self.photoMimetype(), base64=base64.b64encode(self.photo())) | ||||
elif self.photo(): | elif self.photo(): | ||||
data['photo'] = dict(uri=self.photo()) | data['photo'] = dict(uri=self.photo()) | ||||
return data | return data | ||||
def _names2dict(self, namecomp): | def _names2dict(self, namecomp): | ||||
names_map = { | names_map = { | ||||
'surname': 'surnames', | 'surname': 'surnames', | ||||
'given': 'given', | 'given': 'given', | ||||
'additional': 'additional', | 'additional': 'additional', | ||||
'prefix': 'prefixes', | 'prefix': 'prefixes', | ||||
'suffix': 'suffixes', | 'suffix': 'suffixes', | ||||
} | } | ||||
data = dict() | data = dict() | ||||
for p, getter in names_map.iteritems(): | for p, getter in names_map.items(): | ||||
val = None | val = None | ||||
if hasattr(namecomp, getter): | if hasattr(namecomp, getter): | ||||
val = getattr(namecomp, getter)() | val = getattr(namecomp, getter)() | ||||
if isinstance(val, kolabformat.vectors): | if isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val][0] if len(val) > 0 else None | val = [str(x) for x in val][0] if len(val) > 0 else None | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
return data | return data | ||||
def _affiliation2dict(self, affiliation): | def _affiliation2dict(self, affiliation): | ||||
props_map = { | props_map = { | ||||
'organization': 'organisation', | 'organization': 'organisation', | ||||
'department': 'organisationalUnits', | 'department': 'organisationalUnits', | ||||
'role': 'roles', | 'role': 'roles', | ||||
} | } | ||||
data = dict() | data = dict() | ||||
for p, getter in props_map.iteritems(): | for p, getter in props_map.items(): | ||||
val = None | val = None | ||||
if hasattr(affiliation, getter): | if hasattr(affiliation, getter): | ||||
val = getattr(affiliation, getter)() | val = getattr(affiliation, getter)() | ||||
if isinstance(val, kolabformat.vectors): | if isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val][0] if len(val) > 0 else None | val = [str(x) for x in val][0] if len(val) > 0 else None | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
Show All 9 Lines | def _address2dict(self, adr, adrtype=None): | ||||
props_map = { | props_map = { | ||||
'label': 'label', | 'label': 'label', | ||||
'street': 'street', | 'street': 'street', | ||||
'locality': 'locality', | 'locality': 'locality', | ||||
'region': 'region', | 'region': 'region', | ||||
'code': 'code', | 'code': 'code', | ||||
'country': 'country', | 'country': 'country', | ||||
} | } | ||||
addresstype_map = dict([(v, k) for (k, v) in self.addresstype_map.iteritems()]) | addresstype_map = dict([(v, k) for (k, v) in self.addresstype_map.items()]) | ||||
data = dict() | data = dict() | ||||
if adrtype is None: | if adrtype is None: | ||||
adrtype = addresstype_map.get(adr.types(), None) | adrtype = addresstype_map.get(adr.types(), None) | ||||
if adrtype is not None: | if adrtype is not None: | ||||
data['type'] = adrtype | data['type'] = adrtype | ||||
for p, getter in props_map.iteritems(): | for p, getter in props_map.items(): | ||||
val = None | val = None | ||||
if hasattr(adr, getter): | if hasattr(adr, getter): | ||||
val = getattr(adr, getter)() | val = getattr(adr, getter)() | ||||
if isinstance(val, kolabformat.vectors): | if isinstance(val, kolabformat.vectors): | ||||
val = [str(x) for x in val][0] if len(val) > 0 else None | val = [str(x) for x in val][0] if len(val) > 0 else None | ||||
if val is not None: | if val is not None: | ||||
data[p] = val | data[p] = val | ||||
return data | return data | ||||
def _relateds2dict(self, relateds, aslist=True): | def _relateds2dict(self, relateds, aslist=True): | ||||
data = dict() | data = dict() | ||||
related_map = dict([(v, k) for (k, v) in self.related_map.iteritems()]) | related_map = dict([(v, k) for (k, v) in self.related_map.items()]) | ||||
for rel in relateds: | for rel in relateds: | ||||
reltype = related_map.get(rel.relationTypes(), None) | reltype = related_map.get(rel.relationTypes(), None) | ||||
val = rel.uri() if rel.type() == kolabformat.Related.Uid else rel.text() | val = rel.uri() if rel.type() == kolabformat.Related.Uid else rel.text() | ||||
if reltype and val is not None: | if reltype and val is not None: | ||||
if aslist: | if aslist: | ||||
if not data.has_key(reltype): | if reltype not in data: | ||||
data[reltype] = [] | data[reltype] = [] | ||||
data[reltype].append(val) | data[reltype].append(val) | ||||
else: | else: | ||||
data[reltype] = val | data[reltype] = val | ||||
return data | return data | ||||
def _struct2dict(self, struct, propname, map): | def _struct2dict(self, struct, propname, map): | ||||
type_map = dict([(v, k) for (k, v) in map.iteritems()]) | type_map = dict([(v, k) for (k, v) in map.items()]) | ||||
result = dict() | result = dict() | ||||
if hasattr(struct, 'types'): | if hasattr(struct, 'types'): | ||||
result['type'] = type_map.get(struct.types(), None) | result['type'] = type_map.get(struct.types(), None) | ||||
elif hasattr(struct, 'type'): | elif hasattr(struct, 'type'): | ||||
result['type'] = type_map.get(struct.type(), None) | result['type'] = type_map.get(struct.type(), None) | ||||
if hasattr(struct, propname): | if hasattr(struct, propname): | ||||
result[propname] = getattr(struct, propname)() | result[propname] = getattr(struct, propname)() | ||||
return result | return result | ||||
def __str__(self): | def __str__(self): | ||||
xml = kolabformat.writeContact(self) | xml = kolabformat.writeContact(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 ContactIntegrityError, kolabformat.errorMessage() | raise ContactIntegrityError(kolabformat.errorMessage()) | ||||
class ContactIntegrityError(Exception): | class ContactIntegrityError(Exception): | ||||
def __init__(self, message): | def __init__(self, message): | ||||
Exception.__init__(self, message) | Exception.__init__(self, message) |