Skip to content

Commit

Permalink
[dashobard fix]: fix validation check for default_filters (#5297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace Guo authored Jun 27, 2018
1 parent 117507c commit 17b4298
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
14 changes: 9 additions & 5 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/dashboard_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 17b4298

Please sign in to comment.