From 6dbe3d2d53bd393503cd5072b80ef51be3e6043c Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Sat, 15 Jun 2024 18:09:38 +0530 Subject: [PATCH] chore: moved fields layout related code to crm_fields_layout.py --- crm/api/doc.py | 56 ----------------- .../crm_fields_layout/crm_fields_layout.py | 60 ++++++++++++++++++- .../src/components/Modals/ContactModal.vue | 2 +- frontend/src/components/Modals/DealModal.vue | 2 +- frontend/src/components/Modals/LeadModal.vue | 2 +- .../components/Modals/OrganizationModal.vue | 2 +- .../src/components/Settings/FieldsLayout.vue | 4 +- 7 files changed, 65 insertions(+), 63 deletions(-) diff --git a/crm/api/doc.py b/crm/api/doc.py index 6f337426..0159c663 100644 --- a/crm/api/doc.py +++ b/crm/api/doc.py @@ -157,62 +157,6 @@ def get_group_by_fields(doctype: str): return fields -@frappe.whitelist() -def get_fields_layout(doctype: str, type: str): - sections = [] - if frappe.db.exists("CRM Fields Layout", {"dt": doctype, "type": type}): - layout = frappe.get_doc("CRM Fields Layout", {"dt": doctype, "type": type}) - else: - return [] - - if layout.layout: - sections = json.loads(layout.layout) - - allowed_fields = [] - for section in sections: - if not section.get("fields"): - continue - allowed_fields.extend(section.get("fields")) - - fields = frappe.get_meta(doctype).fields - fields = [field for field in fields if field.fieldname in allowed_fields] - - for section in sections: - for field in section.get("fields") if section.get("fields") else []: - field = next((f for f in fields if f.fieldname == field), None) - if field: - if field.fieldtype == "Select": - field.options = field.options.split("\n") - field.options = [{"label": _(option), "value": option} for option in field.options] - field.options.insert(0, {"label": "", "value": ""}) - field = { - "label": _(field.label), - "name": field.fieldname, - "type": field.fieldtype, - "options": field.options, - "mandatory": field.reqd, - } - section["fields"][section.get("fields").index(field["name"])] = field - - return sections or [] - - -@frappe.whitelist() -def save_fields_layout(doctype: str, type: str, layout: str): - if frappe.db.exists("CRM Fields Layout", {"dt": doctype, "type": type}): - doc = frappe.get_doc("CRM Fields Layout", {"dt": doctype, "type": type}) - else: - doc = frappe.new_doc("CRM Fields Layout") - - doc.update({ - "dt": doctype, - "type": type, - "layout": layout, - }) - doc.save(ignore_permissions=True) - - return doc.layout - def get_doctype_fields_meta(DocField, doctype, allowed_fieldtypes, restricted_fields): parent = "parent" if DocField._table_name == "tabDocField" else "dt" return ( diff --git a/crm/fcrm/doctype/crm_fields_layout/crm_fields_layout.py b/crm/fcrm/doctype/crm_fields_layout/crm_fields_layout.py index 36d8bb72..3129c478 100644 --- a/crm/fcrm/doctype/crm_fields_layout/crm_fields_layout.py +++ b/crm/fcrm/doctype/crm_fields_layout/crm_fields_layout.py @@ -1,9 +1,67 @@ # Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -# import frappe +import json +import frappe +from frappe import _ from frappe.model.document import Document class CRMFieldsLayout(Document): pass + +@frappe.whitelist() +def get_fields_layout(doctype: str, type: str): + sections = [] + if frappe.db.exists("CRM Fields Layout", {"dt": doctype, "type": type}): + layout = frappe.get_doc("CRM Fields Layout", {"dt": doctype, "type": type}) + else: + return [] + + if layout.layout: + sections = json.loads(layout.layout) + + allowed_fields = [] + for section in sections: + if not section.get("fields"): + continue + allowed_fields.extend(section.get("fields")) + + fields = frappe.get_meta(doctype).fields + fields = [field for field in fields if field.fieldname in allowed_fields] + + for section in sections: + for field in section.get("fields") if section.get("fields") else []: + field = next((f for f in fields if f.fieldname == field), None) + if field: + if field.fieldtype == "Select": + field.options = field.options.split("\n") + field.options = [{"label": _(option), "value": option} for option in field.options] + field.options.insert(0, {"label": "", "value": ""}) + field = { + "label": _(field.label), + "name": field.fieldname, + "type": field.fieldtype, + "options": field.options, + "mandatory": field.reqd, + } + section["fields"][section.get("fields").index(field["name"])] = field + + return sections or [] + + +@frappe.whitelist() +def save_fields_layout(doctype: str, type: str, layout: str): + if frappe.db.exists("CRM Fields Layout", {"dt": doctype, "type": type}): + doc = frappe.get_doc("CRM Fields Layout", {"dt": doctype, "type": type}) + else: + doc = frappe.new_doc("CRM Fields Layout") + + doc.update({ + "dt": doctype, + "type": type, + "layout": layout, + }) + doc.save(ignore_permissions=True) + + return doc.layout diff --git a/frontend/src/components/Modals/ContactModal.vue b/frontend/src/components/Modals/ContactModal.vue index 4e23756f..c8e855ca 100644 --- a/frontend/src/components/Modals/ContactModal.vue +++ b/frontend/src/components/Modals/ContactModal.vue @@ -235,7 +235,7 @@ const detailFields = computed(() => { }) const sections = createResource({ - url: 'crm.api.doc.get_fields_layout', + url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_fields_layout', cache: ['quickEntryFields', 'Contact'], params: { doctype: 'Contact', type: 'Quick Entry'}, auto: true, diff --git a/frontend/src/components/Modals/DealModal.vue b/frontend/src/components/Modals/DealModal.vue index be2f7a72..08260ef0 100644 --- a/frontend/src/components/Modals/DealModal.vue +++ b/frontend/src/components/Modals/DealModal.vue @@ -77,7 +77,7 @@ const chooseExistingContact = ref(false) const chooseExistingOrganization = ref(false) const sections = createResource({ - url: 'crm.api.doc.get_fields_layout', + url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_fields_layout', cache: ['quickEntryFields', 'CRM Deal'], params: { doctype: 'CRM Deal', type: 'Quick Entry'}, auto: true, diff --git a/frontend/src/components/Modals/LeadModal.vue b/frontend/src/components/Modals/LeadModal.vue index a8245ffc..d288196d 100644 --- a/frontend/src/components/Modals/LeadModal.vue +++ b/frontend/src/components/Modals/LeadModal.vue @@ -40,7 +40,7 @@ const error = ref(null) const isLeadCreating = ref(false) const sections = createResource({ - url: 'crm.api.doc.get_fields_layout', + url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_fields_layout', cache: ['quickEntryFields', 'CRM Lead'], params: { doctype: 'CRM Lead', type: 'Quick Entry' }, auto: true, diff --git a/frontend/src/components/Modals/OrganizationModal.vue b/frontend/src/components/Modals/OrganizationModal.vue index d04205dc..a051166c 100644 --- a/frontend/src/components/Modals/OrganizationModal.vue +++ b/frontend/src/components/Modals/OrganizationModal.vue @@ -225,7 +225,7 @@ const fields = computed(() => { }) const sections = createResource({ - url: 'crm.api.doc.get_fields_layout', + url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_fields_layout', cache: ['quickEntryFields', 'CRM Organization'], params: { doctype: 'CRM Organization', type: 'Quick Entry'}, auto: true, diff --git a/frontend/src/components/Settings/FieldsLayout.vue b/frontend/src/components/Settings/FieldsLayout.vue index 66014571..9f6879c7 100644 --- a/frontend/src/components/Settings/FieldsLayout.vue +++ b/frontend/src/components/Settings/FieldsLayout.vue @@ -56,7 +56,7 @@ const doctype = ref('CRM Lead') const oldSections = ref([]) const sections = createResource({ - url: 'crm.api.doc.get_fields_layout', + url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_fields_layout', cache: ['sidebar-sections', doctype.value], params: { doctype: doctype.value, type: 'Side Panel' }, auto: true, @@ -78,7 +78,7 @@ function saveChanges() { section.fields = section.fields.map((field) => field.fieldname || field.name) }) loading.value = true - call('crm.api.doc.save_fields_layout', { + call('crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.save_fields_layout', { doctype: doctype.value, type: 'Side Panel', layout: JSON.stringify(_sections),