From ebd6de1ae14c5a5c8cac11db6e38d7ace4c821cb Mon Sep 17 00:00:00 2001 From: pateljannat Date: Fri, 18 Dec 2020 12:04:50 +0530 Subject: [PATCH 1/3] fix: document naming rule validation for fields --- .../document_naming_rule/document_naming_rule.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/frappe/core/doctype/document_naming_rule/document_naming_rule.py b/frappe/core/doctype/document_naming_rule/document_naming_rule.py index 3ff47facc3..62d007609f 100644 --- a/frappe/core/doctype/document_naming_rule/document_naming_rule.py +++ b/frappe/core/doctype/document_naming_rule/document_naming_rule.py @@ -6,8 +6,19 @@ from __future__ import unicode_literals import frappe from frappe.model.document import Document from frappe.utils.data import evaluate_filters +from frappe import _ class DocumentNamingRule(Document): + def validate(self): + self.validate_fields_in_conditions() + + def validate_fields_in_conditions(self): + for condition in self.conditions: + docfields = frappe.get_meta(self.document_type).fields + matching_field = list(filter(lambda x: x.fieldname == condition.field, docfields)) + if not len(matching_field): + frappe.throw(_("{0} is not a field of doctype {1}").format(frappe.bold(condition.field), frappe.bold(self.document_type))) + def apply(self, doc): ''' Apply naming rules for the given document. Will set `name` if the rule is matched. From 6d989531914dc6b6a0d0c9b9e653aaab6bb520be Mon Sep 17 00:00:00 2001 From: pateljannat Date: Fri, 18 Dec 2020 14:35:00 +0530 Subject: [PATCH 2/3] fix: change request --- .../doctype/document_naming_rule/document_naming_rule.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frappe/core/doctype/document_naming_rule/document_naming_rule.py b/frappe/core/doctype/document_naming_rule/document_naming_rule.py index 62d007609f..5ae9528cea 100644 --- a/frappe/core/doctype/document_naming_rule/document_naming_rule.py +++ b/frappe/core/doctype/document_naming_rule/document_naming_rule.py @@ -13,10 +13,9 @@ class DocumentNamingRule(Document): self.validate_fields_in_conditions() def validate_fields_in_conditions(self): + docfields = [x.fieldname for x in frappe.get_meta(self.document_type).fields] for condition in self.conditions: - docfields = frappe.get_meta(self.document_type).fields - matching_field = list(filter(lambda x: x.fieldname == condition.field, docfields)) - if not len(matching_field): + if condition.field not in docfields: frappe.throw(_("{0} is not a field of doctype {1}").format(frappe.bold(condition.field), frappe.bold(self.document_type))) def apply(self, doc): From d3a046a72ca20d1da1364b47a814963c83b691a5 Mon Sep 17 00:00:00 2001 From: pateljannat Date: Fri, 18 Dec 2020 21:18:47 +0530 Subject: [PATCH 3/3] fix: check for doctype change before validation --- .../doctype/document_naming_rule/document_naming_rule.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frappe/core/doctype/document_naming_rule/document_naming_rule.py b/frappe/core/doctype/document_naming_rule/document_naming_rule.py index 5ae9528cea..4b34293af6 100644 --- a/frappe/core/doctype/document_naming_rule/document_naming_rule.py +++ b/frappe/core/doctype/document_naming_rule/document_naming_rule.py @@ -13,10 +13,11 @@ class DocumentNamingRule(Document): self.validate_fields_in_conditions() def validate_fields_in_conditions(self): - docfields = [x.fieldname for x in frappe.get_meta(self.document_type).fields] - for condition in self.conditions: - if condition.field not in docfields: - frappe.throw(_("{0} is not a field of doctype {1}").format(frappe.bold(condition.field), frappe.bold(self.document_type))) + if self.has_value_changed("document_type"): + docfields = [x.fieldname for x in frappe.get_meta(self.document_type).fields] + for condition in self.conditions: + if condition.field not in docfields: + frappe.throw(_("{0} is not a field of doctype {1}").format(frappe.bold(condition.field), frappe.bold(self.document_type))) def apply(self, doc): '''