diff --git a/base_vcard/__init__.py b/base_vcard/__init__.py index 2078a2d..17a2ae3 100644 --- a/base_vcard/__init__.py +++ b/base_vcard/__init__.py @@ -1,2 +1,3 @@ from . import vcard_model from . import res_partner +from . import res_users diff --git a/base_vcard/res_partner.py b/base_vcard/res_partner.py index 17eded1..080577b 100644 --- a/base_vcard/res_partner.py +++ b/base_vcard/res_partner.py @@ -92,28 +92,49 @@ def _fill_get_vcard(self, cr, uid, ids, vcard): vcard.photo.type_param = "PNG" def _fill_set_vcard(self, cr, uid, ids, vcard, update_values): - partner_obj = self.browse(cr, uid, ids)[0] - update_values = {} + partner_obj = self.browse(cr, uid, ids)[0] if ids else False # a vcard MUST have an N and FN attribute, so we don't have to # check whether these are available here if 'email' in vcard: for email in vcard['email']: - if email.value != partner_obj.email: + if not partner_obj or email.value != partner_obj.email: update_values['email'] = email.value + def types(tel): + return (map(lambda x: x.lower(), tel.type_paramlist) + if ('type' in tel.params or 'TYPE' in tel.params) + else []) + # update phone numbers if 'tel' in vcard: work_tel = [tel for tel in vcard['tel'] - if u'work' in map(lambda x: x.lower(), tel.type_paramlist) and - u'fax' not in map(lambda x: x.lower(), tel.type_paramlist)] + if u'work' in types(tel) and + u'fax' not in types(tel)] + if not work_tel: + if types(tel): + work_tel = [tel for tel in vcard['tel'] + if u'main' in types(tel) and + u'cell' not in types(tel)] + else: + work_tel = [tel for tel in vcard['tel']] + for tel in work_tel: - if tel.value != partner_obj.phone: + if not partner_obj or tel.value != partner_obj.phone: update_values['phone'] = tel.value + break + + fax_tel = [tel for tel in vcard['tel'] + if u'fax' in types(tel)] + for tel in fax_tel: + if not partner_obj or tel.value != partner_obj.fax: + update_values['fax'] = tel.value + break cell_tel = [tel for tel in vcard['tel'] - if u'cell' in map(lambda x: x.lower(), tel.type_paramlist)] + if u'cell' in types(tel)] for tel in cell_tel: - if tel.value != partner_obj.mobile: + if not partner_obj or tel.value != partner_obj.mobile: update_values['mobile'] = tel.value + break diff --git a/base_vcard/res_users.py b/base_vcard/res_users.py new file mode 100644 index 0000000..bd05e88 --- /dev/null +++ b/base_vcard/res_users.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +import uuid +from openerp import models + + +class User(models.Model): + _inherit = ['res.users', 'vcard.model'] + _name = 'res.users' diff --git a/base_vcard/vcard_model.py b/base_vcard/vcard_model.py index 74cfca4..80777ab 100644 --- a/base_vcard/vcard_model.py +++ b/base_vcard/vcard_model.py @@ -116,7 +116,7 @@ def get_vcard(self, cr, uid, ids): self._fill_get_vcard(cr, uid, ids, vcard) return vcard - def set_vcard(self, cr, uid, ids, vcard_string): + def get_values_from_vcard(self, cr, uid, ids, vcard_string): "Import a model from a vCard" vcard = vobject.readOne(vcard_string) @@ -134,6 +134,10 @@ def set_vcard(self, cr, uid, ids, vcard_string): pickle.dumps(unmapped_properties) self._fill_set_vcard(cr, uid, ids, vcard.contents, update_values) + return update_values + + def set_vcard(self, cr, uid, ids, vcard_string): + update_values = self.get_values_from_vcard(cr, uid, ids, vcard_string) self.write(cr, uid, ids, update_values) def get_uid_by_vcard(self, vcard_string):