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
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:
--- /dev/null
+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
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)