diff --git a/frappe/__init__.py b/frappe/__init__.py index a6a5067ea2..c5f13f2295 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -1154,6 +1154,7 @@ def make_property_setter(args, ignore_validate=False, validate_fields_for_doctyp 'doctype_or_field': args.doctype_or_field, 'doc_type': doctype, 'field_name': args.fieldname, + 'row_name': args.row_name, 'property': args.property, 'value': args.value, 'property_type': args.property_type or "Data", diff --git a/frappe/app.py b/frappe/app.py index c4d6a0235a..82471c4e32 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -160,6 +160,10 @@ def handle_exception(e): http_status_code = getattr(e, "http_status_code", 500) return_as_message = False + if frappe.conf.get('developer_mode'): + # don't fail silently + print(frappe.get_traceback()) + if frappe.get_request_header('Accept') and (frappe.local.is_ajax or 'application/json' in frappe.get_request_header('Accept')): # handle ajax responses first # if the request is ajax, send back the trace or error message diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 1aa6beb6a6..8a9c130fbe 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -752,8 +752,8 @@ def validate_fields(meta): def check_illegal_default(d): if d.fieldtype == "Check" and not d.default: d.default = '0' - if d.fieldtype == "Check" and d.default not in ('0', '1'): - frappe.throw(_("Default for 'Check' type of field must be either '0' or '1'")) + if d.fieldtype == "Check" and cint(d.default) not in (0, 1): + frappe.throw(_("Default for 'Check' type of field {0} must be either '0' or '1'").format(frappe.bold(d.fieldname))) if d.fieldtype == "Select" and d.default: if not d.options: frappe.throw(_("Options for {0} must be set before setting the default value.").format(frappe.bold(d.fieldname))) diff --git a/frappe/core/doctype/doctype/test_doctype.py b/frappe/core/doctype/doctype/test_doctype.py index 00e80ce4e7..6f4a400577 100644 --- a/frappe/core/doctype/doctype/test_doctype.py +++ b/frappe/core/doctype/doctype/test_doctype.py @@ -12,41 +12,22 @@ from frappe.core.doctype.doctype.doctype import UniqueFieldnameError, IllegalMan class TestDocType(unittest.TestCase): - def new_doctype(self, name, unique=0, depends_on=''): - return frappe.get_doc({ - "doctype": "DocType", - "module": "Core", - "custom": 1, - "fields": [{ - "label": "Some Field", - "fieldname": "some_fieldname", - "fieldtype": "Data", - "unique": unique, - "depends_on": depends_on, - }], - "permissions": [{ - "role": "System Manager", - "read": 1, - }], - "name": name - }) - def test_validate_name(self): - self.assertRaises(frappe.NameError, self.new_doctype("_Some DocType").insert) - self.assertRaises(frappe.NameError, self.new_doctype("8Some DocType").insert) - self.assertRaises(frappe.NameError, self.new_doctype("Some (DocType)").insert) + self.assertRaises(frappe.NameError, new_doctype("_Some DocType").insert) + self.assertRaises(frappe.NameError, new_doctype("8Some DocType").insert) + self.assertRaises(frappe.NameError, new_doctype("Some (DocType)").insert) for name in ("Some DocType", "Some_DocType"): if frappe.db.exists("DocType", name): frappe.delete_doc("DocType", name) - doc = self.new_doctype(name).insert() + doc = new_doctype(name).insert() doc.delete() def test_doctype_unique_constraint_dropped(self): if frappe.db.exists("DocType", "With_Unique"): frappe.delete_doc("DocType", "With_Unique") - dt = self.new_doctype("With_Unique", unique=1) + dt = new_doctype("With_Unique", unique=1) dt.insert() doc1 = frappe.new_doc("With_Unique") @@ -67,7 +48,7 @@ class TestDocType(unittest.TestCase): doc2.delete() def test_validate_search_fields(self): - doc = self.new_doctype("Test Search Fields") + doc = new_doctype("Test Search Fields") doc.search_fields = "some_fieldname" doc.insert() self.assertEqual(doc.name, "Test Search Fields") @@ -85,7 +66,7 @@ class TestDocType(unittest.TestCase): self.assertRaises(frappe.ValidationError, doc.save) def test_depends_on_fields(self): - doc = self.new_doctype("Test Depends On", depends_on="eval:doc.__islocal == 0") + doc = new_doctype("Test Depends On", depends_on="eval:doc.__islocal == 0") doc.insert() # check if the assignment operation is allowed in depends_on @@ -261,7 +242,7 @@ class TestDocType(unittest.TestCase): frappe.flags.allow_doctype_export = 0 def test_unique_field_name_for_two_fields(self): - doc = self.new_doctype('Test Unique Field') + doc = new_doctype('Test Unique Field') field_1 = doc.append('fields', {}) field_1.fieldname = 'some_fieldname_1' field_1.fieldtype = 'Data' @@ -273,7 +254,7 @@ class TestDocType(unittest.TestCase): self.assertRaises(UniqueFieldnameError, doc.insert) def test_fieldname_is_not_name(self): - doc = self.new_doctype('Test Name Field') + doc = new_doctype('Test Name Field') field_1 = doc.append('fields', {}) field_1.label = 'Name' field_1.fieldtype = 'Data' @@ -283,7 +264,7 @@ class TestDocType(unittest.TestCase): self.assertRaises(InvalidFieldNameError, doc.save) def test_illegal_mandatory_validation(self): - doc = self.new_doctype('Test Illegal mandatory') + doc = new_doctype('Test Illegal mandatory') field_1 = doc.append('fields', {}) field_1.fieldname = 'some_fieldname_1' field_1.fieldtype = 'Section Break' @@ -292,7 +273,7 @@ class TestDocType(unittest.TestCase): self.assertRaises(IllegalMandatoryError, doc.insert) def test_link_with_wrong_and_no_options(self): - doc = self.new_doctype('Test link') + doc = new_doctype('Test link') field_1 = doc.append('fields', {}) field_1.fieldname = 'some_fieldname_1' field_1.fieldtype = 'Link' @@ -304,7 +285,7 @@ class TestDocType(unittest.TestCase): self.assertRaises(WrongOptionsDoctypeLinkError, doc.insert) def test_hidden_and_mandatory_without_default(self): - doc = self.new_doctype('Test hidden and mandatory') + doc = new_doctype('Test hidden and mandatory') field_1 = doc.append('fields', {}) field_1.fieldname = 'some_fieldname_1' field_1.fieldtype = 'Data' @@ -314,7 +295,7 @@ class TestDocType(unittest.TestCase): self.assertRaises(HiddenAndMandatoryWithoutDefaultError, doc.insert) def test_field_can_not_be_indexed_validation(self): - doc = self.new_doctype('Test index') + doc = new_doctype('Test index') field_1 = doc.append('fields', {}) field_1.fieldname = 'some_fieldname_1' field_1.fieldtype = 'Long Text' @@ -327,14 +308,14 @@ class TestDocType(unittest.TestCase): from frappe.desk.form.linked_with import get_submitted_linked_docs, cancel_all_linked_docs #create doctype - link_doc = self.new_doctype('Test Linked Doctype') + link_doc = new_doctype('Test Linked Doctype') link_doc.is_submittable = 1 for data in link_doc.get('permissions'): data.submit = 1 data.cancel = 1 link_doc.insert() - doc = self.new_doctype('Test Doctype') + doc = new_doctype('Test Doctype') doc.is_submittable = 1 field_2 = doc.append('fields', {}) field_2.label = 'Test Linked Doctype' @@ -377,12 +358,12 @@ class TestDocType(unittest.TestCase): doc.delete() frappe.db.commit() - def test_ignore_cancelation_of_linked_doctype_during_cancell(self): + def test_ignore_cancelation_of_linked_doctype_during_cancel(self): import json from frappe.desk.form.linked_with import get_submitted_linked_docs, cancel_all_linked_docs #create linked doctype - link_doc = self.new_doctype('Test Linked Doctype 1') + link_doc = new_doctype('Test Linked Doctype 1') link_doc.is_submittable = 1 for data in link_doc.get('permissions'): data.submit = 1 @@ -390,7 +371,7 @@ class TestDocType(unittest.TestCase): link_doc.insert() #create first parent doctype - test_doc_1 = self.new_doctype('Test Doctype 1') + test_doc_1 = new_doctype('Test Doctype 1') test_doc_1.is_submittable = 1 field_2 = test_doc_1.append('fields', {}) @@ -405,7 +386,7 @@ class TestDocType(unittest.TestCase): test_doc_1.insert() #crete second parent doctype - doc = self.new_doctype('Test Doctype 2') + doc = new_doctype('Test Doctype 2') doc.is_submittable = 1 field_2 = doc.append('fields', {}) @@ -469,3 +450,28 @@ class TestDocType(unittest.TestCase): doc.delete() test_doc_1.delete() frappe.db.commit() + +def new_doctype(name, unique=0, depends_on='', fields=None): + doc = frappe.get_doc({ + "doctype": "DocType", + "module": "Core", + "custom": 1, + "fields": [{ + "label": "Some Field", + "fieldname": "some_fieldname", + "fieldtype": "Data", + "unique": unique, + "depends_on": depends_on, + }], + "permissions": [{ + "role": "System Manager", + "read": 1, + }], + "name": name + }) + + if fields: + for f in fields: + doc.append('fields', f) + + return doc \ No newline at end of file diff --git a/frappe/core/doctype/doctype_action/doctype_action.json b/frappe/core/doctype/doctype_action/doctype_action.json index 0f9da802eb..080755c479 100644 --- a/frappe/core/doctype/doctype_action/doctype_action.json +++ b/frappe/core/doctype/doctype_action/doctype_action.json @@ -9,7 +9,8 @@ "action_type", "action", "group", - "hidden" + "hidden", + "custom" ], "fields": [ { @@ -48,12 +49,19 @@ "fieldname": "hidden", "fieldtype": "Check", "label": "Hidden" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "hidden": 1, + "label": "Custom" } ], "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2020-08-21 14:44:03.845315", + "modified": "2020-09-24 14:19:05.549835", "modified_by": "Administrator", "module": "Core", "name": "DocType Action", diff --git a/frappe/core/doctype/doctype_link/doctype_link.json b/frappe/core/doctype/doctype_link/doctype_link.json index 752b4bb5da..0453894467 100644 --- a/frappe/core/doctype/doctype_link/doctype_link.json +++ b/frappe/core/doctype/doctype_link/doctype_link.json @@ -7,7 +7,9 @@ "field_order": [ "link_doctype", "link_fieldname", - "group" + "group", + "hidden", + "custom" ], "fields": [ { @@ -30,10 +32,25 @@ "fieldtype": "Data", "in_list_view": 1, "label": "Group" + }, + { + "default": "0", + "fieldname": "hidden", + "fieldtype": "Check", + "label": "Hidden" + }, + { + "default": "0", + "fieldname": "custom", + "fieldtype": "Check", + "hidden": 1, + "label": "Custom" } ], + "index_web_pages_for_search": 1, "istable": 1, - "modified": "2019-09-24 11:41:25.291377", + "links": [], + "modified": "2020-09-24 14:19:25.189511", "modified_by": "Administrator", "module": "Core", "name": "DocType Link", diff --git a/frappe/core/doctype/report/report.py b/frappe/core/doctype/report/report.py index fe3156d995..9d30409a2a 100644 --- a/frappe/core/doctype/report/report.py +++ b/frappe/core/doctype/report/report.py @@ -49,8 +49,8 @@ class Report(Document): self.export_doc() def on_trash(self): - if (self.is_standard == 'Yes' - and not cint(getattr(frappe.local.conf, 'developer_mode', 0)) + if (self.is_standard == 'Yes' + and not cint(getattr(frappe.local.conf, 'developer_mode', 0)) and not frappe.flags.in_patch): frappe.throw(_("You are not allowed to delete Standard Report")) delete_custom_role('report', self.name) diff --git a/frappe/custom/doctype/custom_link/custom_link.js b/frappe/custom/doctype/custom_link/custom_link.js deleted file mode 100644 index 8662724b1a..0000000000 --- a/frappe/custom/doctype/custom_link/custom_link.js +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2020, Frappe Technologies and contributors -// For license information, please see license.txt - -frappe.ui.form.on('Custom Link', { - refresh: function(frm) { - frm.set_query("document_type", function () { - return { - filters: { - custom: 0, - istable: 0, - module: ['not in', ["Email", "Core", "Custom", "Event Streaming", "Social", "Data Migration", "Geo", "Desk"]] - } - }; - }); - - frm.add_custom_button(__('Go to {0} List', [frm.doc.document_type]), function() { - frappe.set_route('List', frm.doc.document_type); - }); - } -}); diff --git a/frappe/custom/doctype/customize_form/customize_form.js b/frappe/custom/doctype/customize_form/customize_form.js index b1743a96a5..2d220b864c 100644 --- a/frappe/custom/doctype/customize_form/customize_form.js +++ b/frappe/custom/doctype/customize_form/customize_form.js @@ -5,6 +5,7 @@ frappe.provide("frappe.customize_form"); frappe.ui.form.on("Customize Form", { onload: function(frm) { + frm.disable_save(); frm.set_query("doc_type", function() { return { translate_values: false, @@ -27,7 +28,7 @@ frappe.ui.form.on("Customize Form", { }); $(frm.wrapper).on("grid-row-render", function(e, grid_row) { - if(grid_row.doc && grid_row.doc.fieldtype=="Section Break") { + if (grid_row.doc && grid_row.doc.fieldtype=="Section Break") { $(grid_row.row).css({"font-weight": "bold"}); } }); @@ -40,19 +41,25 @@ frappe.ui.form.on("Customize Form", { frm.trigger("setup_sortable"); }); + if (localStorage['customize_doctype']) { + // set default value from customize form + frm.set_value('doc_type', localStorage['customize_doctype']); + } + }, doc_type: function(frm) { - if(frm.doc.doc_type) { + if (frm.doc.doc_type) { return frm.call({ method: "fetch_to_customize", doc: frm.doc, freeze: true, callback: function(r) { - if(r) { - if(r._server_messages && r._server_messages.length) { + if (r) { + if (r._server_messages && r._server_messages.length) { frm.set_value("doc_type", ""); } else { + localStorage['customize_doctype'] = frm.doc.doc_type; frm.refresh(); frm.trigger("setup_sortable"); } @@ -69,7 +76,7 @@ frappe.ui.form.on("Customize Form", { frm.doc.fields.forEach(function(f, i) { var data_row = frm.page.body.find('[data-fieldname="fields"] [data-idx="'+ f.idx +'"] .data-row'); - if(f.is_custom_field) { + if (f.is_custom_field) { data_row.addClass("highlight"); } else { f._sortable = false; @@ -82,26 +89,26 @@ frappe.ui.form.on("Customize Form", { frm.disable_save(); frm.page.clear_icons(); - if(frm.doc.doc_type) { + if (frm.doc.doc_type) { frappe.customize_form.set_primary_action(frm); frm.add_custom_button(__('Go to {0} List', [frm.doc.doc_type]), function() { frappe.set_route('List', frm.doc.doc_type); - }); + }, __('Actions')); - frm.add_custom_button(__('Refresh Form'), function() { + frm.add_custom_button(__('Reload'), function() { frm.script_manager.trigger("doc_type"); - }, "fa fa-refresh", "btn-default"); + }, __('Actions')); frm.add_custom_button(__('Reset to defaults'), function() { frappe.customize_form.confirm(__('Remove all customizations?'), frm); - }, "fa fa-eraser", "btn-default"); + }, __('Actions')); frm.add_custom_button(__('Set Permissions'), function() { frappe.set_route('permission-manager', frm.doc.doc_type); - }, "fa fa-lock", "btn-default"); + }, __('Actions')); - if(frappe.boot.developer_mode) { + if (frappe.boot.developer_mode) { frm.add_custom_button(__('Export Customizations'), function() { frappe.prompt( [ @@ -124,34 +131,36 @@ frappe.ui.form.on("Customize Form", { }); }, __("Select Module")); - }); + }, __('Actions')); } } // sort order select - if(frm.doc.doc_type) { + if (frm.doc.doc_type) { var fields = $.map(frm.doc.fields, - function(df) { return frappe.model.is_value_type(df.fieldtype) ? df.fieldname : null; }); + function(df) { + return frappe.model.is_value_type(df.fieldtype) ? df.fieldname : null; + }); fields = ["", "name", "modified"].concat(fields); frm.set_df_property("sort_field", "options", fields); } - if(frappe.route_options && frappe.route_options.doc_type) { + if (frappe.route_options && frappe.route_options.doc_type) { setTimeout(function() { frm.set_value("doc_type", frappe.route_options.doc_type); frappe.route_options = null; }, 1000); } - } }); +// can't delete standard fields frappe.ui.form.on("Customize Form Field", { before_fields_remove: function(frm, doctype, name) { var row = frappe.get_doc(doctype, name); - if(!(row.is_custom_field || row.__islocal)) { + if (!(row.is_custom_field || row.__islocal)) { frappe.msgprint(__("Cannot delete standard field. You can hide it if you want")); - throw "cannot delete custom field"; + throw "cannot delete standard field"; } }, fields_add: function(frm, cdt, cdn) { @@ -160,16 +169,46 @@ frappe.ui.form.on("Customize Form Field", { } }); +// can't delete standard links +frappe.ui.form.on("DocType Link", { + before_links_remove: function(frm, doctype, name) { + let row = frappe.get_doc(doctype, name); + if (!(row.custom || row.__islocal)) { + frappe.msgprint(__("Cannot delete standard link. You can hide it if you want")); + throw "cannot delete standard link"; + } + }, + links_add: function(frm, cdt, cdn) { + let f = frappe.model.get_doc(cdt, cdn); + f.custom = 1; + } +}); + +// can't delete standard actions +frappe.ui.form.on("DocType Action", { + before_actions_remove: function(frm, doctype, name) { + let row = frappe.get_doc(doctype, name); + if (!(row.custom || row.__islocal)) { + frappe.msgprint(__("Cannot delete standard action. You can hide it if you want")); + throw "cannot delete standard action"; + } + }, + actions_add: function(frm, cdt, cdn) { + let f = frappe.model.get_doc(cdt, cdn); + f.custom = 1; + } +}); + frappe.customize_form.set_primary_action = function(frm) { frm.page.set_primary_action(__("Update"), function() { - if(frm.doc.doc_type) { + if (frm.doc.doc_type) { return frm.call({ doc: frm.doc, freeze: true, btn: frm.page.btn_primary, method: "save_customization", callback: function(r) { - if(!r.exc) { + if (!r.exc) { frappe.customize_form.clear_locals_and_refresh(frm); frm.script_manager.trigger("doc_type"); } @@ -180,7 +219,7 @@ frappe.customize_form.set_primary_action = function(frm) { }; frappe.customize_form.confirm = function(msg, frm) { - if(!frm.doc.doc_type) return; + if (!frm.doc.doc_type) return; var d = new frappe.ui.Dialog({ title: 'Reset To Defaults', @@ -192,7 +231,7 @@ frappe.customize_form.confirm = function(msg, frm) { doc: frm.doc, method: "reset_to_defaults", callback: function(r) { - if(r.exc) { + if (r.exc) { frappe.msgprint(r.exc); } else { d.hide(); diff --git a/frappe/custom/doctype/customize_form/customize_form.json b/frappe/custom/doctype/customize_form/customize_form.json index cd57aa23fe..ff102b3c08 100644 --- a/frappe/custom/doctype/customize_form/customize_form.json +++ b/frappe/custom/doctype/customize_form/customize_form.json @@ -10,8 +10,9 @@ "doc_type", "properties", "label", - "default_print_format", "max_attachments", + "search_fields", + "column_break_5", "allow_copy", "istable", "editable_grid", @@ -20,22 +21,27 @@ "track_views", "allow_auto_repeat", "allow_import", - "show_preview_popup", - "image_view", - "column_break_5", + "fields_section_break", + "fields", + "view_settings_section", "title_field", "image_field", - "search_fields", - "section_break_8", - "sort_field", - "column_break_10", - "sort_order", - "section_break_23", + "default_print_format", + "column_break_29", + "show_preview_popup", + "image_view", + "email_settings_section", "email_append_to", "sender_field", "subject_field", - "fields_section_break", - "fields" + "document_actions_section", + "actions", + "document_links_section", + "links", + "section_break_8", + "sort_field", + "column_break_10", + "sort_order" ], "fields": [ { @@ -130,9 +136,11 @@ "label": "Search Fields" }, { + "collapsible": 1, "depends_on": "doc_type", "fieldname": "section_break_8", - "fieldtype": "Section Break" + "fieldtype": "Section Break", + "label": "List Settings" }, { "fieldname": "sort_field", @@ -161,7 +169,8 @@ "fieldname": "fields", "fieldtype": "Table", "label": "Fields", - "options": "Customize Form Field" + "options": "Customize Form Field", + "reqd": 1 }, { "default": "0", @@ -200,24 +209,67 @@ "fieldtype": "Check", "label": "Allow document creation via Email" }, - { - "depends_on": "doc_type", - "fieldname": "section_break_23", - "fieldtype": "Section Break" - }, { "default": "0", "fieldname": "show_preview_popup", "fieldtype": "Check", "label": "Show Preview Popup" + }, + { + "collapsible": 1, + "depends_on": "doc_type", + "fieldname": "view_settings_section", + "fieldtype": "Section Break", + "label": "View Settings" + }, + { + "fieldname": "column_break_29", + "fieldtype": "Column Break" + }, + { + "collapsible": 1, + "collapsible_depends_on": "email_append_to", + "depends_on": "doc_type", + "fieldname": "email_settings_section", + "fieldtype": "Section Break", + "label": "Email Settings" + }, + { + "collapsible": 1, + "collapsible_depends_on": "links", + "depends_on": "doc_type", + "fieldname": "document_links_section", + "fieldtype": "Section Break", + "label": "Document Links" + }, + { + "fieldname": "links", + "fieldtype": "Table", + "label": "Links", + "options": "DocType Link" + }, + { + "collapsible": 1, + "collapsible_depends_on": "actions", + "depends_on": "doc_type", + "fieldname": "document_actions_section", + "fieldtype": "Section Break", + "label": "Document Actions" + }, + { + "fieldname": "actions", + "fieldtype": "Table", + "label": "Actions", + "options": "DocType Action" } ], "hide_toolbar": 1, "icon": "fa fa-glass", "idx": 1, + "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2020-04-10 12:16:01.320411", + "modified": "2020-09-24 14:16:49.594012", "modified_by": "Administrator", "module": "Custom", "name": "Customize Form", diff --git a/frappe/custom/doctype/customize_form/customize_form.py b/frappe/custom/doctype/customize_form/customize_form.py index 5c4e16fad7..61ecdd88b9 100644 --- a/frappe/custom/doctype/customize_form/customize_form.py +++ b/frappe/custom/doctype/customize_form/customize_form.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals Customize Form is a Single DocType used to mask the Property Setter Thus providing a better UI from user perspective """ +import json import frappe import frappe.translate from frappe import _ @@ -14,8 +15,447 @@ from frappe.model.document import Document from frappe.model import no_value_fields, core_doctypes_list from frappe.core.doctype.doctype.doctype import validate_fields_for_doctype, check_email_append_to from frappe.custom.doctype.custom_field.custom_field import create_custom_field +from frappe.custom.doctype.property_setter.property_setter import delete_property_setter from frappe.model.docfield import supports_translation +class CustomizeForm(Document): + def on_update(self): + frappe.db.sql("delete from tabSingles where doctype='Customize Form'") + frappe.db.sql("delete from `tabCustomize Form Field`") + + def fetch_to_customize(self): + self.clear_existing_doc() + if not self.doc_type: + return + + meta = frappe.get_meta(self.doc_type) + + self.validate_doctype(meta) + + # load the meta properties on the customize (self) object + self.load_properties(meta) + + # load custom translation + translation = self.get_name_translation() + self.label = translation.translated_text if translation else '' + + self.create_auto_repeat_custom_field_if_requried(meta) + + # NOTE doc (self) is sent to clientside by run_method + + def validate_doctype(self, meta): + ''' + Check if the doctype is allowed to be customized. + ''' + if self.doc_type in core_doctypes_list: + frappe.throw(_("Core DocTypes cannot be customized.")) + + if meta.issingle: + frappe.throw(_("Single DocTypes cannot be customized.")) + + if meta.custom: + frappe.throw(_("Only standard DocTypes are allowed to be customized from Customize Form.")) + + def load_properties(self, meta): + ''' + Load the customize object (this) with the metadata properties + ''' + # doctype properties + for prop in doctype_properties: + self.set(prop, meta.get(prop)) + + for d in meta.get("fields"): + new_d = {"fieldname": d.fieldname, "is_custom_field": d.get("is_custom_field"), "name": d.name} + for prop in docfield_properties: + new_d[prop] = d.get(prop) + self.append("fields", new_d) + + for fieldname in ('links', 'actions'): + for d in meta.get(fieldname): + self.append(fieldname, d) + + def create_auto_repeat_custom_field_if_requried(self, meta): + if self.allow_auto_repeat: + if not frappe.db.exists('Custom Field', {'fieldname': 'auto_repeat', + 'dt': self.doc_type}): + insert_after = self.fields[len(self.fields) - 1].fieldname + df = dict( + fieldname='auto_repeat', + label='Auto Repeat', + fieldtype='Link', + options='Auto Repeat', + insert_after=insert_after, + read_only=1, no_copy=1, print_hide=1) + create_custom_field(self.doc_type, df) + + + def get_name_translation(self): + '''Get translation object if exists of current doctype name in the default language''' + return frappe.get_value('Translation', { + 'source_text': self.doc_type, + 'language': frappe.local.lang or 'en' + }, ['name', 'translated_text'], as_dict=True) + + def set_name_translation(self): + '''Create, update custom translation for this doctype''' + current = self.get_name_translation() + if current: + if self.label and current.translated_text != self.label: + frappe.db.set_value('Translation', current.name, 'translated_text', self.label) + frappe.translate.clear_cache() + else: + # clear translation + frappe.delete_doc('Translation', current.name) + + else: + if self.label: + frappe.get_doc(dict(doctype='Translation', + source_text=self.doc_type, + translated_text=self.label, + language_code=frappe.local.lang or 'en')).insert() + + def clear_existing_doc(self): + doc_type = self.doc_type + + for fieldname in self.meta.get_valid_columns(): + self.set(fieldname, None) + + for df in self.meta.get_table_fields(): + self.set(df.fieldname, []) + + self.doc_type = doc_type + self.name = "Customize Form" + + def save_customization(self): + if not self.doc_type: + return + + self.flags.update_db = False + self.flags.rebuild_doctype_for_global_search = False + self.set_property_setters() + self.update_custom_fields() + self.set_name_translation() + validate_fields_for_doctype(self.doc_type) + check_email_append_to(self) + + if self.flags.update_db: + frappe.db.updatedb(self.doc_type) + + if not hasattr(self, 'hide_success') or not self.hide_success: + frappe.msgprint(_("{0} updated").format(_(self.doc_type)), alert=True) + frappe.clear_cache(doctype=self.doc_type) + self.fetch_to_customize() + + if self.flags.rebuild_doctype_for_global_search: + frappe.enqueue('frappe.utils.global_search.rebuild_for_doctype', + now=True, doctype=self.doc_type) + + def set_property_setters(self): + meta = frappe.get_meta(self.doc_type) + + # doctype + self.set_property_setters_for_doctype(meta) + + # docfield + 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 + self.set_property_setters_for_docfield(meta, df, meta_df) + + # action and links + self.set_property_setters_for_actions_and_links(meta) + + def set_property_setters_for_doctype(self, meta): + for prop, prop_type in doctype_properties.items(): + if self.get(prop) != meta.get(prop): + self.make_property_setter(prop, self.get(prop), prop_type) + + def set_property_setters_for_docfield(self, meta, df, meta_df): + for prop, prop_type in docfield_properties.items(): + if prop != "idx" and (df.get(prop) or '') != (meta_df[0].get(prop) or ''): + if not self.allow_property_change(prop, meta_df, df): + continue + + self.make_property_setter(prop, df.get(prop), prop_type, + 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 == "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_property_setters_for_actions_and_links(self, meta): + ''' + Apply property setters or create custom records for DocType Action and DocType Link + ''' + for doctype, fieldname, field_map in ( + ('DocType Link', 'links', doctype_link_properties), + ('DocType Action', 'actions', doctype_action_properties) + ): + has_custom = False + items = [] + for i, d in enumerate(self.get(fieldname) or []): + d.idx = i + if frappe.db.exists(doctype, d.name) and not d.custom: + # check property and apply property setter + original = frappe.get_doc(doctype, d.name) + for prop, prop_type in field_map.items(): + if d.get(prop) != original.get(prop): + self.make_property_setter(prop, d.get(prop), prop_type, + apply_on=doctype, row_name=d.name) + items.append(d.name) + else: + # custom - just insert/update + d.parent = self.doc_type + d.custom = 1 + d.save(ignore_permissions=True) + has_custom = True + items.append(d.name) + + self.update_order_property_setter(has_custom, fieldname) + self.clear_removed_items(doctype, items) + + def update_order_property_setter(self, has_custom, fieldname): + ''' + We need to maintain the order of the link/actions if the user has shuffled them. + So we create a new property (ex `links_order`) to keep a list of items. + ''' + property_name = '{}_order'.format(fieldname) + if has_custom: + # save the order of the actions and links + self.make_property_setter(property_name, + json.dumps([d.name for d in self.get(fieldname)]), 'Small Text') + else: + frappe.db.delete('Property Setter', dict(property=property_name, + doc_type=self.doc_type)) + + + def clear_removed_items(self, doctype, items): + ''' + Clear rows that do not appear in `items`. These have been removed by the user. + ''' + if items: + frappe.db.delete(doctype, dict(parent=self.doc_type, custom=1, + name=('not in', items))) + else: + frappe.db.delete(doctype, dict(parent=self.doc_type, custom=1)) + + def update_custom_fields(self): + for i, df in enumerate(self.get("fields")): + if df.get("is_custom_field"): + if not frappe.db.exists('Custom Field', {'dt': self.doc_type, 'fieldname': df.fieldname}): + self.add_custom_field(df, i) + self.flags.update_db = True + else: + self.update_in_custom_field(df, i) + + self.delete_custom_fields() + + def add_custom_field(self, df, i): + d = frappe.new_doc("Custom Field") + + d.dt = self.doc_type + + for prop in docfield_properties: + d.set(prop, df.get(prop)) + + if i!=0: + d.insert_after = self.fields[i-1].fieldname + d.idx = i + + d.insert() + df.fieldname = d.fieldname + + def update_in_custom_field(self, df, i): + meta = frappe.get_meta(self.doc_type) + meta_df = meta.get("fields", {"fieldname": df.fieldname}) + if not (meta_df and meta_df[0].get("is_custom_field")): + # not a custom field + return + + custom_field = frappe.get_doc("Custom Field", meta_df[0].name) + changed = False + for prop in docfield_properties: + if df.get(prop) != custom_field.get(prop): + if prop == "fieldtype": + self.validate_fieldtype_change(df, meta_df[0].get(prop), df.get(prop)) + + custom_field.set(prop, df.get(prop)) + changed = True + + # check and update `insert_after` property + if i!=0: + insert_after = self.fields[i-1].fieldname + if custom_field.insert_after != insert_after: + custom_field.insert_after = insert_after + custom_field.idx = i + changed = True + + if changed: + custom_field.db_update() + self.flags.update_db = True + #custom_field.save() + + def delete_custom_fields(self): + meta = frappe.get_meta(self.doc_type) + fields_to_remove = (set([df.fieldname for df in meta.get("fields")]) + - set(df.fieldname for df in self.get("fields"))) + + for fieldname in fields_to_remove: + df = meta.get("fields", {"fieldname": fieldname})[0] + if df.get("is_custom_field"): + frappe.delete_doc("Custom Field", df.name) + + def make_property_setter(self, prop, value, property_type, fieldname=None, + apply_on=None, row_name = None): + delete_property_setter(self.doc_type, prop, fieldname) + + property_value = self.get_existing_property_value(prop, fieldname) + + if property_value==value: + return + + if not apply_on: + apply_on = "DocField" if fieldname else "DocType" + + # create a new property setter + frappe.make_property_setter({ + "doctype": self.doc_type, + "doctype_or_field": apply_on, + "fieldname": fieldname, + "row_name": row_name, + "property": prop, + "value": value, + "property_type": property_type + }) + + def get_existing_property_value(self, property_name, fieldname=None): + # check if there is any need to make property setter! + if fieldname: + property_value = frappe.db.get_value("DocField", {"parent": self.doc_type, + "fieldname": fieldname}, property_name) + else: + if frappe.db.has_column("DocType", property_name): + property_value = frappe.db.get_value("DocType", self.doc_type, property_name) + else: + property_value = None + + return property_value + + def validate_fieldtype_change(self, df, old_value, new_value): + allowed = False + self.check_length_for_fieldtypes = [] + for allowed_changes in ALLOWED_FIELDTYPE_CHANGE: + if (old_value in allowed_changes and new_value in allowed_changes): + allowed = True + old_value_length = cint(frappe.db.type_map.get(old_value)[1]) + new_value_length = cint(frappe.db.type_map.get(new_value)[1]) + + # Ignore fieldtype check validation if new field type has unspecified maxlength + # Changes like DATA to TEXT, where new_value_lenth equals 0 will not be validated + if new_value_length and (old_value_length > new_value_length): + self.check_length_for_fieldtypes.append({'df': df, 'old_value': old_value}) + self.validate_fieldtype_length() + else: + self.flags.update_db = True + break + if not allowed: + frappe.throw(_("Fieldtype cannot be changed from {0} to {1} in row {2}").format(old_value, new_value, df.idx)) + + def validate_fieldtype_length(self): + for field in self.check_length_for_fieldtypes: + df = field.get('df') + max_length = cint(frappe.db.type_map.get(df.fieldtype)[1]) + fieldname = df.fieldname + docs = frappe.db.sql(''' + SELECT name, {fieldname}, LENGTH({fieldname}) AS len + FROM `tab{doctype}` + WHERE LENGTH({fieldname}) > {max_length} + '''.format( + fieldname=fieldname, + doctype=self.doc_type, + max_length=max_length + ), as_dict=True) + links = [] + label = df.label + for doc in docs: + links.append(frappe.utils.get_link_to_form(self.doc_type, doc.name)) + links_str = ', '.join(links) + + if docs: + frappe.throw(_('Value for field {0} is too long in {1}. Length should be lesser than {2} characters') + .format( + frappe.bold(label), + links_str, + frappe.bold(max_length) + ), title=_('Data Too Long'), is_minimizable=len(docs) > 1) + + self.flags.update_db = True + + def reset_to_defaults(self): + if not self.doc_type: + return + + reset_customization(self.doc_type) + self.fetch_to_customize() + +def reset_customization(doctype): + frappe.db.sql(""" + DELETE FROM `tabProperty Setter` WHERE doc_type=%s + and `field_name`!='naming_series' + and `property`!='options' + """, doctype) + frappe.clear_cache(doctype=doctype) + doctype_properties = { 'search_fields': 'Data', 'title_field': 'Data', @@ -82,356 +522,31 @@ docfield_properties = { 'hide_seconds': 'Check' } -allowed_fieldtype_change = (('Currency', 'Float', 'Percent'), ('Small Text', 'Data'), - ('Text', 'Data'), ('Text', 'Text Editor', 'Code', 'Signature', 'HTML Editor'), ('Data', 'Select'), - ('Text', 'Small Text'), ('Text', 'Data', 'Barcode'), ('Code', 'Geolocation'), ('Table', 'Table MultiSelect')) - -allowed_fieldtype_for_options_change = ('Read Only', 'HTML', 'Select', 'Data') - -class CustomizeForm(Document): - def on_update(self): - frappe.db.sql("delete from tabSingles where doctype='Customize Form'") - frappe.db.sql("delete from `tabCustomize Form Field`") - - def fetch_to_customize(self): - self.clear_existing_doc() - if not self.doc_type: - return - - meta = frappe.get_meta(self.doc_type) - - if self.doc_type in core_doctypes_list: - return frappe.msgprint(_("Core DocTypes cannot be customized.")) - - if meta.issingle: - return frappe.msgprint(_("Single DocTypes cannot be customized.")) - - if meta.custom: - return frappe.msgprint(_("Only standard DocTypes are allowed to be customized from Customize Form.")) - - # doctype properties - for property in doctype_properties: - self.set(property, meta.get(property)) - - for d in meta.get("fields"): - new_d = {"fieldname": d.fieldname, "is_custom_field": d.get("is_custom_field"), "name": d.name} - for property in docfield_properties: - new_d[property] = d.get(property) - self.append("fields", new_d) - - # load custom translation - translation = self.get_name_translation() - self.label = translation.translated_text if translation else '' - - #If allow_auto_repeat is set, add auto_repeat custom field. - if self.allow_auto_repeat: - if not frappe.db.exists('Custom Field', {'fieldname': 'auto_repeat', 'dt': self.doc_type}): - insert_after = self.fields[len(self.fields) - 1].fieldname - df = dict(fieldname='auto_repeat', label='Auto Repeat', fieldtype='Link', options='Auto Repeat', insert_after=insert_after, read_only=1, no_copy=1, print_hide=1) - create_custom_field(self.doc_type, df) - - # NOTE doc is sent to clientside by run_method - - def get_name_translation(self): - '''Get translation object if exists of current doctype name in the default language''' - return frappe.get_value('Translation', { - 'source_text': self.doc_type, - 'language': frappe.local.lang or 'en' - }, ['name', 'translated_text'], as_dict=True) - - def set_name_translation(self): - '''Create, update custom translation for this doctype''' - current = self.get_name_translation() - if current: - if self.label and current.translated_text != self.label: - frappe.db.set_value('Translation', current.name, 'translated_text', self.label) - frappe.translate.clear_cache() - else: - # clear translation - frappe.delete_doc('Translation', current.name) - - else: - if self.label: - frappe.get_doc(dict(doctype='Translation', - source_text=self.doc_type, - translated_text=self.label, - language_code=frappe.local.lang or 'en')).insert() - - def clear_existing_doc(self): - doc_type = self.doc_type - - for fieldname in self.meta.get_valid_columns(): - self.set(fieldname, None) - - for df in self.meta.get_table_fields(): - self.set(df.fieldname, []) - - self.doc_type = doc_type - self.name = "Customize Form" - - def save_customization(self): - if not self.doc_type: - return - - self.flags.update_db = False - self.flags.rebuild_doctype_for_global_search = False - self.set_property_setters() - self.update_custom_fields() - self.set_name_translation() - validate_fields_for_doctype(self.doc_type) - check_email_append_to(self) - - if self.flags.update_db: - frappe.db.updatedb(self.doc_type) - - if not hasattr(self, 'hide_success') or not self.hide_success: - frappe.msgprint(_("{0} updated").format(_(self.doc_type)), alert=True) - frappe.clear_cache(doctype=self.doc_type) - self.fetch_to_customize() - - if self.flags.rebuild_doctype_for_global_search: - frappe.enqueue('frappe.utils.global_search.rebuild_for_doctype', - now=True, doctype=self.doc_type) - - 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): - 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)) - 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) - - def update_custom_fields(self): - for i, df in enumerate(self.get("fields")): - if df.get("is_custom_field"): - if not frappe.db.exists('Custom Field', {'dt': self.doc_type, 'fieldname': df.fieldname}): - self.add_custom_field(df, i) - self.flags.update_db = True - else: - self.update_in_custom_field(df, i) - - self.delete_custom_fields() - - def add_custom_field(self, df, i): - d = frappe.new_doc("Custom Field") - - d.dt = self.doc_type - - for property in docfield_properties: - d.set(property, df.get(property)) - - if i!=0: - d.insert_after = self.fields[i-1].fieldname - d.idx = i - - d.insert() - df.fieldname = d.fieldname - - def update_in_custom_field(self, df, i): - meta = frappe.get_meta(self.doc_type) - meta_df = meta.get("fields", {"fieldname": df.fieldname}) - if not (meta_df and meta_df[0].get("is_custom_field")): - # not a custom field - return - - custom_field = frappe.get_doc("Custom Field", meta_df[0].name) - changed = False - for property in docfield_properties: - if df.get(property) != custom_field.get(property): - if property == "fieldtype": - self.validate_fieldtype_change(df, meta_df[0].get(property), df.get(property)) - - custom_field.set(property, df.get(property)) - changed = True - - # check and update `insert_after` property - if i!=0: - insert_after = self.fields[i-1].fieldname - if custom_field.insert_after != insert_after: - custom_field.insert_after = insert_after - custom_field.idx = i - changed = True - - if changed: - custom_field.db_update() - self.flags.update_db = True - #custom_field.save() - - def delete_custom_fields(self): - meta = frappe.get_meta(self.doc_type) - fields_to_remove = (set([df.fieldname for df in meta.get("fields")]) - - set(df.fieldname for df in self.get("fields"))) - - for fieldname in fields_to_remove: - df = meta.get("fields", {"fieldname": fieldname})[0] - if df.get("is_custom_field"): - frappe.delete_doc("Custom Field", df.name) - - def make_property_setter(self, property, value, property_type, fieldname=None): - self.delete_existing_property_setter(property, fieldname) - - property_value = self.get_existing_property_value(property, fieldname) - - if property_value==value: - return - - # create a new property setter - # ignore validation becuase it will be done at end - frappe.make_property_setter({ - "doctype": self.doc_type, - "doctype_or_field": "DocField" if fieldname else "DocType", - "fieldname": fieldname, - "property": property, - "value": value, - "property_type": property_type - }, ignore_validate=True) - - def delete_existing_property_setter(self, property, fieldname=None): - # first delete existing property setter - existing_property_setter = frappe.db.get_value("Property Setter", {"doc_type": self.doc_type, - "property": property, "field_name['']": fieldname or ''}) - - if existing_property_setter: - frappe.db.sql("delete from `tabProperty Setter` where name=%s", existing_property_setter) - - def get_existing_property_value(self, property_name, fieldname=None): - # check if there is any need to make property setter! - if fieldname: - property_value = frappe.db.get_value("DocField", {"parent": self.doc_type, - "fieldname": fieldname}, property_name) - else: - try: - property_value = frappe.db.get_value("DocType", self.doc_type, property_name) - except Exception as e: - if frappe.db.is_column_missing(e): - property_value = None - else: - raise - - return property_value - - def validate_fieldtype_change(self, df, old_value, new_value): - allowed = False - self.check_length_for_fieldtypes = [] - for allowed_changes in allowed_fieldtype_change: - if (old_value in allowed_changes and new_value in allowed_changes): - allowed = True - old_value_length = cint(frappe.db.type_map.get(old_value)[1]) - new_value_length = cint(frappe.db.type_map.get(new_value)[1]) - - # Ignore fieldtype check validation if new field type has unspecified maxlength - # Changes like DATA to TEXT, where new_value_lenth equals 0 will not be validated - if new_value_length and (old_value_length > new_value_length): - self.check_length_for_fieldtypes.append({'df': df, 'old_value': old_value}) - self.validate_fieldtype_length() - else: - self.flags.update_db = True - break - if not allowed: - frappe.throw(_("Fieldtype cannot be changed from {0} to {1} in row {2}").format(old_value, new_value, df.idx)) - - def validate_fieldtype_length(self): - for field in self.check_length_for_fieldtypes: - df = field.get('df') - max_length = cint(frappe.db.type_map.get(df.fieldtype)[1]) - fieldname = df.fieldname - docs = frappe.db.sql(''' - SELECT name, {fieldname}, LENGTH({fieldname}) AS len - FROM `tab{doctype}` - WHERE LENGTH({fieldname}) > {max_length} - '''.format( - fieldname=fieldname, - doctype=self.doc_type, - max_length=max_length - ), as_dict=True) - links = [] - label = df.label - for doc in docs: - links.append(frappe.utils.get_link_to_form(self.doc_type, doc.name)) - links_str = ', '.join(links) - - if docs: - frappe.throw(_('Value for field {0} is too long in {1}. Length should be lesser than {2} characters') - .format( - frappe.bold(label), - links_str, - frappe.bold(max_length) - ), title=_('Data Too Long'), is_minimizable=len(docs) > 1) - - self.flags.update_db = True - - def reset_to_defaults(self): - if not self.doc_type: - return - - reset_customization(self.doc_type) - self.fetch_to_customize() - -def reset_customization(doctype): - frappe.db.sql(""" - DELETE FROM `tabProperty Setter` WHERE doc_type=%s - and `field_name`!='naming_series' - and `property`!='options' - """, doctype) - frappe.clear_cache(doctype=doctype) \ No newline at end of file +doctype_link_properties = { + 'link_doctype': 'Link', + 'link_fieldname': 'Data', + 'group': 'Data', + 'hidden': 'Check' +} + +doctype_action_properties = { + 'label': 'Link', + 'action_type': 'Select', + 'action': 'Small Text', + 'group': 'Data', + 'hidden': 'Check' +} + + +ALLOWED_FIELDTYPE_CHANGE = ( + ('Currency', 'Float', 'Percent'), + ('Small Text', 'Data'), + ('Text', 'Data'), + ('Text', 'Text Editor', 'Code', 'Signature', 'HTML Editor'), + ('Data', 'Select'), + ('Text', 'Small Text'), + ('Text', 'Data', 'Barcode'), + ('Code', 'Geolocation'), + ('Table', 'Table MultiSelect')) + +ALLOWED_OPTIONS_CHANGE = ('Read Only', 'HTML', 'Select', 'Data') diff --git a/frappe/custom/doctype/customize_form/test_customize_form.py b/frappe/custom/doctype/customize_form/test_customize_form.py index cace25a03d..46a2f2f9df 100644 --- a/frappe/custom/doctype/customize_form/test_customize_form.py +++ b/frappe/custom/doctype/customize_form/test_customize_form.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import frappe, unittest, json from frappe.test_runner import make_test_records_for_doctype from frappe.core.doctype.doctype.doctype import InvalidFieldNameError +from frappe.core.doctype.doctype.test_doctype import new_doctype test_dependencies = ["Custom Field", "Property Setter"] class TestCustomizeForm(unittest.TestCase): @@ -24,6 +25,7 @@ class TestCustomizeForm(unittest.TestCase): def setUp(self): self.insert_custom_field() + frappe.db.delete('Property Setter', dict(doc_type='Event')) frappe.db.commit() frappe.clear_cache(doctype="Event") @@ -185,9 +187,75 @@ class TestCustomizeForm(unittest.TestCase): d.run_method("save_customization") def test_core_doctype_customization(self): - d = self.get_customize_form('User') - e = self.get_customize_form('Custom Field') + self.assertRaises(frappe.ValidationError, self.get_customize_form, 'User') - # core doctype is invalid, hence no attributes are set - self.assertEquals(d.get("fields"), []) - self.assertEquals(e.get("fields"), []) + def test_custom_link(self): + try: + # create a dummy doctype linked to Event + testdt_name = 'Test Link for Event' + testdt = new_doctype(testdt_name, fields=[ + dict(fieldtype='Link', fieldname='event', options='Event') + ]).insert() + + testdt_name1 = 'Test Link for Event 1' + testdt1 = new_doctype(testdt_name1, fields=[ + dict(fieldtype='Link', fieldname='event', options='Event') + ]).insert() + + # add a custom link + d = self.get_customize_form("Event") + + d.append('links', dict(link_doctype=testdt_name, link_fieldname='event', group='Tests')) + d.append('links', dict(link_doctype=testdt_name1, link_fieldname='event', group='Tests')) + + d.run_method("save_customization") + + frappe.clear_cache() + event = frappe.get_meta('Event') + + # check links exist + self.assertTrue([d.name for d in event.links if d.link_doctype == testdt_name]) + self.assertTrue([d.name for d in event.links if d.link_doctype == testdt_name1]) + + # check order + order = json.loads(event.links_order) + self.assertListEqual(order, [d.name for d in event.links]) + + # remove the link + d = self.get_customize_form("Event") + d.links = [] + d.run_method("save_customization") + + frappe.clear_cache() + event = frappe.get_meta('Event') + self.assertFalse([d.name for d in (event.links or []) if d.link_doctype == testdt_name]) + finally: + testdt.delete() + testdt1.delete() + + def test_custom_action(self): + test_route = '#List/DocType' + + # create a dummy action (route) + d = self.get_customize_form("Event") + d.append('actions', dict(label='Test Action', action_type='Route', action=test_route)) + d.run_method("save_customization") + + frappe.clear_cache() + event = frappe.get_meta('Event') + + # check if added to meta + action = [d for d in event.actions if d.label=='Test Action'] + self.assertEqual(len(action), 1) + self.assertEqual(action[0].action, test_route) + + # clear the action + d = self.get_customize_form("Event") + d.actions = [] + d.run_method("save_customization") + + frappe.clear_cache() + event = frappe.get_meta('Event') + + action = [d for d in event.actions if d.label=='Test Action'] + self.assertEqual(len(action), 0) diff --git a/frappe/custom/doctype/customize_form_field/customize_form_field.json b/frappe/custom/doctype/customize_form_field/customize_form_field.json index 1c7349ef01..1d71e1d1e3 100644 --- a/frappe/custom/doctype/customize_form_field/customize_form_field.json +++ b/frappe/custom/doctype/customize_form_field/customize_form_field.json @@ -11,8 +11,6 @@ "label", "fieldtype", "fieldname", - "hide_seconds", - "hide_days", "reqd", "unique", "in_list_view", @@ -23,6 +21,7 @@ "allow_in_quick_entry", "translatable", "column_break_7", + "default", "precision", "length", "options", @@ -47,8 +46,9 @@ "column_break_33", "read_only_depends_on", "display", - "default", "in_filter", + "hide_seconds", + "hide_days", "column_break_21", "description", "print_hide", @@ -100,6 +100,7 @@ "depends_on": "eval:!in_list([\"Section Break\", \"Column Break\", \"Button\", \"HTML\"], doc.fieldtype)", "fieldname": "reqd", "fieldtype": "Check", + "in_list_view": 1, "label": "Mandatory", "oldfieldname": "reqd", "oldfieldtype": "Check", @@ -283,7 +284,7 @@ }, { "fieldname": "default", - "fieldtype": "Text", + "fieldtype": "Small Text", "label": "Default", "oldfieldname": "default", "oldfieldtype": "Text" @@ -419,7 +420,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2020-08-28 11:28:59.084060", + "modified": "2020-09-24 14:05:31.093927", "modified_by": "Administrator", "module": "Custom", "name": "Customize Form Field", diff --git a/frappe/custom/doctype/property_setter/property_setter.json b/frappe/custom/doctype/property_setter/property_setter.json index 5888e11969..b318d92c5a 100644 --- a/frappe/custom/doctype/property_setter/property_setter.json +++ b/frappe/custom/doctype/property_setter/property_setter.json @@ -1,358 +1,133 @@ { - "allow_copy": 0, - "allow_import": 0, - "allow_rename": 0, - "beta": 0, - "creation": "2013-01-10 16:34:04", - "custom": 0, - "description": "Property Setter overrides a standard DocType or Field property", - "docstatus": 0, - "doctype": "DocType", - "document_type": "Setup", - "editable_grid": 0, - "engine": "InnoDB", + "actions": [], + "creation": "2013-01-10 16:34:04", + "description": "Property Setter overrides a standard DocType or Field property", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "help", + "sb0", + "doctype_or_field", + "doc_type", + "field_name", + "row_name", + "column_break0", + "property", + "property_type", + "value", + "default_value" + ], "fields": [ { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "help", - "fieldtype": "HTML", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Help", - "length": 0, - "no_copy": 0, - "options": "