refactor: generate_report_result method

- to fix codacy complexity issue
This commit is contained in:
Suraj Shetty 2020-04-01 12:34:18 +05:30
parent 5f5ab02260
commit fe54fb67c9
3 changed files with 30 additions and 33 deletions

View file

@ -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

View file

@ -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
return _list

View file

@ -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
}