[IMP] res_partner: moved regex of name_create of res.partner into a dedicated method, allowing ot use it outside of the name_create. Cleaned a bit the code, updated comments to the method.

bzr revid: tde@openerp.com-20120831171247-uc0babnzfk4logoa
This commit is contained in:
Thibault Delavallée 2012-08-31 19:12:47 +02:00
parent d37745559e
commit 0de195b893
1 changed files with 16 additions and 16 deletions

View File

@ -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 <raoul@grosbedon.fr>', 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 <raoul@grosbedon.fr>': 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]