[Explore view] Use POST method for charting requests (#3993)

* [Explore view] Use POST method for charting requests

* fix per code review comments

* more code review fixes

* code review fix: remove duplicated calls for getting values from request

* [Explore view] Use POST method for charting requests

* fix per code review comments

* more code review fixes

* code review fix: remove duplicated calls for getting values from request
This commit is contained in:
Grace Guo
2018-02-13 17:21:15 -08:00
committed by GitHub
parent d2d973153f
commit 342180b263
29 changed files with 478 additions and 211 deletions

View File

@@ -19,31 +19,59 @@ export function getAnnotationJsonUrl(slice_id, form_data, isNative) {
}).toString();
}
export function getExploreUrl(form_data, endpointType = 'base', force = false,
curUrl = null, requestParams = {}) {
if (!form_data.datasource) {
export function getURIDirectory(formData, endpointType = 'base') {
// Building the directory part of the URI
let directory = '/superset/explore/';
if (['json', 'csv', 'query'].indexOf(endpointType) >= 0) {
directory = '/superset/explore_json/';
}
const [datasource_id, datasource_type] = formData.datasource.split('__');
directory += `${datasource_type}/${datasource_id}/`;
return directory;
}
export function getExploreLongUrl(formData, endpointType) {
if (!formData.datasource) {
return null;
}
const uri = new URI('/');
const directory = getURIDirectory(formData, endpointType);
const search = uri.search(true);
search.form_data = JSON.stringify(formData);
if (endpointType === 'standalone') {
search.standalone = 'true';
}
return uri.directory(directory).search(search).toString();
}
export function getExploreUrlAndPayload({
formData,
endpointType = 'base',
force = false,
curUrl = null,
requestParams = {},
}) {
if (!formData.datasource) {
return null;
}
// The search params from the window.location are carried through,
// but can be specified with curUrl (used for unit tests to spoof
// the window.location).
let uri = URI(window.location.search);
let uri = new URI([location.protocol, '//', location.host].join(''));
if (curUrl) {
uri = URI(URI(curUrl).search());
}
// Building the directory part of the URI
let directory = '/superset/explore/';
if (['json', 'csv', 'query'].indexOf(endpointType) >= 0) {
directory = '/superset/explore_json/';
}
const [datasource_id, datasource_type] = form_data.datasource.split('__');
directory += `${datasource_type}/${datasource_id}/`;
const directory = getURIDirectory(formData, endpointType);
// Building the querystring (search) part of the URI
const search = uri.search(true);
search.form_data = JSON.stringify(form_data);
if (formData.slice_id) {
search.form_data = JSON.stringify({ slice_id: formData.slice_id });
}
if (force) {
search.force = 'true';
}
@@ -65,5 +93,33 @@ export function getExploreUrl(form_data, endpointType = 'base', force = false,
});
}
uri = uri.search(search).directory(directory);
return uri.toString();
const payload = { ...formData };
return {
url: uri.toString(),
payload,
};
}
export function exportChart(formData, endpointType) {
const { url, payload } = getExploreUrlAndPayload({ formData, endpointType });
const exploreForm = document.createElement('form');
exploreForm.action = url;
exploreForm.method = 'POST';
exploreForm.target = '_blank';
const token = document.createElement('input');
token.type = 'hidden';
token.name = 'csrf_token';
token.value = (document.getElementById('csrf_token') || {}).value;
exploreForm.appendChild(token);
const data = document.createElement('input');
data.type = 'hidden';
data.name = 'form_data';
data.value = JSON.stringify(payload);
exploreForm.appendChild(data);
document.body.appendChild(exploreForm);
exploreForm.submit();
document.body.removeChild(exploreForm);
}