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

Fix form data issue switching viz types #5100

Merged
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 @@ -293,12 +293,6 @@ ExploreViewContainer.propTypes = propTypes;

function mapStateToProps({ explore, charts, impressionId }) {
const form_data = getFormDataFromControls(explore.controls);
// fill in additional params stored in form_data but not used by control
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graceguo-supercat added this as part of 9edbd64c5

Object.keys(explore.rawFormData).forEach((key) => {
if (form_data[key] === undefined) {
form_data[key] = explore.rawFormData[key];
}
});
const chartKey = Object.keys(charts)[0];
const chart = charts[chartKey];
return {
Expand Down
7 changes: 7 additions & 0 deletions superset/assets/src/explore/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,13 @@ export const controls = {
description: t('The number of seconds before expiring the cache'),
},

url_params: {
type: 'HiddenControl',
label: t('URL Parameters'),
hidden: true,
description: t('Extra parameters for use in jinja templated queries'),
},

order_by_entity: {
type: 'CheckboxControl',
label: t('Order by entity id'),
Expand Down
7 changes: 0 additions & 7 deletions superset/assets/src/explore/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,6 @@ export function applyDefaultFormData(form_data) {
formData[k] = form_data[k];
}
});
// fill in additional params stored in form_data but not used by control
Object.keys(form_data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing forensics here: @graceguo-supercat added this as part of [Explore] Save custom url parameters when user save slices (#4578)

.forEach((key) => {
if (formData[key] === undefined) {
formData[key] = form_data[key];
}
});
return formData;
}

Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/visTypes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const sections = {
controlSetRows: [
['datasource'],
['viz_type'],
['slice_id', 'cache_timeout'],
['slice_id', 'cache_timeout', 'url_params'],
],
},
colorScheme: {
Expand Down
13 changes: 9 additions & 4 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ def clean_fulfilled_requests(session):
session.commit()
return redirect('/accessrequestsmodelview/list/')

def get_form_data(self, slice_id=None):
def get_form_data(self, slice_id=None, use_slice_data=False):
form_data = {}
post_data = request.form.get('form_data')
request_args_data = request.args.get('form_data')
Expand Down Expand Up @@ -970,7 +970,12 @@ def get_form_data(self, slice_id=None):
slice_id = form_data.get('slice_id') or slice_id
slc = None

if slice_id:
# Check if form data only contains slice_id
contains_only_slc_id = not any(key != 'slice_id' for key in form_data)

# Include the slice_form_data if request from explore or slice calls
# or if form_data only contains slice_id
if slice_id and (use_slice_data or contains_only_slc_id):
slc = db.session.query(models.Slice).filter_by(id=slice_id).first()
slice_form_data = slc.form_data.copy()
# allow form_data in request override slice from_data
Expand Down Expand Up @@ -1010,7 +1015,7 @@ def get_viz(
@has_access
@expose('/slice/<slice_id>/')
def slice(self, slice_id):
form_data, slc = self.get_form_data(slice_id)
form_data, slc = self.get_form_data(slice_id, use_slice_data=True)
endpoint = '/superset/explore/?form_data={}'.format(
parse.quote(json.dumps(form_data)),
)
Expand Down Expand Up @@ -1217,7 +1222,7 @@ def datasource_info(datasource_id, datasource_type, form_data):
@expose('/explore/', methods=['GET', 'POST'])
def explore(self, datasource_type=None, datasource_id=None):
user_id = g.user.get_id() if g.user else None
form_data, slc = self.get_form_data()
form_data, slc = self.get_form_data(use_slice_data=True)

datasource_id, datasource_type = self.datasource_info(
datasource_id, datasource_type, form_data)
Expand Down
50 changes: 20 additions & 30 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ def test_save_slice(self):

form_data = {
'viz_type': 'sankey',
'groupby': 'target',
'groupby': 'source',
'metric': 'sum__value',
'row_limit': 5000,
'slice_id': new_slice_id,
'time_range': 'now',
}
# Setting the name back to its original name by overwriting new slice
self.get_resp(
Expand All @@ -207,6 +208,7 @@ def test_save_slice(self):
)
slc = db.session.query(models.Slice).filter_by(id=new_slice_id).first()
assert slc.slice_name == new_slice_name
assert slc.viz.form_data == form_data
db.session.delete(slc)

def test_filter_endpoint(self):
Expand Down Expand Up @@ -557,7 +559,7 @@ def test_slice_id_is_always_logged_correctly_on_web_request(self):
# superset/explore case
slc = db.session.query(models.Slice).filter_by(slice_name='Girls').one()
qry = db.session.query(models.Log).filter_by(slice_id=slc.id)
self.get_resp(slc.slice_url, {'form_data': json.dumps(slc.viz.form_data)})
self.get_resp(slc.slice_url, {'form_data': json.dumps(slc.form_data)})
self.assertEqual(1, qry.count())

def test_slice_id_is_always_logged_correctly_on_ajax_request(self):
Expand All @@ -566,7 +568,7 @@ def test_slice_id_is_always_logged_correctly_on_ajax_request(self):
slc = db.session.query(models.Slice).filter_by(slice_name='Girls').one()
qry = db.session.query(models.Log).filter_by(slice_id=slc.id)
slc_url = slc.slice_url.replace('explore', 'explore_json')
self.get_json_resp(slc_url, {'form_data': json.dumps(slc.viz.form_data)})
self.get_json_resp(slc_url, {'form_data': json.dumps(slc.form_data)})
self.assertEqual(1, qry.count())

def test_slice_query_endpoint(self):
Expand Down Expand Up @@ -654,46 +656,34 @@ def test_comments_in_sqlatable_query(self):
rendered_query = text_type(table.get_from_clause())
self.assertEqual(clean_query, rendered_query)

def test_slice_url_overrides(self):
# No override
self.login(username='admin')
slice_name = 'Girls'
slc = self.get_slice(slice_name, db.session)
resp = self.get_resp(slc.explore_json_url)
assert '"Jennifer"' in resp

# Overriding groupby
url = slc.get_explore_url(
base_url='/superset/explore_json',
overrides={'groupby': ['state']})
resp = self.get_resp(url)
assert '"CA"' in resp

def test_slice_payload_no_data(self):
self.login(username='admin')
slc = self.get_slice('Girls', db.session)
json_endpoint = '/superset/explore_json/'
form_data = slc.form_data
form_data.update({
'filters': [{'col': 'state', 'op': 'in', 'val': ['N/A']}],
})

url = slc.get_explore_url(
base_url='/superset/explore_json',
overrides={
'filters': [{'col': 'state', 'op': 'in', 'val': ['N/A']}],
},
data = self.get_json_resp(
json_endpoint,
{'form_data': json.dumps(form_data)},
)

data = self.get_json_resp(url)
self.assertEqual(data['status'], utils.QueryStatus.SUCCESS)
self.assertEqual(data['error'], 'No data')

def test_slice_payload_invalid_query(self):
self.login(username='admin')
slc = self.get_slice('Girls', db.session)
form_data = slc.form_data
form_data.update({
'groupby': ['N/A'],
})

url = slc.get_explore_url(
base_url='/superset/explore_json',
overrides={'groupby': ['N/A']},
data = self.get_json_resp(
'/superset/explore_json/',
{'form_data': json.dumps(form_data)},
)

data = self.get_json_resp(url)
self.assertEqual(data['status'], utils.QueryStatus.FAILED)
assert 'KeyError' in data['stacktrace']

Expand Down