[IMP] res_partner: overrided name_create to support custom creation: info@openerp.com (name and email), and Raoul <raoul@raoul.fr>.

bzr revid: tde@openerp.com-20120713130454-2x5fsvz7az3p1jln
This commit is contained in:
Thibault Delavallée 2012-07-13 15:04:54 +02:00
parent 9a4fe00b09
commit 9c4dd0565b
1 changed files with 32 additions and 0 deletions

View File

@ -61,6 +61,38 @@ class res_partner_category(osv.osv):
res.append((record['id'], name))
return res
def name_create(self, cr, uid, name, context=None):
""" Overrider of orm's name_create method for partners. The purpose is
to handle some basic syntaxic tricks to create partners using the
name_create.
Supported syntax:
- 'info@mail.com': create a partner with name info@mail.com, and
sets its email to info@mail.com
- '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 :
- (^|\s)([\w|\.]+)@([\w|\.]*): (void), info, openerp.com
- (^|\s)([\w|\.|\s]+)[\<]([\w|\.]+)@([\w|\.]*)[\>]: (void), Raoul
Grosbedon, raoul, grosbedon.fr
"""
contact_regex = re.compile('(^|\s)([\w|\.|\s]+)[\<]([\w|\.]+)@([\w|\.]*)[\>]')
email_regex = re.compile('(^|\s)([\w|\.]+)@([\w|\.]*)')
contact_regex_res = contact_regex.findall(name)
email_regex_res = email_regex.findall(name)
if contact_regex_res:
name = contact_regex_res[0][1]
name = name.rstrip(' ') # remove extra spaces on the right
email = '%s@%s' % (contact_regex_res[0][2], contact_regex_res[0][3])
rec_id = self.create(cr, uid, {self._rec_name: name, 'email': email}, context);
return self.name_get(cr, uid, [rec_id], context)[0]
elif email_regex:
email = '%s@%s' % (email_regex_res[0][1], email_regex_res[0][2])
rec_id = self.create(cr, uid, {self._rec_name: email, 'email': email}, context);
return self.name_get(cr, uid, [rec_id], context)[0]
else:
return super(res_partner, self).create(cr, uid, name, context)
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
if not args:
args=[]