feat: Use field_order attr in DocType JSON file for cleaner git diffs
This commit is contained in:
parent
a11128c6d0
commit
d30ec73d43
2 changed files with 51 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ from __future__ import unicode_literals
|
|||
import frappe, os
|
||||
import frappe.model
|
||||
from frappe.modules import scrub, get_module_path, scrub_dt_dn
|
||||
import json
|
||||
|
||||
def export_doc(doc):
|
||||
export_to_files([[doc.doctype, doc.name]])
|
||||
|
|
@ -38,7 +39,35 @@ def write_document_file(doc, record_module=None, create_init=True):
|
|||
|
||||
# write the data file
|
||||
fname = scrub(doc.name)
|
||||
with open(os.path.join(folder, fname +".json"),'w+') as txtfile:
|
||||
with open(os.path.join(folder, fname + ".json"), 'a+') as txtfile:
|
||||
# if exporting DocType, retain order of 'fields' table and change order in 'field_order'
|
||||
if doc.doctype == "DocType":
|
||||
newdoc["field_order"] = [f.fieldname for f in doc.fields]
|
||||
|
||||
try:
|
||||
olddoc = json.loads(txtfile.read())
|
||||
old_field_names = [f['fieldname'] for f in olddoc.get("fields", [])]
|
||||
|
||||
if old_field_names:
|
||||
new_field_dicts = []
|
||||
remaining_field_names = [f.fieldname for f in doc.fields]
|
||||
|
||||
for fieldname in old_field_names:
|
||||
field_dict = filter(lambda d: d['fieldname'] == fieldname, newdoc['fields'])
|
||||
if field_dict:
|
||||
new_field_dicts.append(field_dict[0])
|
||||
remaining_field_names.remove(fieldname)
|
||||
|
||||
for fieldname in remaining_field_names:
|
||||
field_dict = filter(lambda d: d['fieldname'] == fieldname, newdoc['fields'])
|
||||
new_field_dicts.append(field_dict[0])
|
||||
|
||||
newdoc['fields'] = new_field_dicts
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
txtfile.seek(0)
|
||||
txtfile.truncate()
|
||||
txtfile.write(frappe.as_json(newdoc))
|
||||
|
||||
def get_module_name(doc):
|
||||
|
|
|
|||
|
|
@ -89,6 +89,27 @@ def read_doc_from_file(path):
|
|||
else:
|
||||
raise IOError('%s missing' % path)
|
||||
|
||||
# set order of fields from field_order
|
||||
if doc.get("doctype") == "DocType":
|
||||
if doc.get("field_order") and doc.get("fields"):
|
||||
new_field_dicts = []
|
||||
remaining_field_names = [f['fieldname'] for f in doc['fields']]
|
||||
|
||||
for fieldname in doc['field_order']:
|
||||
field_dict = filter(lambda d: d['fieldname'] == fieldname, doc['fields'])
|
||||
if field_dict:
|
||||
new_field_dicts.append(field_dict[0])
|
||||
remaining_field_names.remove(fieldname)
|
||||
|
||||
for fieldname in remaining_field_names:
|
||||
field_dict = filter(lambda d: d['fieldname'] == fieldname, doc['fields'])
|
||||
new_field_dicts.append(field_dict[0])
|
||||
|
||||
doc['fields'] = new_field_dicts
|
||||
|
||||
if "field_order" in doc:
|
||||
del doc['field_order']
|
||||
|
||||
return doc
|
||||
|
||||
ignore_doctypes = [""]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue