[IMP] res.partner: simplified+corrected parsing of partner name and email in name_cre...
authorOlivier Dony <odo@openerp.com>
Fri, 31 Aug 2012 22:52:05 +0000 (00:52 +0200)
committerOlivier Dony <odo@openerp.com>
Fri, 31 Aug 2012 22:52:05 +0000 (00:52 +0200)
bzr revid: odo@openerp.com-20120831225205-04l4udn95cz3n72o

openerp/addons/base/res/res_partner.py
openerp/addons/base/tests/__init__.py
openerp/addons/base/tests/test_base.py [new file with mode: 0644]
openerp/tools/misc.py

index 42ddd1d..9314970 100644 (file)
@@ -351,14 +351,17 @@ class res_partner(osv.osv):
             res.append((record.id, name))
         return res
 
-    def parse_partner_name(self, cr, uid, name, context=None):
+    def _parse_partner_name(self, text, context=None):
         """ Supported syntax:
-            - 'Raoul <raoul@grosbedon.fr>', available with '  ', '<>', '{}',
-                '()', '[]': will find name and email address
+            - 'Raoul <raoul@grosbedon.fr>': 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))
+        match = re.search(r'([^\s,<@]+@[^>\s,]+)', text)
+        if match:
+            email = match.group(1) 
+            name = text[:text.index(email)].replace('"','').replace('<','').strip()
+        else:
+            name, email = text, ''
+        return name, email
 
     def name_create(self, cr, uid, name, context=None):
         """ Override of orm's name_create method for partners. The purpose is
@@ -367,7 +370,7 @@ class res_partner(osv.osv):
             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)
+        name, email = self._parse_partner_name(name, context=context)
         if context.get('force_email') and not email:
             raise osv.except_osv(_('Warning'), _("Couldn't create contact without email address !"))
         if not name and email:
index 5ea7654..42fef2e 100644 (file)
@@ -1,5 +1,5 @@
-import test_ir_values
+import test_ir_values, test_base
 
 checks = [
-    test_ir_values,
+    test_ir_values, test_base
 ]
diff --git a/openerp/addons/base/tests/test_base.py b/openerp/addons/base/tests/test_base.py
new file mode 100644 (file)
index 0000000..1ff200a
--- /dev/null
@@ -0,0 +1,26 @@
+import unittest2
+
+import openerp.tests.common as common
+
+class test_base(common.TransactionCase):
+
+    def setUp(self):
+        super(test_base,self).setUp()
+        self.res_partner = self.registry('res.partner')
+
+    def test_00_res_partner_name_parse(self):
+        parse = self.res_partner._parse_partner_name
+        # samples use effective TLDs from the Mozilla public suffix
+        # list at http://publicsuffix.org
+        test_samples = [
+            ('"Raoul Grosbedon" <raoul@chirurgiens-dentistes.fr> ', 'Raoul Grosbedon', 'raoul@chirurgiens-dentistes.fr'),
+            ('ryu+giga-Sushi@aizubange.fukushima.jp', '', 'ryu+giga-Sushi@aizubange.fukushima.jp'),
+            ('Raoul chirurgiens-dentistes.fr', 'Raoul chirurgiens-dentistes.fr', ''),
+            (" Raoul O'hara  <!@historicalsociety.museum>", "Raoul O'hara", '!@historicalsociety.museum')
+        ]
+        for text, name, mail in test_samples:
+            self.assertEqual((name,mail), parse(text), 'Partner name parsing failed')
+
+
+if __name__ == '__main__':
+    unittest2.main()
\ No newline at end of file
index 39e86b1..875fa7a 100644 (file)
@@ -406,7 +406,7 @@ def append_content_to_html(html, content, plaintext=True):
        EOF), and wrapping the provided content in a <pre/> block
        unless ``plaintext`` is False. A side-effect of this
        method is to coerce all HTML tags to lowercase in ``html``,
-       and strips enclosing <html> or <body> tags in content if
+       and strip enclosing <html> or <body> tags in content if
        ``plaintext`` is False.
        
        :param str html: html tagsoup (doesn't have to be XHTML)