From 1e3d833b025d936f7e52113115648aa623de9a94 Mon Sep 17 00:00:00 2001 From: John Bodley <4567245+john-bodley@users.noreply.github.com> Date: Wed, 6 Nov 2019 09:15:58 -0800 Subject: [PATCH] [sip-15] Fixing time range endpoints from dashboards (#8513) --- superset/views/utils.py | 26 +++++++++++++++++++++----- tests/utils_tests.py | 6 +++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/superset/views/utils.py b/superset/views/utils.py index 76255b9f0..1498472ec 100644 --- a/superset/views/utils.py +++ b/superset/views/utils.py @@ -137,7 +137,9 @@ def get_form_data(slice_id=None, use_slice_data=False): update_time_range(form_data) if app.config["SIP_15_ENABLED"]: - form_data["time_range_endpoints"] = get_time_range_endpoints(form_data, slc) + form_data["time_range_endpoints"] = get_time_range_endpoints( + form_data, slc, slice_id + ) return form_data, slc @@ -207,17 +209,23 @@ def apply_display_max_row_limit( def get_time_range_endpoints( - form_data: Dict[str, Any], slc: Optional[models.Slice] + form_data: Dict[str, Any], + slc: Optional[models.Slice] = None, + slice_id: Optional[int] = None, ) -> Optional[Tuple[TimeRangeEndpoint, TimeRangeEndpoint]]: """ Get the slice aware time range endpoints from the form-data falling back to the SQL database specific definition or default if not defined. + Note under certain circumstances the slice object may not exist, however the slice + ID may be defined which serves as a fallback. + When SIP-15 is enabled all slices and will the [start, end) interval. If the grace period is defined and has ended all slices will adhere to the [start, end) interval. :param form_data: The form-data - :param slc: The chart + :param slc: The slice + :param slice_id: The slice ID :returns: The time range endpoints tuple """ @@ -229,14 +237,22 @@ def get_time_range_endpoints( endpoints = form_data.get("time_range_endpoints") - if slc and not endpoints: + if (slc or slice_id) and not endpoints: try: _, datasource_type = get_datasource_info(None, None, form_data) except SupersetException: return None if datasource_type == "table": - endpoints = slc.datasource.database.get_extra().get("time_range_endpoints") + if not slc: + slc = ( + db.session.query(models.Slice).filter_by(id=slice_id).one_or_none() + ) + + if slc: + endpoints = slc.datasource.database.get_extra().get( + "time_range_endpoints" + ) if not endpoints: endpoints = app.config["SIP_15_DEFAULT_TIME_RANGE_ENDPOINTS"] diff --git a/tests/utils_tests.py b/tests/utils_tests.py index d767fe140..81adb93a0 100644 --- a/tests/utils_tests.py +++ b/tests/utils_tests.py @@ -886,19 +886,19 @@ class UtilsTestCase(unittest.TestCase): def test_get_time_range_endpoints(self): self.assertEqual( - get_time_range_endpoints(form_data={}, slc=None), + get_time_range_endpoints(form_data={}), (TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.EXCLUSIVE), ) self.assertEqual( get_time_range_endpoints( - form_data={"time_range_endpoints": ["inclusive", "inclusive"]}, slc=None + form_data={"time_range_endpoints": ["inclusive", "inclusive"]} ), (TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.INCLUSIVE), ) self.assertEqual( - get_time_range_endpoints(form_data={"datasource": "1_druid"}, slc=None), + get_time_range_endpoints(form_data={"datasource": "1_druid"}), (TimeRangeEndpoint.INCLUSIVE, TimeRangeEndpoint.EXCLUSIVE), )