From e2de37b090c2714c4030060f6c45c0063dfbcfa6 Mon Sep 17 00:00:00 2001 From: Ayush Shukla Date: Wed, 7 Jun 2017 15:49:27 +0530 Subject: [PATCH] Email parse addr fix. (#3371) * Added name fix * Removed print * Changed method name * Added test case for name * Codacy fix * Codacy Exception fix --- .../communication/test_communication.py | 41 +++++++++++++------ frappe/utils/__init__.py | 24 ++++++----- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/frappe/core/doctype/communication/test_communication.py b/frappe/core/doctype/communication/test_communication.py index 653904a07b..5de6353083 100644 --- a/frappe/core/doctype/communication/test_communication.py +++ b/frappe/core/doctype/communication/test_communication.py @@ -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 Name with quotes and " ', - 'foo@bar@google.com', 'Surname, Name ', - 'Purchase@ABC ', 'xyz@abc2.com ', - 'Name [something else] ', - '.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 Name with quotes and " ', + "Surname, Name ", + "Purchase@ABC ", "xyz@abc2.com ", + "Name [something else] "] + + 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 Name with quotes and " ', + "Surname, Name ", + "Purchase@ABC ", "xyz@abc2.com ", + "Name [something else] "] + + 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]) diff --git a/frappe/utils/__init__.py b/frappe/utils/__init__.py index 90bdd86fba..1e601e1050 100644 --- a/frappe/utils/__init__.py +++ b/frappe/utils/__init__.py @@ -55,13 +55,12 @@ def get_formatted_email(user): """get Email Address of user formatted as: `John Doe `""" 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("|", 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():