Skip to content

Commit

Permalink
Get save slice working in v2 (#2106)
Browse files Browse the repository at this point in the history
  • Loading branch information
vera-liu authored and mistercrunch committed Feb 11, 2017
1 parent cf472f7 commit b24e522
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ class ExploreViewContainer extends React.Component {
onHide={this.toggleModal.bind(this)}
actions={this.props.actions}
form_data={this.props.form_data}
datasource_type={this.props.datasource_type}
/>
}
<div className="row">
Expand Down
28 changes: 17 additions & 11 deletions superset/assets/javascripts/explorev2/components/SaveModal.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint camel-case: 0 */
/* eslint camelcase: 0 */
import React, { PropTypes } from 'react';
import $ from 'jquery';
import { Modal, Alert, Button, Radio } from 'react-bootstrap';
import Select from 'react-select';
import { connect } from 'react-redux';
import { getParamObject } from '../exploreUtils';

const propTypes = {
can_edit: PropTypes.bool,
onHide: PropTypes.func.isRequired,
actions: PropTypes.object.isRequired,
form_data: PropTypes.object,
datasource_type: PropTypes.string.isRequired,
user_id: PropTypes.string.isRequired,
dashboards: PropTypes.array.isRequired,
alert: PropTypes.string,
slice: PropTypes.object,
datasource: PropTypes.object,
};

class SaveModal extends React.Component {
Expand Down Expand Up @@ -58,13 +58,13 @@ class SaveModal extends React.Component {
saveOrOverwrite(gotodash) {
this.setState({ alert: null });
this.props.actions.removeSaveModalAlert();
const params = getParamObject(
this.props.form_data, this.props.datasource_type, this.state.action === 'saveas');
const sliceParams = {};
params.datasource_name = this.props.form_data.datasource_name;

let sliceName = null;
sliceParams.action = this.state.action;
if (this.props.slice.slice_id) {
sliceParams.slice_id = this.props.slice.slice_id;
}
if (sliceParams.action === 'saveas') {
sliceName = this.state.newSliceName;
if (sliceName === '') {
Expand All @@ -73,7 +73,7 @@ class SaveModal extends React.Component {
}
sliceParams.slice_name = sliceName;
} else {
sliceParams.slice_name = this.props.form_data.slice_name;
sliceParams.slice_name = this.props.slice.slice_name;
}

const addToDash = this.state.addToDash;
Expand All @@ -100,9 +100,13 @@ class SaveModal extends React.Component {
dashboard = null;
}
sliceParams.goto_dash = gotodash;
const baseUrl = '/superset/explore/' +
`${this.props.datasource_type}/${this.props.form_data.datasource}/`;
const saveUrl = `${baseUrl}?${$.param(params, true)}&${$.param(sliceParams, true)}`;

const baseUrl = `/superset/explore/${this.props.datasource.type}/${this.props.datasource.id}/`;
sliceParams.datasource_name = this.props.datasource.name;

const saveUrl = `${baseUrl}?form_data=` +
`${encodeURIComponent(JSON.stringify(this.props.form_data))}` +
`&${$.param(sliceParams, true)}`;
this.props.actions.saveSlice(saveUrl);
this.props.onHide();
}
Expand Down Expand Up @@ -140,7 +144,7 @@ class SaveModal extends React.Component {
checked={this.state.action === 'overwrite'}
onChange={this.changeAction.bind(this, 'overwrite')}
>
{`Overwrite slice ${this.props.form_data.slice_name}`}
{`Overwrite slice ${this.props.slice.slice_name}`}
</Radio>

<Radio
Expand Down Expand Up @@ -223,6 +227,8 @@ SaveModal.propTypes = propTypes;

function mapStateToProps(state) {
return {
datasource: state.datasource,
slice: state.slice,
can_edit: state.can_edit,
user_id: state.user_id,
dashboards: state.dashboards,
Expand Down
15 changes: 0 additions & 15 deletions superset/assets/javascripts/explorev2/exploreUtils.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
/* eslint camelcase: 0 */
export function getParamObject(form_data, datasource_type, saveNewSlice) {
const data = {
datasource_id: form_data.datasource,
datasource_type,
};
Object.keys(form_data).forEach((field) => {
// filter out null fields
if (form_data[field] !== null && field !== 'datasource'
&& !(saveNewSlice && field === 'slice_name')) {
data[field] = form_data[field];
}
});
return data;
}

export function getExploreUrl(form_data, dummy, endpoint = 'base') {
const [datasource_id, datasource_type] = form_data.datasource.split('__');
let params = `${datasource_type}/${datasource_id}/`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ const defaultProps = {
saveSlice: sinon.spy(),
},
form_data: defaultFormData,
datasource_id: 1,
datasource_name: 'birth_names',
datasource_type: 'table',
user_id: 1,
slice: {},
};

describe('SaveModal', () => {
Expand Down
44 changes: 14 additions & 30 deletions superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,11 @@ def explore(self, datasource_type, datasource_id):
action = request.args.get('action')
if action in ('saveas', 'overwrite'):
return self.save_or_overwrite_slice(
request.args, slc, slice_add_perm, slice_edit_perm)
request.args,
slc, slice_add_perm,
slice_edit_perm,
datasource_id,
datasource_type)

form_data['datasource'] = str(datasource_id) + '__' + datasource_type
standalone = request.args.get("standalone") == "true"
Expand Down Expand Up @@ -1665,37 +1669,21 @@ def filter(self, datasource_type, datasource_id, column):
mimetype="application/json")

def save_or_overwrite_slice(
self, args, slc, slice_add_perm, slice_edit_perm):
self, args, slc, slice_add_perm, slice_edit_perm,
datasource_id, datasource_type):
"""Save or overwrite a slice"""
slice_name = args.get('slice_name')
action = args.get('action')

# TODO use form processing form wtforms
d = args.to_dict(flat=False)
del d['action']
if 'previous_viz_type' in d:
del d['previous_viz_type']

as_list = ('metrics', 'groupby', 'columns', 'all_columns',
'mapbox_label', 'order_by_cols')
for k in d:
v = d.get(k)
if k in as_list and not isinstance(v, list):
d[k] = [v] if v else []
if k not in as_list and isinstance(v, list):
d[k] = v[0]

datasource_type = args.get('datasource_type')
datasource_id = args.get('datasource_id')
form_data = self.get_form_data()

if action in ('saveas'):
if 'slice_id' in d:
d.pop('slice_id') # don't save old slice_id
if 'slice_id' in form_data:
form_data.pop('slice_id') # don't save old slice_id
slc = models.Slice(owners=[g.user] if g.user else [])

slc.params = json.dumps(d, indent=4, sort_keys=True)
slc.params = json.dumps(form_data)
slc.datasource_name = args.get('datasource_name')
slc.viz_type = args.get('viz_type')
slc.viz_type = form_data['viz_type']
slc.datasource_type = datasource_type
slc.datasource_id = datasource_id
slc.slice_name = slice_name
Expand Down Expand Up @@ -1734,13 +1722,9 @@ def save_or_overwrite_slice(
db.session.commit()

if request.args.get('goto_dash') == 'true':
if request.args.get('V2') == 'true':
return dash.url
return redirect(dash.url)
return dash.url
else:
if request.args.get('V2') == 'true':
return slc.slice_url
return redirect(slc.slice_url)
return slc.slice_url

def save_slice(self, slc):
session = db.session()
Expand Down

0 comments on commit b24e522

Please sign in to comment.