strip unwanted fields from json fixture export
This commit is contained in:
parent
e03a2323a5
commit
1b59d640ee
2 changed files with 17 additions and 7 deletions
|
|
@ -44,6 +44,19 @@ def export_csv(doctype, path):
|
|||
csvfile.write(frappe.response.result.encode("utf-8"))
|
||||
|
||||
def export_json(doctype, path, filters=None):
|
||||
def post_process(out):
|
||||
del_keys = ('parent', 'parentfield', 'parenttype', 'modified_by', 'creation', 'owner', 'idx')
|
||||
for doc in out:
|
||||
for key in del_keys:
|
||||
if key in doc:
|
||||
del doc[key]
|
||||
for k, v in doc.items():
|
||||
if isinstance(v, list):
|
||||
for child in v:
|
||||
for key in del_keys + ('docstatus', 'doctype', 'modified', 'name'):
|
||||
if key in child:
|
||||
del child[key]
|
||||
|
||||
from frappe.utils.response import json_handler
|
||||
out = []
|
||||
if frappe.db.get_value("DocType", doctype, "issingle"):
|
||||
|
|
@ -51,6 +64,7 @@ def export_json(doctype, path, filters=None):
|
|||
else:
|
||||
for doc in frappe.get_all(doctype, fields=["name"], filters=filters, limit_page_length=0, order_by="creation asc"):
|
||||
out.append(frappe.get_doc(doctype, doc.name).as_dict())
|
||||
post_process(out)
|
||||
with open(path, "w") as outfile:
|
||||
outfile.write(json.dumps(out, default=json_handler, indent=1, sort_keys=True))
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import frappe, os
|
||||
from frappe.core.page.data_import_tool.data_import_tool import import_doc, export_fixture, export_csv
|
||||
from frappe.core.page.data_import_tool.data_import_tool import import_doc, export_json
|
||||
|
||||
def sync_fixtures(app=None):
|
||||
if app:
|
||||
|
|
@ -25,9 +25,5 @@ def export_fixtures():
|
|||
print "Exporting {0}".format(fixture)
|
||||
if not os.path.exists(frappe.get_app_path(app, "fixtures")):
|
||||
os.mkdir(frappe.get_app_path(app, "fixtures"))
|
||||
if isinstance(fixture, basestring):
|
||||
fixture = [fixture];
|
||||
if frappe.db.get_value("DocType", fixture[0], "issingle"):
|
||||
export_fixture(fixture[0], app)
|
||||
else:
|
||||
export_csv(fixture, frappe.get_app_path(app, "fixtures", frappe.scrub(fixture[0]) + ".csv"))
|
||||
|
||||
export_json(fixture, frappe.get_app_path(app, "fixtures", frappe.scrub(fixture) + ".json"))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue