From 15b3cc890f9c14c78b2fb6932e698ce206c54c95 Mon Sep 17 00:00:00 2001 From: casesolved-co-uk Date: Sat, 6 Mar 2021 08:55:58 +0000 Subject: [PATCH 1/2] feat: allow query/custom reports to save custom data in the json field --- .../prepared_report/prepared_report.py | 5 ++++- frappe/desk/query_report.py | 14 +++++++++---- frappe/patches.txt | 1 + frappe/patches/v13_0/queryreport_columns.py | 21 +++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 frappe/patches/v13_0/queryreport_columns.py diff --git a/frappe/core/doctype/prepared_report/prepared_report.py b/frappe/core/doctype/prepared_report/prepared_report.py index e947cee8ed..c27853f460 100644 --- a/frappe/core/doctype/prepared_report/prepared_report.py +++ b/frappe/core/doctype/prepared_report/prepared_report.py @@ -37,7 +37,10 @@ def run_background(prepared_report): custom_report_doc = report reference_report = custom_report_doc.reference_report report = frappe.get_doc("Report", reference_report) - report.custom_columns = custom_report_doc.json + if custom_report_doc.json: + data = json.loads(custom_report_doc.json) + if data: + report.custom_columns = data["columns"] result = generate_report_result( report=report, diff --git a/frappe/desk/query_report.py b/frappe/desk/query_report.py index 22d47d1120..9589507ca6 100644 --- a/frappe/desk/query_report.py +++ b/frappe/desk/query_report.py @@ -36,7 +36,10 @@ def get_report_doc(report_name): reference_report = custom_report_doc.reference_report doc = frappe.get_doc("Report", reference_report) doc.custom_report = report_name - doc.custom_columns = custom_report_doc.json + if custom_report_doc.json: + data = json.loads(custom_report_doc.json) + if data: + doc.custom_columns = data["columns"] doc.is_custom_report = True if not doc.is_permitted(): @@ -83,7 +86,7 @@ def generate_report_result(report, filters=None, user=None, custom_columns=None) if report.custom_columns: # saved columns (with custom columns / with different column order) - columns = json.loads(report.custom_columns) + columns = report.custom_columns # unsaved custom_columns if custom_columns: @@ -524,9 +527,12 @@ def save_report(reference_report, report_name, columns): "report_type": "Custom Report", }, ) + if docname: report = frappe.get_doc("Report", docname) - report.update({"json": columns}) + existing_jd = json.loads(report.json) + existing_jd["columns"] = json.loads(columns) + report.update({"json": json.dumps(existing_jd, separators=(',', ':'))}) report.save() frappe.msgprint(_("Report updated successfully")) @@ -536,7 +542,7 @@ def save_report(reference_report, report_name, columns): { "doctype": "Report", "report_name": report_name, - "json": columns, + "json": f'{{"columns":{columns}}}', "ref_doctype": report_doc.ref_doctype, "is_standard": "No", "report_type": "Custom Report", diff --git a/frappe/patches.txt b/frappe/patches.txt index 5251b3da30..516ddb6094 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -334,3 +334,4 @@ frappe.patches.v13_0.delete_package_publish_tool frappe.patches.v13_0.rename_list_view_setting_to_list_view_settings frappe.patches.v13_0.remove_twilio_settings frappe.patches.v12_0.rename_uploaded_files_with_proper_name +frappe.patches.v13_0.queryreport_columns diff --git a/frappe/patches/v13_0/queryreport_columns.py b/frappe/patches/v13_0/queryreport_columns.py new file mode 100644 index 0000000000..cc09bae37c --- /dev/null +++ b/frappe/patches/v13_0/queryreport_columns.py @@ -0,0 +1,21 @@ +# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +from __future__ import unicode_literals +import frappe, json + +def execute(): + """Convert Query Report json to support other content""" + records = frappe.get_all('Report', + filters={ + "json": ["!=", ""] + }, + fields=["name", "json"] + ) + for record in records: + jstr = record["json"] + data = json.loads(jstr) + if isinstance(data, list): + # double escape braces + jstr = f'{{"columns":{jstr}}}' + frappe.db.update('Report', record["name"], "json", jstr) From 49a79e4d0076a940a32763191dcfa45e846dd014 Mon Sep 17 00:00:00 2001 From: casesolved-co-uk Date: Fri, 9 Apr 2021 00:27:02 +0000 Subject: [PATCH 2/2] chore: fix sider --- frappe/patches/v13_0/queryreport_columns.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frappe/patches/v13_0/queryreport_columns.py b/frappe/patches/v13_0/queryreport_columns.py index cc09bae37c..6c2a1b1219 100644 --- a/frappe/patches/v13_0/queryreport_columns.py +++ b/frappe/patches/v13_0/queryreport_columns.py @@ -2,7 +2,8 @@ # MIT License. See license.txt from __future__ import unicode_literals -import frappe, json +import frappe +import json def execute(): """Convert Query Report json to support other content"""