fix: added hooks to validate contact before save
This commit is contained in:
parent
61fa0eb616
commit
f5e3b81be8
@ -1,48 +1,69 @@
|
|||||||
import frappe
|
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()
|
@frappe.whitelist()
|
||||||
def create_new(contact, field, value):
|
def create_new(contact, field, value):
|
||||||
"""Create new email or phone for a contact"""
|
"""Create new email or phone for a contact"""
|
||||||
if not frappe.has_permission("Contact", "write", contact):
|
if not frappe.has_permission("Contact", "write", contact):
|
||||||
frappe.throw("Not permitted", frappe.PermissionError)
|
frappe.throw("Not permitted", frappe.PermissionError)
|
||||||
|
|
||||||
contact = frappe.get_doc("Contact", contact)
|
contact = frappe.get_doc("Contact", contact)
|
||||||
|
|
||||||
if field == "email":
|
if field == "email":
|
||||||
contact.append("email_ids", {"email_id": value})
|
contact.append("email_ids", {"email_id": value})
|
||||||
elif field in ("mobile_no", "phone"):
|
elif field in ("mobile_no", "phone"):
|
||||||
contact.append("phone_nos", {"phone": value})
|
contact.append("phone_nos", {"phone": value})
|
||||||
else:
|
else:
|
||||||
frappe.throw("Invalid field")
|
frappe.throw("Invalid field")
|
||||||
|
|
||||||
contact.save()
|
contact.save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def set_as_primary(contact, field, value):
|
def set_as_primary(contact, field, value):
|
||||||
"""Set email or phone as primary for a contact"""
|
"""Set email or phone as primary for a contact"""
|
||||||
if not frappe.has_permission("Contact", "write", contact):
|
if not frappe.has_permission("Contact", "write", contact):
|
||||||
frappe.throw("Not permitted", frappe.PermissionError)
|
frappe.throw("Not permitted", frappe.PermissionError)
|
||||||
|
|
||||||
contact = frappe.get_doc("Contact", contact)
|
contact = frappe.get_doc("Contact", contact)
|
||||||
|
|
||||||
if field == "email":
|
if field == "email":
|
||||||
for email in contact.email_ids:
|
for email in contact.email_ids:
|
||||||
if email.email_id == value:
|
if email.email_id == value:
|
||||||
email.is_primary = 1
|
email.is_primary = 1
|
||||||
else:
|
else:
|
||||||
email.is_primary = 0
|
email.is_primary = 0
|
||||||
elif field in ("mobile_no", "phone"):
|
elif field in ("mobile_no", "phone"):
|
||||||
name = "is_primary_mobile_no" if field == "mobile_no" else "is_primary_phone"
|
name = "is_primary_mobile_no" if field == "mobile_no" else "is_primary_phone"
|
||||||
for phone in contact.phone_nos:
|
for phone in contact.phone_nos:
|
||||||
if phone.phone == value:
|
if phone.phone == value:
|
||||||
phone.set(name, 1)
|
phone.set(name, 1)
|
||||||
else:
|
else:
|
||||||
phone.set(name, 0)
|
phone.set(name, 0)
|
||||||
else:
|
else:
|
||||||
frappe.throw("Invalid field")
|
frappe.throw("Invalid field")
|
||||||
|
|
||||||
contact.save()
|
contact.save()
|
||||||
return True
|
return True
|
||||||
|
|||||||
12
crm/hooks.py
12
crm/hooks.py
@ -125,13 +125,11 @@ website_route_rules = [
|
|||||||
# ---------------
|
# ---------------
|
||||||
# Hook on document methods and events
|
# Hook on document methods and events
|
||||||
|
|
||||||
# doc_events = {
|
doc_events = {
|
||||||
# "*": {
|
"Contact": {
|
||||||
# "on_update": "method",
|
"validate": ["crm.api.contact.validate"],
|
||||||
# "on_cancel": "method",
|
},
|
||||||
# "on_trash": "method"
|
}
|
||||||
# }
|
|
||||||
# }
|
|
||||||
|
|
||||||
# Scheduled Tasks
|
# Scheduled Tasks
|
||||||
# ---------------
|
# ---------------
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user