From 138c7841c2bdbf4cb1c674e7c2d4e238b72e919c Mon Sep 17 00:00:00 2001 From: Andy Zhu Date: Wed, 10 Mar 2021 18:02:50 +1300 Subject: [PATCH] fix: set_primary and set_primary_email bug (#12299) * fix: set_primary and set_primary_email bug When user untick the `is_primary_phone` or `is_primary_mobile` from the contact numbers table, we should reset the phone, mobile number etc. Same goes for the email address table. * Update frappe/contacts/doctype/contact/contact.py fix variable naming as suggested Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * Update frappe/contacts/doctype/contact/contact.py fix variable naming as suggested Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * Update frappe/contacts/doctype/contact/contact.py fix variable naming as suggested Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * Update frappe/contacts/doctype/contact/contact.py fix variable naming as suggested Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * Update frappe/contacts/doctype/contact/contact.py fix variable naming as suggested Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * Update frappe/contacts/doctype/contact/contact.py fix variable naming as suggested Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> * test: Set empty string instead of none for number Co-authored-by: Suraj Shetty <13928957+surajshetty3416@users.noreply.github.com> Co-authored-by: Suraj Shetty --- frappe/contacts/doctype/contact/contact.py | 10 ++++++++++ .../test_addresses_and_contacts.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/frappe/contacts/doctype/contact/contact.py b/frappe/contacts/doctype/contact/contact.py index 42fa039f74..b3d4c6fc5c 100644 --- a/frappe/contacts/doctype/contact/contact.py +++ b/frappe/contacts/doctype/contact/contact.py @@ -97,11 +97,16 @@ class Contact(Document): if len([email.email_id for email in self.email_ids if email.is_primary]) > 1: frappe.throw(_("Only one {0} can be set as primary.").format(frappe.bold("Email ID"))) + primary_email_exists = False for d in self.email_ids: if d.is_primary == 1: + primary_email_exists = True self.email_id = d.email_id.strip() break + if not primary_email_exists: + self.email_id = "" + def set_primary(self, fieldname): # Used to set primary mobile and phone no. if len(self.phone_nos) == 0: @@ -115,11 +120,16 @@ class Contact(Document): if len(is_primary) > 1: frappe.throw(_("Only one {0} can be set as primary.").format(frappe.bold(frappe.unscrub(fieldname)))) + primary_number_exists = False for d in self.phone_nos: if d.get(field_name) == 1: + primary_number_exists = True setattr(self, fieldname, d.phone) break + if not primary_number_exists: + setattr(self, fieldname, "") + def get_default_contact(doctype, name): '''Returns default contact for the given doctype, name''' out = frappe.db.sql('''select parent, 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 2db395102a..9e98dcf6f6 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 @@ -103,7 +103,7 @@ class TestAddressesAndContacts(unittest.TestCase): create_linked_contact(links_list, d) report_data = get_data({"reference_doctype": "Test Custom Doctype"}) for idx, link in enumerate(links_list): - test_item = [link, 'test address line 1', 'test address line 2', 'Milan', None, None, 'Italy', 0, '_Test First Name', '_Test Last Name', '_Test Address-Billing', '+91 0000000000', None, 'test_contact@example.com', 1] + test_item = [link, 'test address line 1', 'test address line 2', 'Milan', None, None, 'Italy', 0, '_Test First Name', '_Test Last Name', '_Test Address-Billing', '+91 0000000000', '', 'test_contact@example.com', 1] self.assertListEqual(test_item, report_data[idx]) def tearDown(self):