浏览代码

Email parse addr fix. (#3371)

* Added name fix

* Removed print

* Changed method name

* Added test  case for name

* Codacy fix

* Codacy Exception fix
version-14
Ayush Shukla 8 年前
committed by Faris Ansari
父节点
当前提交
e2de37b090
共有 2 个文件被更改,包括 42 次插入23 次删除
  1. +28
    -13
      frappe/core/doctype/communication/test_communication.py
  2. +14
    -10
      frappe/utils/__init__.py

+ 28
- 13
frappe/core/doctype/communication/test_communication.py 查看文件

@@ -9,21 +9,36 @@ test_records = frappe.get_test_records('Communication')


class TestCommunication(unittest.TestCase): class TestCommunication(unittest.TestCase):


def test_parse_addr(self):
valid_email_list = ["me@example.com", "a.nonymous@example.com", "name@tag@example.com",
"foo@example.com", 'Full Name <full@example.com>',
'"Full Name with quotes and <weird@chars.com>" <weird@example.com>',
'foo@bar@google.com', 'Surname, Name <name.surname@domain.com>',
'Purchase@ABC <purchase@abc.com>', 'xyz@abc2.com <xyz@abc.com>',
'Name [something else] <name@domain.com>',
'.com@test@yahoo.com']

invalid_email_list = ['[invalid!email]', 'invalid-email',
'tes2', 'e', 'rrrrrrrr', 'manas','[[[sample]]]',
'[invalid!email].com']
def test_email(self):
valid_email_list = ["Full Name <full@example.com>",
'"Full Name with quotes and <weird@chars.com>" <weird@example.com>',
"Surname, Name <name.surname@domain.com>",
"Purchase@ABC <purchase@abc.com>", "xyz@abc2.com <xyz@abc.com>",
"Name [something else] <name@domain.com>"]

invalid_email_list = ["[invalid!email]", "invalid-email",
"tes2", "e", "rrrrrrrr", "manas","[[[sample]]]",
"[invalid!email].com"]

for x in valid_email_list:
self.assertTrue(frappe.utils.parse_addr(x)[1])

for x in invalid_email_list:
self.assertFalse(frappe.utils.parse_addr(x)[0])

def test_name(self):
valid_email_list = ["Full Name <full@example.com>",
'"Full Name with quotes and <weird@chars.com>" <weird@example.com>',
"Surname, Name <name.surname@domain.com>",
"Purchase@ABC <purchase@abc.com>", "xyz@abc2.com <xyz@abc.com>",
"Name [something else] <name@domain.com>"]

invalid_email_list = ["[invalid!email]", "invalid-email",
"tes2", "e", "rrrrrrrr", "manas","[[[sample]]]",
"[invalid!email].com"]


for x in valid_email_list: for x in valid_email_list:
self.assertTrue(frappe.utils.parse_addr(x))
self.assertTrue(frappe.utils.parse_addr(x)[0])


for x in invalid_email_list: for x in invalid_email_list:
self.assertFalse(frappe.utils.parse_addr(x)[0]) self.assertFalse(frappe.utils.parse_addr(x)[0])


+ 14
- 10
frappe/utils/__init__.py 查看文件

@@ -55,13 +55,12 @@ def get_formatted_email(user):
"""get Email Address of user formatted as: `John Doe <johndoe@example.com>`""" """get Email Address of user formatted as: `John Doe <johndoe@example.com>`"""
if user == "Administrator": if user == "Administrator":
return user return user
from email.utils import formataddr
fullname = get_fullname(user) fullname = get_fullname(user)
return formataddr((fullname, user)) return formataddr((fullname, user))


def extract_email_id(email): def extract_email_id(email):
"""fetch only the email part of the Email Address""" """fetch only the email part of the Email Address"""
full_name, email_id = parse_addr(email)
email_id = parse_addr(email)[1]
if email_id and isinstance(email_id, basestring) and not isinstance(email_id, unicode): if email_id and isinstance(email_id, basestring) and not isinstance(email_id, unicode):
email_id = email_id.decode("utf-8", "ignore") email_id = email_id.decode("utf-8", "ignore")
return email_id return email_id
@@ -70,8 +69,6 @@ def validate_email_add(email_str, throw=False):
"""Validates the email string""" """Validates the email string"""
email = email_str = (email_str or "").strip() email = email_str = (email_str or "").strip()


valid = True

def _check(e): def _check(e):
_valid = True _valid = True
if not e: if not e:
@@ -387,7 +384,6 @@ def is_markdown(text):
return not re.search("<p[\s]*>|<br[\s]*>", text) return not re.search("<p[\s]*>|<br[\s]*>", text)


def get_sites(sites_path=None): def get_sites(sites_path=None):
import os
if not sites_path: if not sites_path:
sites_path = getattr(frappe.local, 'sites_path', None) or '.' sites_path = getattr(frappe.local, 'sites_path', None) or '.'


@@ -464,30 +460,38 @@ def parse_addr(email_string):
""" """
name, email = parseaddr(email_string) name, email = parseaddr(email_string)
if check_format(email): if check_format(email):
name = get_name_from_email_string(email_string, email, name)
return (name, email) return (name, email)
else: else:
email_regex = re.compile(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)") email_regex = re.compile(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)")
email_list = re.findall(email_regex, email_string) email_list = re.findall(email_regex, email_string)
if len(email_list) > 0 and check_format(email_list[0]): if len(email_list) > 0 and check_format(email_list[0]):
#take only first email address #take only first email address
return (name, email_list[0])
email = email_list[0]
name = get_name_from_email_string(email_string, email, name)
return (name, email)
return (None, email) return (None, email)


def check_format(email_id): def check_format(email_id):
""" """
Check if email_id is valid. valid email:text@example.com Check if email_id is valid. valid email:text@example.com
String check ensures that email_id contains both '.' and
'@' and index of '@' is less than '.'
String check ensures that email_id contains both '.' and
'@' and index of '@' is less than '.'
""" """
is_valid = False
is_valid = False
try: try:
pos = email_id.rindex("@") pos = email_id.rindex("@")
is_valid = pos > 0 and (email_id.rindex(".") > pos) and (len(email_id) - pos > 4) is_valid = pos > 0 and (email_id.rindex(".") > pos) and (len(email_id) - pos > 4)
except Exception, e:
except Exception:
#print(e) #print(e)
pass pass
return is_valid return is_valid


def get_name_from_email_string(email_string, email_id, name):
name = email_string.replace(email_id, '')
name = re.sub('[^A-Za-z0-9 ]+', '', name).strip()
return name

def get_installed_apps_info(): def get_installed_apps_info():
out = [] out = []
for app in frappe.get_installed_apps(): for app in frappe.get_installed_apps():


正在加载...
取消
保存