From fa662e1732262ee02167e44fd4d0db96f2fbaa80 Mon Sep 17 00:00:00 2001 From: Himanshu Warekar Date: Thu, 8 Aug 2019 23:49:53 +0530 Subject: [PATCH] feat: move to newer contacts structure --- frappe/contacts/doctype/contact/contact.py | 4 ++-- .../test_addresses_and_contacts.py | 4 ++-- .../core/doctype/communication/communication.py | 9 +++++---- .../doctype/communication/test_communication.py | 13 ++++++++----- frappe/core/doctype/user/user.py | 17 ++++++++++++----- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/frappe/contacts/doctype/contact/contact.py b/frappe/contacts/doctype/contact/contact.py index 38c2f0e14d..f1aa08792f 100644 --- a/frappe/contacts/doctype/contact/contact.py +++ b/frappe/contacts/doctype/contact/contact.py @@ -82,7 +82,7 @@ class Contact(Document): if (link.link_doctype, link.link_name) in reference_links: return True - def add_email_address(self, email_id, autosave=False): + def add_email(self, email_id, autosave=False): self.append("email_ids", { "email_id": email_id }) @@ -90,7 +90,7 @@ class Contact(Document): if autosave: self.save(ignore_permissions=True) - def add_phone_number(self, phone_number, autosave=False): + def add_phone(self, phone_number, autosave=False): self.append("phone_numbers", { "phone": phone_number }) diff --git a/frappe/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py b/frappe/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py index e315019ca7..133137f69f 100644 --- a/frappe/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py +++ b/frappe/contacts/report/addresses_and_contacts/test_addresses_and_contacts.py @@ -74,13 +74,13 @@ def create_linked_contact(link_list): contact = frappe.get_doc({ "doctype": "Contact", "salutation": "Mr", - "email_id": "test_contact@example.com", "first_name": "_Test First Name", "last_name": "_Test Last Name", "is_primary_contact": 1, - "phone": "+91 0000000000", "status": "Open" }) + contact.add_email("test_contact@example.com") + contact.add_phone("+91 0000000000") for name in link_list: contact.append("links",{ diff --git a/frappe/core/doctype/communication/communication.py b/frappe/core/doctype/communication/communication.py index 0bf24325ad..c32dfcee99 100644 --- a/frappe/core/doctype/communication/communication.py +++ b/frappe/core/doctype/communication/communication.py @@ -339,10 +339,11 @@ def get_contacts(email_strings): if not contact_name: contact = frappe.get_doc({ - "doctype": "Contact", - "first_name": frappe.unscrub(email.split("@")[0]), - "email_id": email - }).insert(ignore_permissions=True) + "doctype": "Contact", + "first_name": frappe.unscrub(email.split("@")[0]), + }) + contact.add_email(email) + contact.insert(ignore_permissions=True) contact_name = contact.name contacts.append(contact_name) diff --git a/frappe/core/doctype/communication/test_communication.py b/frappe/core/doctype/communication/test_communication.py index 3ce5646c7e..5c8dbc1284 100644 --- a/frappe/core/doctype/communication/test_communication.py +++ b/frappe/core/doctype/communication/test_communication.py @@ -101,20 +101,23 @@ class TestCommunication(unittest.TestCase): contact_sender = frappe.get_doc({ "doctype": "Contact", "first_name": frappe.generate_hash(length=10), - "email_id": "comm_sender@example.com" - }).insert(ignore_permissions=True) + }) + contact_sender.add_email("comm_sender@example.com") + contact_sender.insert(ignore_permissions=True) contact_recipient = frappe.get_doc({ "doctype": "Contact", "first_name": frappe.generate_hash(length=10), - "email_id": "comm_recipient@example.com" }).insert(ignore_permissions=True) + contact_recipient.add_email("comm_recipient@example.com") + contact_recipient.insert(ignore_permissions=True) contact_cc = frappe.get_doc({ "doctype": "Contact", "first_name": frappe.generate_hash(length=10), - "email_id": "comm_cc@example.com" - }).insert(ignore_permissions=True) + }) + contact_cc.add_email("comm_cc@example.com") + contact_cc.insert(ignore_permissions=True) comm = frappe.get_doc({ "doctype": "Communication", diff --git a/frappe/core/doctype/user/user.py b/frappe/core/doctype/user/user.py index e233285fa3..5700274b7d 100644 --- a/frappe/core/doctype/user/user.py +++ b/frappe/core/doctype/user/user.py @@ -1068,16 +1068,23 @@ def create_contact(user, ignore_links=False, ignore_mandatory=False): if user.name in ["Administrator", "Guest"]: return if not frappe.db.get_value("Contact", {"email_id": user.email}): - frappe.get_doc({ + d = frappe.get_doc({ "doctype": "Contact", "first_name": user.first_name, "last_name": user.last_name, - "email_id": user.email, "user": user.name, "gender": user.gender, - "phone": user.phone, - "mobile_no": user.mobile_no - }).insert(ignore_permissions=True, ignore_links=ignore_links, ignore_mandatory=ignore_mandatory) + }) + + if user.email: + d.add_email(user.email) + + if user.phone: + d.add_phone(user.phone) + + if user.mobile_no: + d.add_phone(user.mobile_no) + d.insert(ignore_permissions=True, ignore_links=ignore_links, ignore_mandatory=ignore_mandatory) @frappe.whitelist()