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 121 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.iteritems()]) | ||||
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.iteritems(): | ||||
Show All 20 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())) | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | 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.iteritems()]) | ||||
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): | ||||
Show All 26 Lines |