1
0
forked from test/crm

chore: moved fields layout related code to crm_fields_layout.py

This commit is contained in:
Shariq Ansari 2024-06-15 18:09:38 +05:30
parent bbe5fa2249
commit 6dbe3d2d53
7 changed files with 65 additions and 63 deletions

View File

@ -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 (

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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),