Skip to content

Commit

Permalink
fix: fieldname referenced before assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra authored and akhilnarang committed Mar 20, 2024
1 parent eab535d commit 95a0db8
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions frappe/desk/reportview.py
Expand Up @@ -433,16 +433,12 @@ def get_labels(fields, doctype):
"""get column labels based on column names"""
labels = []
for key in fields:
key = key.split(" as ")[0]

if key.startswith(("count(", "sum(", "avg(")):
try:
parenttype, fieldname = parse_field(key)
except ValueError:
continue

if "." in key:
parenttype, fieldname = key.split(".")[0][4:-1], key.split(".")[1].strip("`")
else:
parenttype = doctype
fieldname = fieldname.strip("`")
parenttype = parenttype or doctype

if parenttype == doctype and fieldname == "name":
label = _("ID", context="Label of name column in report")
Expand All @@ -461,17 +457,12 @@ def get_labels(fields, doctype):

def handle_duration_fieldtype_values(doctype, data, fields):
for field in fields:
key = field.split(" as ")[0]

if key.startswith(("count(", "sum(", "avg(")):
try:
parenttype, fieldname = parse_field(field)
except ValueError:
continue

if "." in key:
parenttype, fieldname = key.split(".")[0][4:-1], key.split(".")[1].strip("`")
else:
parenttype = doctype
fieldname = field.strip("`")

parenttype = parenttype or doctype
df = frappe.get_meta(parenttype).get_field(fieldname)

if df and df.fieldtype == "Duration":
Expand All @@ -484,6 +475,19 @@ def handle_duration_fieldtype_values(doctype, data, fields):
return data


def parse_field(field: str) -> tuple[str | None, str]:
"""Parse a field into parenttype and fieldname."""
key = field.split(" as ")[0]

if key.startswith(("count(", "sum(", "avg(")):
raise ValueError

if "." in key:
return key.split(".")[0][4:-1], key.split(".")[1].strip("`")

return None, key.strip("`")


@frappe.whitelist()
def delete_items():
"""delete selected items"""
Expand Down

0 comments on commit 95a0db8

Please sign in to comment.