Skip to content

Commit

Permalink
Merge pull request apache#20 from graceguo-supercat/gg-CherryPickFixU…
Browse files Browse the repository at this point in the history
…RLParams

Cherry Pick: [Explore] Save custom url parameters when user save slices (apache#4578)
  • Loading branch information
Grace Guo authored Mar 8, 2018
2 parents fb08fa3 + 04dff7a commit 53ea8f3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ 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
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
2 changes: 2 additions & 0 deletions superset/assets/javascripts/explore/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ initJQueryAjax();
const exploreViewContainer = document.getElementById('app');
const bootstrapData = JSON.parse(exploreViewContainer.getAttribute('data-bootstrap'));
const controls = getControlsState(bootstrapData, bootstrapData.form_data);
const rawFormData = { ...bootstrapData.form_data };
delete bootstrapData.form_data;
delete bootstrapData.common.locale;
delete bootstrapData.common.language_pack;

// Initial state
const bootstrappedState = Object.assign(
bootstrapData, {
rawFormData,
controls,
filterColumnOpts: [],
isDatasourceMetaLoading: false,
Expand Down
7 changes: 7 additions & 0 deletions superset/assets/javascripts/explore/stores/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ 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)
.forEach((key) => {
if (formData[key] === undefined) {
formData[key] = form_data[key];
}
});
return formData;
}

Expand Down
15 changes: 11 additions & 4 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from datetime import datetime, timedelta
import inspect
import json
import random
import time
import uuid
Expand All @@ -30,15 +31,21 @@


def url_param(param, default=None):
"""Get a url paramater
"""Get a url or post data parameter
:param param: the url parameter to lookup
:param param: the parameter to lookup
:type param: str
:param default: the value to return in the absence of the parameter
:type default: str
"""
print(request.args)
return request.args.get(param, default)
if request.args.get(param):
return request.args.get(param, default)
# Supporting POST as well as get
if request.form.get('form_data'):
form_data = json.loads(request.form.get('form_data'))
url_params = form_data['url_params'] or {}
return url_params.get(param, default)
return default


def current_user_id():
Expand Down
9 changes: 9 additions & 0 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,15 @@ def get_filter_key(f):
del form_data['extra_filters']


def merge_request_params(form_data, params):
url_params = {}
for key, value in params.items():
if key in ('form_data', 'r'):
continue
url_params[key] = value
form_data['url_params'] = url_params


def get_update_perms_flag():
val = os.environ.get('SUPERSET_UPDATE_PERMS')
return val.lower() not in ('0', 'false', 'no') if val else True
8 changes: 7 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import superset.models.core as models
from superset.models.sql_lab import Query
from superset.sql_parse import SupersetQuery
from superset.utils import has_access, merge_extra_filters, QueryStatus
from superset.utils import (
has_access, merge_extra_filters, merge_request_params, QueryStatus,
)
from .base import (
api, BaseSupersetView, CsvResponse, DeleteMixin,
generate_download_headers, get_error_msg, get_user_roles,
Expand Down Expand Up @@ -1255,6 +1257,10 @@ def explore(self, datasource_type=None, datasource_id=None):
# On explore, merge extra filters into the form data
merge_extra_filters(form_data)

# merge request url params
if request.method == 'GET':
merge_request_params(form_data, request.args)

# handle save or overwrite
action = request.args.get('action')

Expand Down
17 changes: 16 additions & 1 deletion tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from superset.utils import (
base_json_conv, datetime_f, json_int_dttm_ser, json_iso_dttm_ser,
JSONEncodedDict, memoized, merge_extra_filters, parse_human_timedelta,
JSONEncodedDict, memoized, merge_extra_filters, merge_request_params,
parse_human_timedelta,
SupersetException, validate_json, zlib_compress, zlib_decompress_to_string,
)

Expand Down Expand Up @@ -216,6 +217,20 @@ def test_merge_extra_filters_adds_unequal_lists(self):
merge_extra_filters(form_data)
self.assertEquals(form_data, expected)

def test_merge_request_params(self):
form_data = {
'since': '2000',
'until': 'now',
}
url_params = {
'form_data': form_data,
'dashboard_ids': '(1,2,3,4,5)',
}
merge_request_params(form_data, url_params)
self.assertIn('url_params', form_data.keys())
self.assertIn('dashboard_ids', form_data['url_params'])
self.assertNotIn('form_data', form_data.keys())

def test_datetime_f(self):
self.assertEquals(
datetime_f(datetime(1990, 9, 21, 19, 11, 19, 626096)),
Expand Down

0 comments on commit 53ea8f3

Please sign in to comment.