diff --git a/openerp/addons/base/res/res_partner.py b/openerp/addons/base/res/res_partner.py index 30255d93ec3..42ddd1d40cd 100644 --- a/openerp/addons/base/res/res_partner.py +++ b/openerp/addons/base/res/res_partner.py @@ -351,27 +351,27 @@ class res_partner(osv.osv): res.append((record.id, name)) return res + def parse_partner_name(self, cr, uid, name, context=None): + """ Supported syntax: + - 'Raoul ', available with ' ', '<>', '{}', + '()', '[]': will find name and email address + - otherwise: default, everything is set as the name """ + regex = re.compile('^(.*?)(?: ?[\<{(\[]?([a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9._]{1,8})[\>})\]]?)?$') + result = regex.match(name) + return (result.group(1).strip(), result.group(2)) + def name_create(self, cr, uid, name, context=None): """ Override of orm's name_create method for partners. The purpose is to handle some basic formats to create partners using the name_create. - Supported syntax: - - 'raoul@grosbedon.fr': create a partner with name raoul@grosbedon.fr - and sets its email to raoul@grosbedon.fr - - 'Raoul Grosbedon ': create a partner with name - Raoul Grosbedon, and set its email to raoul@grosbedon.fr - - anything else: fall back on the default name_create - Regex : - - ([a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9._]{1,8}): raoul@grosbedon.fr - - ([\w\s.\\-]+)[\<]([a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9._]{1,8})[\>]: - Raoul Grosbedon, raoul@grosbedon.fr - """ - regex = re.compile('^(.*?)(?: ?[\<]?([a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9._]{1,8})[\>]?)?$') - result = regex.match(name) - if not result or (context.get('force_create',False) and result.group(2) is None): + If only an email address is received and that the regex cannot find + a name, the name will have the email value. + If 'force_email' key in context: must find the email address. """ + name, email = self.parse_partner_name(cr, uid, name, context=context) + if context.get('force_email') and not email: raise osv.except_osv(_('Warning'), _("Couldn't create contact without email address !")) - name = result.group(1).strip() - email = result.group(2) + if not name and email: + name = email rec_id = self.create(cr, uid, {self._rec_name: name or email, 'email': email or False}, context=context) return self.name_get(cr, uid, [rec_id], context)[0]