fix: api for getting fields layout and refactor old code

This commit is contained in:
Shariq Ansari 2024-06-15 12:22:00 +05:30
parent dff9ca8c2d
commit 7aacbb4c77
7 changed files with 56 additions and 52 deletions

View File

@ -66,12 +66,12 @@ def get_filterable_fields(doctype: str):
# append DocFields # append DocFields
DocField = frappe.qb.DocType("DocField") DocField = frappe.qb.DocType("DocField")
doc_fields = get_fields_meta(DocField, doctype, allowed_fieldtypes, restricted_fields) doc_fields = get_doctype_fields_meta(DocField, doctype, allowed_fieldtypes, restricted_fields)
res.extend(doc_fields) res.extend(doc_fields)
# append Custom Fields # append Custom Fields
CustomField = frappe.qb.DocType("Custom Field") CustomField = frappe.qb.DocType("Custom Field")
custom_fields = get_fields_meta(CustomField, doctype, allowed_fieldtypes, restricted_fields) custom_fields = get_doctype_fields_meta(CustomField, doctype, allowed_fieldtypes, restricted_fields)
res.extend(custom_fields) res.extend(custom_fields)
# append standard fields (getting error when using frappe.model.std_fields) # append standard fields (getting error when using frappe.model.std_fields)
@ -170,13 +170,15 @@ def get_fields_layout(doctype: str, type: str):
allowed_fields = [] allowed_fields = []
for section in sections: for section in sections:
if not section.get("fields"):
continue
allowed_fields.extend(section.get("fields")) allowed_fields.extend(section.get("fields"))
fields = frappe.get_meta(doctype).fields fields = frappe.get_meta(doctype).fields
fields = [field for field in fields if field.fieldname in allowed_fields] fields = [field for field in fields if field.fieldname in allowed_fields]
for section in sections: for section in sections:
for field in section.get("fields"): for field in section.get("fields") if section.get("fields") else []:
field = next((f for f in fields if f.fieldname == field), None) field = next((f for f in fields if f.fieldname == field), None)
if field: if field:
if field.fieldtype == "Select": if field.fieldtype == "Select":
@ -211,7 +213,7 @@ def save_fields_layout(doctype: str, type: str, layout: str):
return doc.layout return doc.layout
def get_fields_meta(DocField, doctype, allowed_fieldtypes, restricted_fields): def get_doctype_fields_meta(DocField, doctype, allowed_fieldtypes, restricted_fields):
parent = "parent" if DocField._table_name == "tabDocField" else "dt" parent = "parent" if DocField._table_name == "tabDocField" else "dt"
return ( return (
frappe.qb.from_(DocField) frappe.qb.from_(DocField)
@ -430,8 +432,9 @@ def get_list_data(
} }
def get_doctype_fields(doctype, name): def get_fields_meta(doctype):
not_allowed_fieldtypes = [ not_allowed_fieldtypes = [
"Tab Break",
"Section Break", "Section Break",
"Column Break", "Column Break",
] ]
@ -439,56 +442,57 @@ def get_doctype_fields(doctype, name):
fields = frappe.get_meta(doctype).fields fields = frappe.get_meta(doctype).fields
fields = [field for field in fields if field.fieldtype not in not_allowed_fieldtypes] fields = [field for field in fields if field.fieldtype not in not_allowed_fieldtypes]
sections = {} fields_meta = {}
section_fields = [] for field in fields:
last_section = None fields_meta[field.fieldname] = field
doc = frappe.get_cached_doc(doctype, name)
return fields_meta
@frappe.whitelist()
def get_sidebar_fields(doctype, name):
if not frappe.db.exists("CRM Fields Layout", {"dt": doctype, "type": "Side Panel"}):
return []
layout = frappe.get_doc("CRM Fields Layout", {"dt": doctype, "type": "Side Panel"}).layout
if not layout:
return []
layout = json.loads(layout)
not_allowed_fieldtypes = [
"Tab Break",
"Section Break",
"Column Break",
]
fields = frappe.get_meta(doctype).fields
fields = [field for field in fields if field.fieldtype not in not_allowed_fieldtypes]
doc = frappe.get_cached_doc(doctype, name)
has_high_permlevel_fields = any(df.permlevel > 0 for df in fields) has_high_permlevel_fields = any(df.permlevel > 0 for df in fields)
if has_high_permlevel_fields: if has_high_permlevel_fields:
has_read_access_to_permlevels = doc.get_permlevel_access("read") has_read_access_to_permlevels = doc.get_permlevel_access("read")
has_write_access_to_permlevels = doc.get_permlevel_access("write") has_write_access_to_permlevels = doc.get_permlevel_access("write")
for field in fields: for section in layout:
if field.fieldtype == "Tab Break" and last_section: section["name"] = section.get("name") or section.get("label")
sections[last_section]["fields"] = section_fields for field in section.get("fields") if section.get("fields") else []:
last_section = None field_obj = next((f for f in fields if f.fieldname == field), None)
if field.read_only: if field_obj:
section_fields = [] if field_obj.permlevel > 0:
continue field_has_write_access = field_obj.permlevel in has_write_access_to_permlevels
if field.fieldtype == "Tab Break": field_has_read_access = field_obj.permlevel in has_read_access_to_permlevels
if field.read_only: if not field_has_write_access and field_has_read_access:
section_fields = [] field_obj.read_only = 1
continue if not field_has_read_access and not field_has_write_access:
section_fields = [] field_obj.hidden = 1
last_section = field.fieldname section["fields"][section.get("fields").index(field)] = get_field_obj(field_obj)
sections[field.fieldname] = {
"label": field.label,
"name": field.fieldname,
"opened": True,
"fields": [],
}
else:
if field.permlevel > 0:
field_has_write_access = field.permlevel in has_write_access_to_permlevels
field_has_read_access = field.permlevel in has_read_access_to_permlevels
if not field_has_write_access and field_has_read_access:
field.read_only = 1
if not field_has_read_access and not field_has_write_access:
field.hidden = 1
section_fields.append(get_field_obj(field))
section_fields = []
for section in sections:
section_fields.append(sections[section])
fields = [field for field in fields if field.fieldtype not in "Tab Break"]
fields_meta = {} fields_meta = {}
for field in fields: for field in fields:
fields_meta[field.fieldname] = field fields_meta[field.fieldname] = field
return section_fields, fields_meta return layout
def get_field_obj(field): def get_field_obj(field):
obj = { obj = {

View File

@ -1,7 +1,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from crm.api.doc import get_doctype_fields, get_assigned_users from crm.api.doc import get_fields_meta, get_assigned_users
from crm.fcrm.doctype.crm_form_script.crm_form_script import get_form_script from crm.fcrm.doctype.crm_form_script.crm_form_script import get_form_script
@frappe.whitelist() @frappe.whitelist()
@ -27,8 +27,8 @@ def get_deal(name):
fields=["contact", "is_primary"], fields=["contact", "is_primary"],
) )
deal["doctype_fields"], deal["all_fields"] = get_doctype_fields("CRM Deal", name)
deal["doctype"] = "CRM Deal" deal["doctype"] = "CRM Deal"
deal["fields_meta"] = get_fields_meta("CRM Deal")
deal["_form_script"] = get_form_script('CRM Deal') deal["_form_script"] = get_form_script('CRM Deal')
deal["_assign"] = get_assigned_users("CRM Deal", deal.name, deal.owner) deal["_assign"] = get_assigned_users("CRM Deal", deal.name, deal.owner)
return deal return deal

View File

@ -1,7 +1,7 @@
import frappe import frappe
from frappe import _ from frappe import _
from crm.api.doc import get_doctype_fields, get_assigned_users from crm.api.doc import get_fields_meta, get_assigned_users
from crm.fcrm.doctype.crm_form_script.crm_form_script import get_form_script from crm.fcrm.doctype.crm_form_script.crm_form_script import get_form_script
@frappe.whitelist() @frappe.whitelist()
@ -15,8 +15,8 @@ def get_lead(name):
frappe.throw(_("Lead not found"), frappe.DoesNotExistError) frappe.throw(_("Lead not found"), frappe.DoesNotExistError)
lead = lead.pop() lead = lead.pop()
lead["doctype_fields"], lead["all_fields"] = get_doctype_fields("CRM Lead", name)
lead["doctype"] = "CRM Lead" lead["doctype"] = "CRM Lead"
lead["fields_meta"] = get_fields_meta("CRM Lead")
lead["_form_script"] = get_form_script('CRM Lead') lead["_form_script"] = get_form_script('CRM Lead')
lead["_assign"] = get_assigned_users("CRM Lead", lead.name, lead.owner) lead["_assign"] = get_assigned_users("CRM Lead", lead.name, lead.owner)
return lead return lead

View File

@ -414,7 +414,7 @@ function updateDeal(fieldname, value, callback) {
} }
function validateRequired(fieldname, value) { function validateRequired(fieldname, value) {
let meta = deal.data.all_fields || {} let meta = deal.data.fields_meta || {}
if (meta[fieldname]?.reqd && !value) { if (meta[fieldname]?.reqd && !value) {
createToast({ createToast({
title: __('Error Updating Deal'), title: __('Error Updating Deal'),

View File

@ -384,7 +384,7 @@ function updateLead(fieldname, value, callback) {
} }
function validateRequired(fieldname, value) { function validateRequired(fieldname, value) {
let meta = lead.data.all_fields || {} let meta = lead.data.fields_meta || {}
if (meta[fieldname]?.reqd && !value) { if (meta[fieldname]?.reqd && !value) {
createToast({ createToast({
title: __('Error Updating Lead'), title: __('Error Updating Lead'),

View File

@ -369,7 +369,7 @@ function updateDeal(fieldname, value, callback) {
} }
function validateRequired(fieldname, value) { function validateRequired(fieldname, value) {
let meta = deal.data.all_fields || {} let meta = deal.data.fields_meta || {}
if (meta[fieldname]?.reqd && !value) { if (meta[fieldname]?.reqd && !value) {
createToast({ createToast({
title: __('Error Updating Deal'), title: __('Error Updating Deal'),

View File

@ -283,7 +283,7 @@ function updateLead(fieldname, value, callback) {
} }
function validateRequired(fieldname, value) { function validateRequired(fieldname, value) {
let meta = lead.data.all_fields || {} let meta = lead.data.fields_meta || {}
if (meta[fieldname]?.reqd && !value) { if (meta[fieldname]?.reqd && !value) {
createToast({ createToast({
title: __('Error Updating Lead'), title: __('Error Updating Lead'),