graceguo-supercat closed pull request #5297: [dashboard fix]: Fix
default_filters validation
URL: https://github.com/apache/incubator-superset/pull/5297
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/superset/views/core.py b/superset/views/core.py
index be951a11db..7c39f1c6d6 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1637,7 +1637,11 @@ def _set_dash_metadata(dashboard, data):
if 'filter_immune_slice_fields' not in md:
md['filter_immune_slice_fields'] = {}
md['expanded_slices'] = data['expanded_slices']
- md['default_filters'] = data.get('default_filters', '')
+ default_filters_data = json.loads(data.get('default_filters',
'{}'))
+ applicable_filters =\
+ {key: v for key, v in default_filters_data.items()
+ if int(key) in slice_ids}
+ md['default_filters'] = json.dumps(applicable_filters)
dashboard.json_metadata = json.dumps(md, indent=4)
return
@@ -1681,10 +1685,10 @@ def _set_dash_metadata(dashboard, data):
md['filter_immune_slice_fields'] = {}
md['expanded_slices'] = data['expanded_slices']
default_filters_data = json.loads(data.get('default_filters', '{}'))
- for key in default_filters_data.keys():
- if int(key) not in slice_ids:
- del default_filters_data[key]
- md['default_filters'] = json.dumps(default_filters_data)
+ applicable_filters = \
+ {key: v for key, v in default_filters_data.items()
+ if int(key) in slice_ids}
+ md['default_filters'] = json.dumps(applicable_filters)
dashboard.json_metadata = json.dumps(md, indent=4)
@api
diff --git a/tests/dashboard_tests.py b/tests/dashboard_tests.py
index 671c07de82..6cc5753689 100644
--- a/tests/dashboard_tests.py
+++ b/tests/dashboard_tests.py
@@ -98,6 +98,31 @@ def test_save_dash_with_filter(self, username='admin'):
resp = self.get_resp(new_url)
self.assertIn('North America', resp)
+ def test_save_dash_with_invalid_filters(self, username='admin'):
+ self.login(username=username)
+ dash = db.session.query(models.Dashboard).filter_by(
+ slug='world_health').first()
+
+ # add an invalid filter slice
+ filters = {str(99999): {'region': ['North America']}}
+ default_filters = json.dumps(filters)
+ data = {
+ 'css': '',
+ 'expanded_slices': {},
+ 'positions': dash.position_array,
+ 'dashboard_title': dash.dashboard_title,
+ 'default_filters': default_filters,
+ }
+
+ url = '/superset/save_dash/{}/'.format(dash.id)
+ resp = self.get_resp(url, data=dict(data=json.dumps(data)))
+ self.assertIn('SUCCESS', resp)
+
+ updatedDash = db.session.query(models.Dashboard).filter_by(
+ slug='world_health').first()
+ new_url = updatedDash.url
+ self.assertNotIn('region', new_url)
+
def test_save_dash_with_dashboard_title(self, username='admin'):
self.login(username=username)
dash = (
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]