|
@@ -15,20 +15,20 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
all_doctypes = all_doctypes=="Yes" |
|
|
all_doctypes = all_doctypes=="Yes" |
|
|
if not parent_doctype: |
|
|
if not parent_doctype: |
|
|
parent_doctype = doctype |
|
|
parent_doctype = doctype |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
column_start_end = {} |
|
|
column_start_end = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if all_doctypes: |
|
|
if all_doctypes: |
|
|
doctype_parentfield = {} |
|
|
doctype_parentfield = {} |
|
|
child_doctypes = [] |
|
|
child_doctypes = [] |
|
|
for df in frappe.get_meta(doctype).get_table_fields(): |
|
|
for df in frappe.get_meta(doctype).get_table_fields(): |
|
|
child_doctypes.append(df.options) |
|
|
child_doctypes.append(df.options) |
|
|
doctype_parentfield[df.options] = df.fieldname |
|
|
doctype_parentfield[df.options] = df.fieldname |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_main_header(): |
|
|
def add_main_header(): |
|
|
w.writerow(['Data Import Template']) |
|
|
w.writerow(['Data Import Template']) |
|
|
w.writerow([data_keys.main_table, doctype]) |
|
|
w.writerow([data_keys.main_table, doctype]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if parent_doctype != doctype: |
|
|
if parent_doctype != doctype: |
|
|
w.writerow([data_keys.parent_table, parent_doctype]) |
|
|
w.writerow([data_keys.parent_table, parent_doctype]) |
|
|
else: |
|
|
else: |
|
@@ -50,7 +50,7 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
def build_field_columns(dt): |
|
|
def build_field_columns(dt): |
|
|
meta = frappe.get_meta(dt) |
|
|
meta = frappe.get_meta(dt) |
|
|
|
|
|
|
|
|
tablecolumns = filter(None, |
|
|
|
|
|
|
|
|
tablecolumns = filter(None, |
|
|
[(meta.get_field(f[0]) or None) for f in frappe.db.sql('desc `tab%s`' % dt)]) |
|
|
[(meta.get_field(f[0]) or None) for f in frappe.db.sql('desc `tab%s`' % dt)]) |
|
|
|
|
|
|
|
|
tablecolumns.sort(lambda a, b: a.idx - b.idx) |
|
|
tablecolumns.sort(lambda a, b: a.idx - b.idx) |
|
@@ -59,7 +59,7 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
column_start_end[dt] = frappe._dict({"start": 0}) |
|
|
column_start_end[dt] = frappe._dict({"start": 0}) |
|
|
else: |
|
|
else: |
|
|
column_start_end[dt] = frappe._dict({"start": len(columns)}) |
|
|
column_start_end[dt] = frappe._dict({"start": len(columns)}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
append_field_column(frappe._dict({ |
|
|
append_field_column(frappe._dict({ |
|
|
"fieldname": "name", |
|
|
"fieldname": "name", |
|
|
"label": "ID", |
|
|
"label": "ID", |
|
@@ -68,7 +68,7 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
"idx": 0, |
|
|
"idx": 0, |
|
|
"info": "Leave blank for new records" |
|
|
"info": "Leave blank for new records" |
|
|
}), True) |
|
|
}), True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for docfield in tablecolumns: |
|
|
for docfield in tablecolumns: |
|
|
append_field_column(docfield, True) |
|
|
append_field_column(docfield, True) |
|
|
|
|
|
|
|
@@ -82,7 +82,7 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
tablerow[column_start_end[dt].start + 2] = doctype_parentfield[dt] |
|
|
tablerow[column_start_end[dt].start + 2] = doctype_parentfield[dt] |
|
|
|
|
|
|
|
|
column_start_end[dt].end = len(columns) + 1 |
|
|
column_start_end[dt].end = len(columns) + 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def append_field_column(docfield, mandatory): |
|
|
def append_field_column(docfield, mandatory): |
|
|
if docfield and ((mandatory and docfield.reqd) or not (mandatory or docfield.reqd)) \ |
|
|
if docfield and ((mandatory and docfield.reqd) or not (mandatory or docfield.reqd)) \ |
|
|
and (docfield.fieldname not in ('parenttype', 'trash_reason')) and not docfield.hidden: |
|
|
and (docfield.fieldname not in ('parenttype', 'trash_reason')) and not docfield.hidden: |
|
@@ -93,7 +93,7 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
typerow.append(docfield.fieldtype) |
|
|
typerow.append(docfield.fieldtype) |
|
|
inforow.append(getinforow(docfield)) |
|
|
inforow.append(getinforow(docfield)) |
|
|
columns.append(docfield.fieldname) |
|
|
columns.append(docfield.fieldname) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def append_empty_field_column(): |
|
|
def append_empty_field_column(): |
|
|
tablerow.append("~") |
|
|
tablerow.append("~") |
|
|
fieldrow.append("~") |
|
|
fieldrow.append("~") |
|
@@ -114,7 +114,7 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
return 'One of: %s' % ', '.join(filter(None, docfield.options.split('\n'))) |
|
|
return 'One of: %s' % ', '.join(filter(None, docfield.options.split('\n'))) |
|
|
elif docfield.fieldtype == 'Link': |
|
|
elif docfield.fieldtype == 'Link': |
|
|
return 'Valid %s' % docfield.options |
|
|
return 'Valid %s' % docfield.options |
|
|
elif docfield.fieldtype in ('Int'): |
|
|
|
|
|
|
|
|
elif docfield.fieldtype == 'Int': |
|
|
return 'Integer' |
|
|
return 'Integer' |
|
|
elif docfield.fieldtype == "Check": |
|
|
elif docfield.fieldtype == "Check": |
|
|
return "0 or 1" |
|
|
return "0 or 1" |
|
@@ -146,28 +146,28 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
|
|
|
|
|
|
if with_data=='Yes': |
|
|
if with_data=='Yes': |
|
|
frappe.permissions.can_export(parent_doctype, raise_exception=True) |
|
|
frappe.permissions.can_export(parent_doctype, raise_exception=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# get permitted data only |
|
|
# get permitted data only |
|
|
data = frappe.get_list(doctype, fields=["*"], limit_page_length=None) |
|
|
data = frappe.get_list(doctype, fields=["*"], limit_page_length=None) |
|
|
for doc in data: |
|
|
for doc in data: |
|
|
# add main table |
|
|
# add main table |
|
|
row_group = [] |
|
|
row_group = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
add_data_row(row_group, doctype, doc, 0) |
|
|
add_data_row(row_group, doctype, doc, 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if all_doctypes: |
|
|
if all_doctypes: |
|
|
# add child tables |
|
|
# add child tables |
|
|
for child_doctype in child_doctypes: |
|
|
for child_doctype in child_doctypes: |
|
|
for ci, child in enumerate(frappe.db.sql("""select * from `tab%s` |
|
|
|
|
|
|
|
|
for ci, child in enumerate(frappe.db.sql("""select * from `tab%s` |
|
|
where parent=%s order by idx""" % (child_doctype, "%s"), doc.name, as_dict=1)): |
|
|
where parent=%s order by idx""" % (child_doctype, "%s"), doc.name, as_dict=1)): |
|
|
add_data_row(row_group, child_doctype, child, ci) |
|
|
add_data_row(row_group, child_doctype, child, ci) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for row in row_group: |
|
|
for row in row_group: |
|
|
w.writerow(row) |
|
|
w.writerow(row) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w = UnicodeWriter() |
|
|
w = UnicodeWriter() |
|
|
key = 'parent' if parent_doctype != doctype else 'name' |
|
|
key = 'parent' if parent_doctype != doctype else 'name' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
add_main_header() |
|
|
add_main_header() |
|
|
|
|
|
|
|
|
w.writerow(['']) |
|
|
w.writerow(['']) |
|
@@ -184,10 +184,10 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data |
|
|
for d in child_doctypes: |
|
|
for d in child_doctypes: |
|
|
append_empty_field_column() |
|
|
append_empty_field_column() |
|
|
build_field_columns(d) |
|
|
build_field_columns(d) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
add_field_headings() |
|
|
add_field_headings() |
|
|
add_data() |
|
|
add_data() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# write out response as a type csv |
|
|
# write out response as a type csv |
|
|
frappe.response['result'] = cstr(w.getvalue()) |
|
|
frappe.response['result'] = cstr(w.getvalue()) |
|
|
frappe.response['type'] = 'csv' |
|
|
frappe.response['type'] = 'csv' |
|
|