[IMP] tools : add email_validate optionaly using flanker

As Flanker can be used to validate an email address and was originally
used in website_crm_score module, the Flanker usage is now moved into tools.

This commit prepares the next Predictive Lead Scoring (aka PLS) ones
as PLS will use the email state as computation variable,
it will need this flanker validation to compute first the email state.

We use also the already implemented normalized email as a double validation.

Flanker is not in the required dependencies of Odoo :
So if flanker is installed, it will check if email is valid
(checking DNS and other standard rules).
If Flanker is not installed, email_validate will only normalize the email.
Anyway, the email must at least be normalizable.

Task ID : 1925439
PR #33589
This commit is contained in:
David Beguin
2019-06-11 07:04:33 +00:00
parent 5a34d64a44
commit 0c6a8a1263
+34
View File
@@ -20,6 +20,30 @@ from odoo.tools import misc
_logger = logging.getLogger(__name__)
# Optional Flanker dependency for email validation.
_flanker_warning = False
try:
from flanker.addresslib import address
if hasattr(address, 'six'):
# Python 3 supported
def checkmail(mail):
return bool(address.validate_address(mail))
else:
def checkmail(mail):
global _flanker_warning
if not _flanker_warning:
_logger.info("Flanker version 0.9 or greater required for Python 3 compatibility")
_flanker_warning = True
return True
except ImportError:
_logger.info('The flanker Python module is not installed, so email validation with flanker is unavailable')
def checkmail(mail):
global _flanker_warning
if not _flanker_warning:
_logger.info('The flanker Python module is not installed, so email validation with flanker is unavailable')
_flanker_warning = True
return True
#----------------------------------------------------------
# HTML Sanitizer
@@ -513,6 +537,16 @@ def email_normalize(text):
return False
return emails[0].lower()
def email_validate(email):
"""
Check is the email is valid using email normalization + optional Flanker module.
If Flanker is installed, it will check if email is valid (checking DNS and other stuff).
If Flanker is not installed, this method only normalizes the email
:param text: string containing the email to validate
:return: normalized email if mail is valid or False
"""
return email_normalize(email) if checkmail(email) else False
def email_escape_char(email_address):
""" Escape problematic characters in the given email address string"""
return email_address.replace('\\', '\\\\').replace('%', '\\%').replace('_', '\\_')