Browse Source

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 years ago
committed by Faris Ansari
parent
commit
e2de37b090
2 changed files with 42 additions and 23 deletions
  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 View File

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

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:
self.assertTrue(frappe.utils.parse_addr(x))
self.assertTrue(frappe.utils.parse_addr(x)[0])

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


+ 14
- 10
frappe/utils/__init__.py View File

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

def extract_email_id(email):
"""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):
email_id = email_id.decode("utf-8", "ignore")
return email_id
@@ -70,8 +69,6 @@ def validate_email_add(email_str, throw=False):
"""Validates the email string"""
email = email_str = (email_str or "").strip()

valid = True

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

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

@@ -464,30 +460,38 @@ def parse_addr(email_string):
"""
name, email = parseaddr(email_string)
if check_format(email):
name = get_name_from_email_string(email_string, email, name)
return (name, email)
else:
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)
if len(email_list) > 0 and check_format(email_list[0]):
#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)

def check_format(email_id):
"""
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:
pos = email_id.rindex("@")
is_valid = pos > 0 and (email_id.rindex(".") > pos) and (len(email_id) - pos > 4)
except Exception, e:
except Exception:
#print(e)
pass
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():
out = []
for app in frappe.get_installed_apps():


Loading…
Cancel
Save