refactor: move call to fetch doctype fields to server-side

This commit is contained in:
Rucha Mahabal 2020-11-30 16:09:15 +05:30
parent 0d43ea5ebb
commit b5b8c5474a
2 changed files with 38 additions and 26 deletions

View File

@ -18,7 +18,27 @@ frappe.ui.form.on('Patient History Settings', {
if (doc.selected_fields)
document_fields = (JSON.parse(doc.selected_fields)).map(f => f.fieldname);
let doctype_fields = frm.events.get_doctype_fields(frm, doc.document_type, document_fields);
frm.call({
method: 'get_doctype_fields',
doc: frm.doc,
args: {
document_type: doc.document_type,
fields: document_fields
},
freeze: true,
callback: function(r) {
if (r.message) {
let doctype = 'Patient History Custom Document Type';
if (standard)
doctype = 'Patient History Standard Document Type';
frm.events.show_field_selector_dialog(frm, doc, doctype, r.message);
}
}
});
},
show_field_selector_dialog: function(frm, doc, doctype, doc_fields) {
let d = new frappe.ui.Dialog({
title: __('{0} Fields', [__(doc.document_type)]),
fields: [
@ -26,7 +46,7 @@ frappe.ui.form.on('Patient History Settings', {
label: __('Select Fields'),
fieldtype: 'MultiCheck',
fieldname: 'fields',
options: doctype_fields,
options: doc_fields,
columns: 2
}
]
@ -49,9 +69,6 @@ frappe.ui.form.on('Patient History Settings', {
});
}
}
let doctype = 'Patient History Custom Document Type';
if (standard)
doctype = 'Patient History Standard Document Type';
d.refresh();
frappe.model.set_value(doctype, doc.name, 'selected_fields', JSON.stringify(selected_fields));
@ -59,26 +76,6 @@ frappe.ui.form.on('Patient History Settings', {
});
d.show();
},
get_doctype_fields(frm, document_type, fields) {
let multiselect_fields = [];
frappe.model.with_doctype(document_type, () => {
// get doctype fields
frappe.get_doc('DocType', document_type).fields.forEach(field => {
if (!in_list(frappe.model.no_value_type, field.fieldtype) ||
in_list(frappe.model.table_fields, field.fieldtype) && !field.hidden) {
multiselect_fields.push({
label: field.label,
value: field.fieldname,
checked: in_list(fields, field.fieldname)
});
}
});
});
return multiselect_fields;
}
});

View File

@ -4,6 +4,7 @@
from __future__ import unicode_literals
import frappe
import json
from frappe import _
from frappe.utils import cstr
from frappe.model.document import Document
@ -24,6 +25,21 @@ class PatientHistorySettings(Document):
frappe.throw(_('Row #{0}: Field {1} in Document Type {2} is not a Date / Datetime field.').format(
entry.idx, frappe.bold(entry.date_fieldname), frappe.bold(entry.document_type)))
def get_doctype_fields(self, document_type, fields):
multicheck_fields = []
doc_fields = frappe.get_meta(document_type).fields
for field in doc_fields:
if field.fieldtype not in frappe.model.no_value_fields or \
field.fieldtype in frappe.model.table_fields and not field.hidden:
multicheck_fields.append({
'label': field.label,
'value': field.fieldname,
'checked': 1 if field.fieldname in fields else 0
})
return multicheck_fields
def create_medical_record(doc, method=None):
medical_record_required = validate_medical_record_required(doc)
@ -100,7 +116,6 @@ def get_date_field(doctype):
def get_patient_history_fields(doc):
import json
dt = get_patient_history_config_dt(doc.doctype)
patient_history_fields = frappe.db.get_value(dt, { 'document_type': doc.doctype }, 'selected_fields')