From 0de195b893ffca38a8bbe8be015e15147377bfa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 31 Aug 2012 19:12:47 +0200 Subject: [PATCH] [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 --- openerp/addons/base/res/res_partner.py | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) 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]