From e9762935f713c6e0dc8c145e380028b1aee88870 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Tue, 25 Jul 2023 19:40:10 +0530 Subject: [PATCH] fix: generate lead_name and some other validations --- crm/crm/doctype/crm_lead/crm_lead.py | 41 ++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/crm/crm/doctype/crm_lead/crm_lead.py b/crm/crm/doctype/crm_lead/crm_lead.py index fc4490fb..74242852 100644 --- a/crm/crm/doctype/crm_lead/crm_lead.py +++ b/crm/crm/doctype/crm_lead/crm_lead.py @@ -1,9 +1,46 @@ # Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -# import frappe +import frappe +from frappe import _ from frappe.model.document import Document +from frappe.utils import has_gravatar, validate_email_address + class CRMLead(Document): - pass + def validate(self): + self.set_full_name() + self.set_lead_name() + self.set_title() + self.validate_email() + + def set_full_name(self): + if self.first_name: + self.lead_name = " ".join( + filter(None, [self.salutation, self.first_name, self.middle_name, self.last_name]) + ) + + def set_lead_name(self): + if not self.lead_name: + # Check for leads being created through data import + if not self.organization_name and not self.email and not self.flags.ignore_mandatory: + frappe.throw(_("A Lead requires either a person's name or an organization's name")) + elif self.organization_name: + self.lead_name = self.organization_name + else: + self.lead_name = self.email.split("@")[0] + + def set_title(self): + self.title = self.organization_name or self.lead_name + + def validate_email(self): + if self.email: + if not self.flags.ignore_email_validation: + validate_email_address(self.email, throw=True) + + if self.email == self.lead_owner: + frappe.throw(_("Lead Owner cannot be same as the Lead Email Address")) + + if self.is_new() or not self.image: + self.image = has_gravatar(self.email)