diff --git a/superset/utils/core.py b/superset/utils/core.py index 3d40b75928c3d..cf90963cd17cc 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -1039,7 +1039,8 @@ def merge_extra_form_data(form_data: Dict[str, Any]) -> None: "druid_time_origin": "__time_origin", "granularity": "__granularity", } - applied_time_extras = form_data["applied_time_extras"] + applied_time_extras = form_data.get("applied_time_extras", {}) + form_data["applied_time_extras"] = applied_time_extras extra_form_data = form_data.pop("extra_form_data", {}) append_form_data = extra_form_data.pop("append_form_data", {}) append_filters = append_form_data.get("filters", None) diff --git a/tests/utils_tests.py b/tests/utils_tests.py index ab2f6ea33d9f6..571590d579798 100644 --- a/tests/utils_tests.py +++ b/tests/utils_tests.py @@ -57,6 +57,7 @@ JSONEncodedDict, memoized, merge_extra_filters, + merge_extra_form_data, merge_request_params, parse_ssl_cert, parse_js_uri_path_item, @@ -902,6 +903,35 @@ def test_build_extra_filters(self): layout, filter_scopes, default_filters, box_plot.id ) == [{"col": "region", "op": "==", "val": "North America"}] + def test_merge_extra_filters_with_no_extras(self): + form_data = { + "time_range": "Last 10 days", + } + merge_extra_form_data(form_data) + self.assertEqual( + form_data, + { + "time_range": "Last 10 days", + "applied_time_extras": {}, + "adhoc_filters": [], + }, + ) + + def test_merge_extra_filters_with_extras(self): + form_data = { + "time_range": "Last 10 days", + "extra_form_data": { + "append_form_data": { + "filters": [{"col": "foo", "op": "IN", "val": "bar"}] + }, + "override_form_data": {"time_range": "Last 100 years",}, + }, + } + merge_extra_form_data(form_data) + assert form_data["applied_time_extras"] == {"__time_range": "Last 100 years"} + assert form_data["time_range"] == "Last 100 years" + assert len(form_data["adhoc_filters"]) == 1 + def test_ssl_certificate_parse(self): parsed_certificate = parse_ssl_cert(ssl_certificate) self.assertEqual(parsed_certificate.serial_number, 12355228710836649848)