From f5e3b81be8ee5f7d8106dfe1e7fde74e790a4e39 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 13 Nov 2023 17:49:08 +0530 Subject: [PATCH] fix: added hooks to validate contact before save --- crm/api/contact.py | 87 ++++++++++++++++++++++++++++------------------ crm/hooks.py | 12 +++---- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/crm/api/contact.py b/crm/api/contact.py index 5711f869..06a63408 100644 --- a/crm/api/contact.py +++ b/crm/api/contact.py @@ -1,48 +1,69 @@ import frappe +def validate(doc, method): + set_primary_email(doc) + set_primary_mobile_no(doc) + doc.set_primary_email() + doc.set_primary("mobile_no") + + +def set_primary_email(doc): + if not doc.email_ids: + return + + if len(doc.email_ids) == 1: + doc.email_ids[0].is_primary = 1 + +def set_primary_mobile_no(doc): + if not doc.phone_nos: + return + + if len(doc.phone_nos) == 1: + doc.phone_nos[0].is_primary_mobile_no = 1 + @frappe.whitelist() def create_new(contact, field, value): - """Create new email or phone for a contact""" - if not frappe.has_permission("Contact", "write", contact): - frappe.throw("Not permitted", frappe.PermissionError) + """Create new email or phone for a contact""" + if not frappe.has_permission("Contact", "write", contact): + frappe.throw("Not permitted", frappe.PermissionError) - contact = frappe.get_doc("Contact", contact) + contact = frappe.get_doc("Contact", contact) - if field == "email": - contact.append("email_ids", {"email_id": value}) - elif field in ("mobile_no", "phone"): - contact.append("phone_nos", {"phone": value}) - else: - frappe.throw("Invalid field") + if field == "email": + contact.append("email_ids", {"email_id": value}) + elif field in ("mobile_no", "phone"): + contact.append("phone_nos", {"phone": value}) + else: + frappe.throw("Invalid field") - contact.save() - return True + contact.save() + return True @frappe.whitelist() def set_as_primary(contact, field, value): - """Set email or phone as primary for a contact""" - if not frappe.has_permission("Contact", "write", contact): - frappe.throw("Not permitted", frappe.PermissionError) + """Set email or phone as primary for a contact""" + if not frappe.has_permission("Contact", "write", contact): + frappe.throw("Not permitted", frappe.PermissionError) - contact = frappe.get_doc("Contact", contact) + contact = frappe.get_doc("Contact", contact) - if field == "email": - for email in contact.email_ids: - if email.email_id == value: - email.is_primary = 1 - else: - email.is_primary = 0 - elif field in ("mobile_no", "phone"): - name = "is_primary_mobile_no" if field == "mobile_no" else "is_primary_phone" - for phone in contact.phone_nos: - if phone.phone == value: - phone.set(name, 1) - else: - phone.set(name, 0) - else: - frappe.throw("Invalid field") + if field == "email": + for email in contact.email_ids: + if email.email_id == value: + email.is_primary = 1 + else: + email.is_primary = 0 + elif field in ("mobile_no", "phone"): + name = "is_primary_mobile_no" if field == "mobile_no" else "is_primary_phone" + for phone in contact.phone_nos: + if phone.phone == value: + phone.set(name, 1) + else: + phone.set(name, 0) + else: + frappe.throw("Invalid field") - contact.save() - return True + contact.save() + return True diff --git a/crm/hooks.py b/crm/hooks.py index 744e0919..fdaef00f 100644 --- a/crm/hooks.py +++ b/crm/hooks.py @@ -125,13 +125,11 @@ website_route_rules = [ # --------------- # Hook on document methods and events -# doc_events = { -# "*": { -# "on_update": "method", -# "on_cancel": "method", -# "on_trash": "method" -# } -# } +doc_events = { + "Contact": { + "validate": ["crm.api.contact.validate"], + }, +} # Scheduled Tasks # ---------------