From 330072c687efd34b28362c746b8ec33114a70953 Mon Sep 17 00:00:00 2001 From: Saif Ur Rehman Date: Thu, 18 Apr 2019 17:52:47 +0500 Subject: [PATCH] feat: Remove null and empty attributes from DocType JSON file on export --- frappe/core/doctype/doctype/doctype.py | 16 ++++++++++++++++ frappe/modules/import_file.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 61169e91e9..012834c696 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -18,6 +18,7 @@ from frappe.modules import make_boilerplate, get_doc_path from frappe.model.db_schema import validate_column_name, validate_column_length, type_map from frappe.model.docfield import supports_translation from frappe.modules.import_file import get_file_path +from six import iteritems import frappe.website.render import json @@ -393,6 +394,21 @@ class DocType(Document): make_property_setter(self.name, "naming_series", "default", naming_series[0].default, "Text", validate_fields_for_doctype=False) def before_export(self, docdict): + # remove null and empty fields + def remove_null_fields(o): + to_remove = [] + for attr, value in iteritems(o): + if isinstance(value, list): + for v in value: + remove_null_fields(v) + elif not value: + to_remove.append(attr) + + for attr in to_remove: + del o[attr] + + remove_null_fields(docdict) + # retain order of 'fields' table and change order in 'field_order' docdict["field_order"] = [f.fieldname for f in self.fields] diff --git a/frappe/modules/import_file.py b/frappe/modules/import_file.py index 1feb9fba5c..d3e00f5342 100644 --- a/frappe/modules/import_file.py +++ b/frappe/modules/import_file.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals, print_function import frappe, os, json -from frappe.modules import get_module_path, scrub_dt_dn, load_doctype_module +from frappe.modules import get_module_path, scrub_dt_dn from frappe.utils import get_datetime_str from frappe.model.base_document import get_controller