diff --git a/superset/models/core.py b/superset/models/core.py index d350ee461c36b..0ea239612d139 100644 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -342,6 +342,15 @@ def table_names(self): @property def url(self): + if self.json_metadata: + # add default_filters to the preselect_filters of dashboard + json_metadata = json.loads(self.json_metadata) + default_filters = json_metadata.get('default_filters') + # make sure default_filters is not empty + if json.loads(default_filters): + filters = parse.quote(default_filters.encode('utf8')) + return '/superset/dashboard/{}/?preselect_filters={}'.format( + self.slug or self.id, filters) return '/superset/dashboard/{}/'.format(self.slug or self.id) @property diff --git a/superset/views/core.py b/superset/views/core.py index bb9a5c9432c56..9898427c477c1 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -1566,9 +1566,10 @@ def copy_dash(self, dashboard_id): # update chartId of layout entities for value in data['positions'].values(): - if isinstance(value, dict) and value.get('meta') \ - and value.get('meta').get('chartId'): - + if ( + isinstance(value, dict) and value.get('meta') and + value.get('meta').get('chartId') + ): old_id = value.get('meta').get('chartId') new_id = old_to_new_sliceids[old_id] value['meta']['chartId'] = new_id @@ -1594,7 +1595,6 @@ def save_dash(self, dashboard_id): .filter_by(id=dashboard_id).first()) check_ownership(dash, raise_if_false=True) data = json.loads(request.form.get('data')) - original_slice_names = {(slc.id): slc.slice_name for slc in dash.slices} self._set_dash_metadata(dash, data) session.merge(dash) session.commit() @@ -1608,9 +1608,10 @@ def _set_dash_metadata(dashboard, data): slice_ids = [] slice_id_to_name = {} for value in positions.values(): - if isinstance(value, dict) and value.get('meta') \ - and value.get('meta').get('chartId'): - + if ( + isinstance(value, dict) and value.get('meta') and + value.get('meta').get('chartId') + ): slice_id = value.get('meta').get('chartId') slice_ids.append(slice_id) slice_id_to_name[slice_id] = value.get('meta').get('chartName') @@ -1642,7 +1643,7 @@ 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'] - default_filters_data = json.loads(data.get('default_filters', '')) + 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] diff --git a/tests/dashboard_tests.py b/tests/dashboard_tests.py index 3c8ed76e24da4..6b6d656cffa2d 100644 --- a/tests/dashboard_tests.py +++ b/tests/dashboard_tests.py @@ -48,7 +48,12 @@ def test_dashboard_modes(self): .filter_by(slug='births') .first() ) - resp = self.get_resp(dash.url + '?edit=true&standalone=true') + url = dash.url + if dash.url.find('?') == -1: + url += '?' + else: + url += '&' + resp = self.get_resp(url + 'edit=true&standalone=true') self.assertIn('editMode": true', resp) self.assertIn('standalone_mode": true', resp) @@ -56,15 +61,20 @@ def test_save_dash(self, username='admin'): self.login(username=username) dash = db.session.query(models.Dashboard).filter_by( slug='births').first() - positions = [] + positions = {} for i, slc in enumerate(dash.slices): + id = 'DASHBOARD_CHART_TYPE-{}'.format(i) d = { - 'col': 0, - 'row': i * 4, - 'size_x': 4, - 'size_y': 4, - 'slice_id': '{}'.format(slc.id)} - positions.append(d) + 'type': 'DASHBOARD_CHART_TYPE', + 'id': id, + 'children': [], + 'meta': { + 'width': 4, + 'height': 50, + 'chartId': slc.id, + }, + } + positions[id] = d data = { 'css': '', 'expanded_slices': {}, @@ -79,15 +89,20 @@ def test_save_dash_with_filter(self, username='admin'): self.login(username=username) dash = db.session.query(models.Dashboard).filter_by( slug='world_health').first() - positions = [] + positions = {} for i, slc in enumerate(dash.slices): + id = 'DASHBOARD_CHART_TYPE-{}'.format(i) d = { - 'col': 0, - 'row': i * 4, - 'size_x': 4, - 'size_y': 4, - 'slice_id': '{}'.format(slc.id)} - positions.append(d) + 'type': 'DASHBOARD_CHART_TYPE', + 'id': id, + 'children': [], + 'meta': { + 'width': 4, + 'height': 50, + 'chartId': slc.id, + }, + } + positions[id] = d filters = {str(dash.slices[0].id): {'region': ['North America']}} default_filters = json.dumps(filters) @@ -119,15 +134,20 @@ def test_save_dash_with_dashboard_title(self, username='admin'): .first() ) origin_title = dash.dashboard_title - positions = [] + positions = {} for i, slc in enumerate(dash.slices): + id = 'DASHBOARD_CHART_TYPE-{}'.format(i) d = { - 'col': 0, - 'row': i * 4, - 'size_x': 4, - 'size_y': 4, - 'slice_id': '{}'.format(slc.id)} - positions.append(d) + 'type': 'DASHBOARD_CHART_TYPE', + 'id': id, + 'children': [], + 'meta': { + 'width': 4, + 'height': 50, + 'chartId': slc.id, + }, + } + positions[id] = d data = { 'css': '', 'expanded_slices': {}, @@ -150,15 +170,20 @@ def test_copy_dash(self, username='admin'): self.login(username=username) dash = db.session.query(models.Dashboard).filter_by( slug='births').first() - positions = [] + positions = {} for i, slc in enumerate(dash.slices): + id = 'DASHBOARD_CHART_TYPE-{}'.format(i) d = { - 'col': 0, - 'row': i * 4, - 'size_x': 4, - 'size_y': 4, - 'slice_id': '{}'.format(slc.id)} - positions.append(d) + 'type': 'DASHBOARD_CHART_TYPE', + 'id': id, + 'children': [], + 'meta': { + 'width': 4, + 'height': 50, + 'chartId': slc.id, + }, + } + positions[id] = d data = { 'css': '', 'duplicate_slices': False, @@ -213,6 +238,46 @@ def test_add_slices(self, username='admin'): o for o in dash.slices if o.slice_name != 'Mapbox Long/Lat'] db.session.commit() + def test_remove_slices(self, username='admin'): + self.login(username=username) + dash = db.session.query(models.Dashboard).filter_by( + slug='births').first() + positions = {} + origin_slices_length = len(dash.slices) + for i, slc in enumerate(dash.slices): + id = 'DASHBOARD_CHART_TYPE-{}'.format(i) + d = { + 'type': 'DASHBOARD_CHART_TYPE', + 'id': id, + 'children': [], + 'meta': { + 'width': 4, + 'height': 50, + 'chartId': slc.id, + }, + } + # remove last slice + if i < len(dash.slices) - 1: + positions[id] = d + + data = { + 'css': '', + 'expanded_slices': {}, + 'positions': positions, + 'dashboard_title': dash.dashboard_title, + } + + # save dash + dash_id = dash.id + url = '/superset/save_dash/{}/'.format(dash_id) + self.client.post(url, data=dict(data=json.dumps(data))) + dash = db.session.query(models.Dashboard).filter_by( + id=dash_id).first() + + # verify slices data + data = dash.data + self.assertEqual(len(data['slices']), origin_slices_length - 1) + def test_public_user_dashboard_access(self): table = ( db.session