Merge pull request #19265 from ankush/prepared_report_sort

fix: sort keys in prepared report filters
This commit is contained in:
Ankush Menat 2022-12-13 16:50:01 +05:30 committed by GitHub
commit b91354e28f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@
import json
from typing import Any
from rq import get_current_job
@ -106,22 +107,31 @@ def make_prepared_report(report_name, filters=None):
{
"doctype": "Prepared Report",
"report_name": report_name,
# This looks like an insanity but, without this it'd be very hard to find Prepared Reports matching given condition
# We're ensuring that spacing is consistent. e.g. JS seems to put no spaces after ":", Python on the other hand does.
"filters": json.dumps(json.loads(filters)),
"filters": process_filters_for_prepared_report(filters),
}
).insert(ignore_permissions=True)
return {"name": prepared_report.name}
def process_filters_for_prepared_report(filters: dict[str, Any] | str) -> str:
if isinstance(filters, str):
filters = json.loads(filters)
# This looks like an insanity but, without this it'd be very hard to find Prepared Reports matching given condition
# We're ensuring that spacing is consistent. e.g. JS seems to put no spaces after ":", Python on the other hand does.
# We are also ensuring that order of keys is same so generated JSON string will be identical too.
# PS: frappe.as_json sorts keys
return frappe.as_json(filters, indent=None, separators=(",", ":"))
@frappe.whitelist()
def get_reports_in_queued_state(report_name, filters):
reports = frappe.get_all(
"Prepared Report",
filters={
"report_name": report_name,
"filters": json.dumps(json.loads(filters)),
"filters": process_filters_for_prepared_report(filters),
"status": "Queued",
"owner": frappe.session.user,
},
@ -134,7 +144,7 @@ def get_completed_prepared_report(filters, user, report_name):
"Prepared Report",
filters={
"status": "Completed",
"filters": json.dumps(filters),
"filters": process_filters_for_prepared_report(filters),
"owner": user,
"report_name": report_name,
},