fix(goal): improve validation (#35186)

This commit is contained in:
Akhil Narang 2025-12-12 13:50:12 +05:30 committed by GitHub
parent ff3ada7030
commit ec615fe9aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -20,6 +20,17 @@ def get_monthly_results(
) -> dict:
"""Get monthly aggregation values for given field of doctype"""
# Only allow some valid aggregations
if aggregation.lower() not in {"sum", "avg", "count", "min", "max"}:
frappe.throw(f"Invalid aggregation type: {aggregation}")
# Check that the goal and date fields exist on the chosen doctype
valid_fields = frappe.get_meta(goal_doctype).get_valid_fields()
if goal_field not in valid_fields:
frappe.throw(f"Invalid goal field: {goal_field}")
if date_col not in valid_fields:
frappe.throw(f"Invalid date field: {date_col}")
Table = DocType(goal_doctype)
date_format = "%m-%Y" if frappe.db.db_type != "postgres" else "MM-YYYY"
@ -32,6 +43,7 @@ def get_monthly_results(
],
filters=filters,
validate_filters=True,
ignore_permissions=False,
)
.groupby("month_year")
.run()