fix: extra column in excel after exporting report with group by (#17126)

Co-authored-by: gavin <gavin18d@gmail.com>
This commit is contained in:
Shariq Ansari 2022-06-28 12:39:45 +05:30 committed by GitHub
parent 9e1d7742eb
commit 971b8160a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 9 deletions

View file

@ -156,8 +156,6 @@ def setup_group_by(data):
**data
)
)
if data.aggregate_on_field:
data.fields.append(f"`tab{data.aggregate_on_doctype}`.`{data.aggregate_on_field}`")
else:
raise_invalid_field(data.aggregate_on_field)
@ -435,11 +433,20 @@ def append_totals_row(data):
def get_labels(fields, doctype):
"""get column labels based on column names"""
labels = []
doctype = doctype.lower()
for key in fields:
key = key.split(" as ")[0]
aggregate_function = ""
key = key.casefold().split(" as ", maxsplit=1)[0]
if key.startswith(("count(", "sum(", "avg(")):
continue
# Get aggregate function and _aggregate_column
# key = 'sum(`tabDocType`.`fieldname`)'
if not key.rstrip().endswith(")"):
continue
_agg_fn, _key = key.split("(", maxsplit=1)
aggregate_function = _agg_fn.lower() # aggregate_function = 'sum'
key = _key[:-1] # key = `tabDocType`.`fieldname`
if "." in key:
parenttype, fieldname = key.split(".")[0][4:-1], key.split(".")[1].strip("`")
@ -455,7 +462,10 @@ def get_labels(fields, doctype):
if parenttype != doctype:
# If the column is from a child table, append the child doctype.
# For example, "Item Code (Sales Invoice Item)".
label += f" ({ _(parenttype) })"
label += f" ({ _(parenttype.title()) })"
if aggregate_function:
label = _("{0} of {1}").format(aggregate_function.capitalize(), label)
labels.append(label)
@ -464,7 +474,7 @@ def get_labels(fields, doctype):
def handle_duration_fieldtype_values(doctype, data, fields):
for field in fields:
key = field.split(" as ")[0]
key = field.casefold().split(" as ", maxsplit=1)[0]
if key.startswith(("count(", "sum(", "avg(")):
continue

View file

@ -417,6 +417,8 @@ class DatabaseQuery(object):
"extract(",
"locate(",
"strpos(",
]
aggregate_functions = [
"count(",
"sum(",
"avg(",
@ -427,6 +429,9 @@ class DatabaseQuery(object):
if not ("tab" in field and "." in field) or any(x for x in sql_functions if x in field):
continue
if any(x for x in aggregate_functions if x in field):
field = field.split("(", 1)[1][:-1]
table_name = field.split(".")[0]
if table_name.lower().startswith("group_concat("):

View file

@ -643,9 +643,7 @@ class TestReportview(unittest.TestCase):
)
response = execute_cmd("frappe.desk.reportview.get")
self.assertListEqual(
response["keys"], ["field_label", "field_name", "_aggregate_column", "columns"]
)
self.assertListEqual(response["keys"], ["field_label", "field_name", "_aggregate_column"])
def test_cast_name(self):
from frappe.core.doctype.doctype.test_doctype import new_doctype