[MERGE]: Merge with latest trunk-server
[odoo/odoo.git] / openerp / addons / base / res / res_partner.py
index 399d79d..bb3245f 100644 (file)
 #
 ##############################################################################
 
-import os
 import math
+import os
 from osv import osv, fields
+import re
 import tools
 from tools.translate import _
 import logging
@@ -307,6 +308,37 @@ class res_partner(osv.osv):
             res.append((record.id, name))
         return res
 
+    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
+        """
+        contact_regex = re.compile('([\w\s.\\-]+)[\<]([a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9._]{1,8})[\>]')
+        email_regex = re.compile('([a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9._]{1,8})')
+        contact_regex_res = contact_regex.findall(name)
+        email_regex_res = email_regex.findall(name)
+        if contact_regex_res:
+            name = contact_regex_res[0][0].rstrip(' ') # remove extra spaces on the right
+            email = contact_regex_res[0][1]
+            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' % (email_regex_res[0])
+            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 = []