From fe54fb67c975d8c88645d8b34dc63263184e940a Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Wed, 1 Apr 2020 12:34:18 +0530 Subject: [PATCH] refactor: generate_report_result method - to fix codacy complexity issue --- frappe/core/doctype/report/report.py | 14 +++++++++- frappe/core/utils.py | 7 +++-- frappe/desk/query_report.py | 42 +++++++++------------------- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/frappe/core/doctype/report/report.py b/frappe/core/doctype/report/report.py index 2d49915f59..967b28b8b2 100644 --- a/frappe/core/doctype/report/report.py +++ b/frappe/core/doctype/report/report.py @@ -6,7 +6,7 @@ import frappe import json, datetime from frappe import _, scrub import frappe.desk.query_report -from frappe.utils import cint +from frappe.utils import cint, cstr from frappe.model.document import Document from frappe.modules.export_file import export_to_files from frappe.modules import make_boilerplate @@ -92,6 +92,18 @@ class Report(Document): make_boilerplate("controller.py", self, {"name": self.name}) make_boilerplate("controller.js", self, {"name": self.name}) + def execute_query_report(self, filters): + if not self.query: + frappe.throw(_("Must specify a Query to run"), title=_('Report Document Error')) + + if not self.query.lower().startswith("select"): + frappe.throw(_("Query must be a SELECT"), title=_('Report Document Error')) + + result = [list(t) for t in frappe.db.sql(self.query, filters)] + columns = [cstr(c[0]) for c in frappe.db.get_description()] + + return [columns, result] + def execute_script_report(self, filters): # save the timestamp to automatically set to prepared threshold = 30 diff --git a/frappe/core/utils.py b/frappe/core/utils.py index abab9e6160..55cfbc34d7 100644 --- a/frappe/core/utils.py +++ b/frappe/core/utils.py @@ -69,16 +69,17 @@ def find_all(list_of_dict, match_function): return found def ljust_list(_list, length, fill_word=None): - '''Similar to ljust but for list + """ + Similar to ljust but for list. Usage: $ ljust_list([1, 2, 3], 5) > [1, 2, 3, None, None] - ''' + """ # make a copy to avoid mutation of passed list _list = list(_list) fill_length = length - len(_list) if fill_length > 0: _list.extend([fill_word] * fill_length) - return _list \ No newline at end of file + return _list diff --git a/frappe/desk/query_report.py b/frappe/desk/query_report.py index 994f816c8a..aaf859e7fd 100644 --- a/frappe/desk/query_report.py +++ b/frappe/desk/query_report.py @@ -8,7 +8,7 @@ import os, json from frappe import _ from frappe.modules import scrub, get_module_path -from frappe.utils import flt, cint, get_html_format, cstr, get_url_to_form +from frappe.utils import flt, cint, get_html_format, get_url_to_form from frappe.model.utils import render_include from frappe.translate import send_translations import frappe.desk.reportview @@ -16,6 +16,7 @@ from frappe.permissions import get_role_permissions from six import string_types, iteritems from datetime import timedelta from frappe.utils import gzip_decompress +from frappe.core.utils import ljust_list def get_report_doc(report_name): doc = frappe.get_doc("Report", report_name) @@ -43,44 +44,27 @@ def get_report_doc(report_name): def generate_report_result(report, filters=None, user=None, custom_columns=None): - status = None - if not user: - user = frappe.session.user - if not filters: - filters = [] + user = user or frappe.session.user + filters = filters or [] if filters and isinstance(filters, string_types): filters = json.loads(filters) - columns, result, message, chart, report_summary, skip_total_row = [], [], None, None, None, 0 + + res = [] + if report.report_type == "Query Report": - if not report.query: - status = "error" - frappe.msgprint(_("Must specify a Query to run"), raise_exception=True) - - if not report.query.lower().startswith("select"): - status = "error" - frappe.msgprint(_("Query must be a SELECT"), raise_exception=True) - - result = [list(t) for t in frappe.db.sql(report.query, filters)] - columns = [cstr(c[0]) for c in frappe.db.get_description()] + res = report.execute_query_report(filters) elif report.report_type == 'Script Report': res = report.execute_script_report(filters) - columns, result = res[0], res[1] - if len(res) > 2: - message = res[2] - if len(res) > 3: - chart = res[3] - if len(res) > 4: - report_summary = res[4] - if len(res) > 5: - skip_total_row = cint(res[5]) + columns, result, message, chart, report_summary, skip_total_row = \ + ljust_list(res, 6) if report.custom_columns: columns = json.loads(report.custom_columns) result = add_data_to_custom_columns(columns, result) - elif custom_columns: + if custom_columns: result = add_data_to_custom_columns(custom_columns, result) for custom_column in custom_columns: @@ -98,8 +82,8 @@ def generate_report_result(report, filters=None, user=None, custom_columns=None) "message": message, "chart": chart, "report_summary": report_summary, - "skip_total_row": skip_total_row, - "status": status, + "skip_total_row": skip_total_row or 0, + "status": None, "execution_time": frappe.cache().hget('report_execution_time', report.name) or 0 }