Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get save slice working in v2 #2106

Merged
merged 1 commit into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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
40 changes: 14 additions & 26 deletions superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,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 @@ -1632,36 +1636,24 @@ 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']
form_data = self.get_form_data()

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')

if action in ('saveas'):
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 @@ -1700,13 +1692,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