def validate(self): from frappe.custom.doctype.customize_form.customize_form import CustomizeForm meta = frappe.get_meta(self.dt, cached=False) fieldnames = [df.fieldname for df in meta.get("fields")] if self.insert_after == "append": self.insert_after = fieldnames[-1] if self.insert_after and self.insert_after in fieldnames: self.idx = fieldnames.index(self.insert_after) + 1 old_fieldtype = self.db_get("fieldtype") is_fieldtype_changed = (not self.is_new()) and (old_fieldtype != self.fieldtype) if is_fieldtype_changed and not CustomizeForm.allow_fieldtype_change( old_fieldtype, self.fieldtype): frappe.throw( _("Fieldtype cannot be changed from {0} to {1}").format( old_fieldtype, self.fieldtype)) if not self.fieldname: frappe.throw(_("Fieldname not set for Custom Field")) if self.get("translatable", 0) and not supports_translation(self.fieldtype): self.translatable = 0 if not self.flags.ignore_validate: from frappe.core.doctype.doctype.doctype import check_fieldname_conflicts check_fieldname_conflicts(self.dt, self.fieldname)
def validate(self): meta = frappe.get_meta(self.dt, cached=False) fieldnames = [df.fieldname for df in meta.get("fields")] if self.insert_after=='append': self.insert_after = fieldnames[-1] if self.insert_after and self.insert_after in fieldnames: self.idx = fieldnames.index(self.insert_after) + 1 self._old_fieldtype = self.db_get('fieldtype') if not self.fieldname: frappe.throw(_("Fieldname not set for Custom Field")) if self.get('translatable', 0) and not supports_translation(self.fieldtype): self.translatable = 0 if not self.flags.ignore_validate: from frappe.core.doctype.doctype.doctype import check_if_fieldname_conflicts_with_methods check_if_fieldname_conflicts_with_methods(self.dt, self.fieldname)
def set_property_setters(self): meta = frappe.get_meta(self.doc_type) # doctype property setters for property in doctype_properties: if self.get(property) != meta.get(property): self.make_property_setter( property=property, value=self.get(property), property_type=doctype_properties[property]) for df in self.get("fields"): meta_df = meta.get("fields", {"fieldname": df.fieldname}) if not meta_df or meta_df[0].get("is_custom_field"): continue for property in docfield_properties: if property != "idx" and (df.get(property) or '') != ( meta_df[0].get(property) or ''): if property == "fieldtype": self.validate_fieldtype_change( df, meta_df[0].get(property), df.get(property)) elif property == "allow_on_submit" and df.get(property): frappe.msgprint(_("Row {0}: Not allowed to enable Allow on Submit for standard fields")\ .format(df.idx)) continue elif property == "reqd" and \ ((frappe.db.get_value("DocField", {"parent":self.doc_type,"fieldname":df.fieldname}, "reqd") == 1) \ and (df.get(property) == 0)): frappe.msgprint(_("Row {0}: Not allowed to disable Mandatory for standard fields")\ .format(df.idx)) continue elif property == "in_list_view" and df.get(property) \ and df.fieldtype!="Attach Image" and df.fieldtype in no_value_fields: frappe.msgprint( _("'In List View' not allowed for type {0} in row {1}" ).format(df.fieldtype, df.idx)) continue elif property == "precision" and cint(df.get("precision")) > 6 \ and cint(df.get("precision")) > cint(meta_df[0].get("precision")): self.flags.update_db = True elif property == "unique": self.flags.update_db = True elif (property == "read_only" and cint(df.get("read_only")) == 0 and frappe.db.get_value("DocField", { "parent": self.doc_type, "fieldname": df.fieldname }, "read_only") == 1): # if docfield has read_only checked and user is trying to make it editable, don't allow it frappe.msgprint( _("You cannot unset 'Read Only' for field {0}"). format(df.label)) continue elif property == "options" and df.get( "fieldtype" ) not in allowed_fieldtype_for_options_change: frappe.msgprint( _("You can't set 'Options' for field {0}").format( df.label)) continue elif property == 'translatable' and not supports_translation( df.get('fieldtype')): frappe.msgprint( _("You can't set 'Translatable' for field {0}"). format(df.label)) continue self.make_property_setter( property=property, value=df.get(property), property_type=docfield_properties[property], fieldname=df.fieldname)
def allow_property_change(self, prop, meta_df, df): if prop == "fieldtype": self.validate_fieldtype_change(df, meta_df[0].get(prop), df.get(prop)) elif prop == "length": old_value_length = cint(meta_df[0].get(prop)) new_value_length = cint(df.get(prop)) if new_value_length and (old_value_length > new_value_length): self.check_length_for_fieldtypes.append({ 'df': df, 'old_value': meta_df[0].get(prop) }) self.validate_fieldtype_length() else: self.flags.update_db = True elif prop == "allow_on_submit" and df.get(prop): if not frappe.db.get_value("DocField", { "parent": self.doc_type, "fieldname": df.fieldname }, "allow_on_submit"): frappe.msgprint(_("Row {0}: Not allowed to enable Allow on Submit for standard fields")\ .format(df.idx)) return False elif prop == "reqd" and \ ((frappe.db.get_value("DocField", {"parent":self.doc_type,"fieldname":df.fieldname}, "reqd") == 1) \ and (df.get(prop) == 0)): frappe.msgprint(_("Row {0}: Not allowed to disable Mandatory for standard fields")\ .format(df.idx)) return False elif prop == "in_list_view" and df.get(prop) \ and df.fieldtype!="Attach Image" and df.fieldtype in no_value_fields: frappe.msgprint( _("'In List View' not allowed for type {0} in row {1}").format( df.fieldtype, df.idx)) return False elif prop == "precision" and cint(df.get("precision")) > 6 \ and cint(df.get("precision")) > cint(meta_df[0].get("precision")): self.flags.update_db = True elif prop == "unique": self.flags.update_db = True elif (prop == "read_only" and cint(df.get("read_only")) == 0 and frappe.db.get_value("DocField", { "parent": self.doc_type, "fieldname": df.fieldname }, "read_only") == 1): # if docfield has read_only checked and user is trying to make it editable, don't allow it frappe.msgprint( _("You cannot unset 'Read Only' for field {0}").format( df.label)) return False elif prop == "options" and df.get( "fieldtype") not in ALLOWED_OPTIONS_CHANGE: frappe.msgprint( _("You can't set 'Options' for field {0}").format(df.label)) return False elif prop == 'translatable' and not supports_translation( df.get('fieldtype')): frappe.msgprint( _("You can't set 'Translatable' for field {0}").format( df.label)) return False elif (prop == 'in_global_search' and df.in_global_search != meta_df[0].get("in_global_search")): self.flags.rebuild_doctype_for_global_search = True return True
def set_default_translatable(self): '''Ensure that non-translatable never will be translatable''' for d in self.fields: if d.translatable and not supports_translation(d.fieldtype): d.translatable = 0
def set_property_setters(self): meta = frappe.get_meta(self.doc_type) # doctype property setters for property in doctype_properties: if self.get(property) != meta.get(property): self.make_property_setter(property=property, value=self.get(property), property_type=doctype_properties[property]) for df in self.get("fields"): meta_df = meta.get("fields", {"fieldname": df.fieldname}) if not meta_df or meta_df[0].get("is_custom_field"): continue for property in docfield_properties: if property != "idx" and (df.get(property) or '') != (meta_df[0].get(property) or ''): if property == "fieldtype": self.validate_fieldtype_change(df, meta_df[0].get(property), df.get(property)) elif property == "allow_on_submit" and df.get(property): frappe.msgprint(_("Row {0}: Not allowed to enable Allow on Submit for standard fields")\ .format(df.idx)) continue elif property == "reqd" and \ ((frappe.db.get_value("DocField", {"parent":self.doc_type,"fieldname":df.fieldname}, "reqd") == 1) \ and (df.get(property) == 0)): frappe.msgprint(_("Row {0}: Not allowed to disable Mandatory for standard fields")\ .format(df.idx)) continue elif property == "in_list_view" and df.get(property) \ and df.fieldtype!="Attach Image" and df.fieldtype in no_value_fields: frappe.msgprint(_("'In List View' not allowed for type {0} in row {1}") .format(df.fieldtype, df.idx)) continue elif property == "precision" and cint(df.get("precision")) > 6 \ and cint(df.get("precision")) > cint(meta_df[0].get("precision")): self.flags.update_db = True elif property == "unique": self.flags.update_db = True elif (property == "read_only" and cint(df.get("read_only"))==0 and frappe.db.get_value("DocField", {"parent": self.doc_type, "fieldname": df.fieldname}, "read_only")==1): # if docfield has read_only checked and user is trying to make it editable, don't allow it frappe.msgprint(_("You cannot unset 'Read Only' for field {0}").format(df.label)) continue elif property == "options" and df.get("fieldtype") not in allowed_fieldtype_for_options_change: frappe.msgprint(_("You can't set 'Options' for field {0}").format(df.label)) continue elif property == 'translatable' and not supports_translation(df.get('fieldtype')): frappe.msgprint(_("You can't set 'Translatable' for field {0}").format(df.label)) continue elif (property == 'in_global_search' and df.in_global_search != meta_df[0].get("in_global_search")): self.flags.rebuild_doctype_for_global_search = True self.make_property_setter(property=property, value=df.get(property), property_type=docfield_properties[property], fieldname=df.fieldname)