Skip to content

Commit

Permalink
SQL Lab page needs to take the post form data, too
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmud committed Mar 22, 2020
1 parent c5222b8 commit cefedf6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
5 changes: 3 additions & 2 deletions superset-frontend/src/SqlLab/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ setupApp();

const appContainer = document.getElementById('app');
const bootstrapData = JSON.parse(appContainer.getAttribute('data-bootstrap'));

initFeatureFlags(bootstrapData.common.feature_flags);

const initialState = getInitialState(bootstrapData);
const sqlLabPersistStateConfig = {
paths: ['sqlLab'],
Expand All @@ -59,7 +61,6 @@ const sqlLabPersistStateConfig = {
// it caused configurations passed from server-side got override.
// see PR 6257 for details
delete state[path].common; // eslint-disable-line no-param-reassign

if (path === 'sqlLab') {
subset[path] = {
...state[path],
Expand Down Expand Up @@ -108,7 +109,7 @@ if (sqlLabMenu) {

const Application = () => (
<Provider store={store}>
<App />
<App formData={bootstrapData.form_data} />
</Provider>
);

Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/SqlLab/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class App extends React.PureComponent {
content = (
<>
<QueryAutoRefresh />
<TabbedSqlEditors />
<TabbedSqlEditors formData={this.props.formData} />
</>
);
}
Expand Down
7 changes: 6 additions & 1 deletion superset-frontend/src/SqlLab/components/TabbedSqlEditors.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ class TabbedSqlEditors extends React.PureComponent {
});
}

const query = URI(window.location).search(true);
// merge post form data with GET search params
const query = {
...this.props.formData,
...URI(window.location).search(true),
};

// Popping a new tab based on the querystring
if (query.id || query.sql || query.savedQueryId || query.datasourceKey) {
if (query.id) {
Expand Down
31 changes: 19 additions & 12 deletions superset-frontend/src/SqlLab/reducers/getInitialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
import { t } from '@superset-ui/translation';
import getToastsFromPyFlashMessages from '../../messageToasts/utils/getToastsFromPyFlashMessages';

export default function getInitialState({ defaultDbId, ...restBootstrapData }) {
/*
export default function getInitialState({
defaultDbId,
common,
active_tab: activeTab,
tab_state_ids: tabStateIds = [],
databases,
queries: queries_,
}) {
/**

This comment has been minimized.

Copy link
@ktmud

ktmud Mar 22, 2020

Author Member

Can ignore changes to this file. It's just refactoring for formatting.

* Before YYYY-MM-DD, the state for SQL Lab was stored exclusively in the
* browser's localStorage. The feature flag `SQLLAB_BACKEND_PERSISTENCE`
* moves the state to the backend instead, migrating it from local storage.
Expand All @@ -39,7 +46,7 @@ export default function getInitialState({ defaultDbId, ...restBootstrapData }) {
autorun: false,
templateParams: null,
dbId: defaultDbId,
queryLimit: restBootstrapData.common.conf.DEFAULT_SQLLAB_LIMIT,
queryLimit: common.conf.DEFAULT_SQLLAB_LIMIT,
validationResult: {
id: null,
errors: [],
Expand All @@ -52,11 +59,11 @@ export default function getInitialState({ defaultDbId, ...restBootstrapData }) {
},
};

/* Load state from the backend. This will be empty if the feature flag
/**
* Load state from the backend. This will be empty if the feature flag
* `SQLLAB_BACKEND_PERSISTENCE` is off.
*/
const activeTab = restBootstrapData.active_tab;
restBootstrapData.tab_state_ids.forEach(({ id, label }) => {
tabStateIds.forEach(({ id, label }) => {
let queryEditor;
if (activeTab && activeTab.id === id) {
queryEditor = {
Expand Down Expand Up @@ -92,7 +99,6 @@ export default function getInitialState({ defaultDbId, ...restBootstrapData }) {
});

const tabHistory = activeTab ? [activeTab.id.toString()] : [];

const tables = [];
if (activeTab) {
activeTab.table_schemas
Expand Down Expand Up @@ -126,9 +132,10 @@ export default function getInitialState({ defaultDbId, ...restBootstrapData }) {
});
}

const { databases, queries } = restBootstrapData;
const queries = { ...queries_ };

/* If the `SQLLAB_BACKEND_PERSISTENCE` feature flag is off, or if the user
/**
* If the `SQLLAB_BACKEND_PERSISTENCE` feature flag is off, or if the user
* hasn't used SQL Lab after it has been turned on, the state will be stored
* in the browser's local storage.
*/
Expand Down Expand Up @@ -174,12 +181,12 @@ export default function getInitialState({ defaultDbId, ...restBootstrapData }) {
queriesLastUpdate: Date.now(),
},
messageToasts: getToastsFromPyFlashMessages(
(restBootstrapData.common || {}).flash_messages || [],
(common || {}).flash_messages || [],
),
localStorageUsageInKilobytes: 0,
common: {
flash_messages: restBootstrapData.common.flash_messages,
conf: restBootstrapData.common.conf,
flash_messages: common.flash_messages,
conf: common.conf,
},
};
}
7 changes: 6 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2756,8 +2756,13 @@ def _get_sqllab_payload(user_id: int) -> Dict[str, Any]:
def sqllab(self):
"""SQL Editor"""
payload = self._get_sqllab_payload(g.user.get_id())
form_data = request.form.get('form_data')
try:
payload['form_data'] = json.loads(form_data)
except:
pass
bootstrap_data = json.dumps(
payload, default=utils.pessimistic_json_iso_dttm_ser
payload, default=utils.pessimistic_json_iso_dttm_ser,
)

return self.render_template(
Expand Down

0 comments on commit cefedf6

Please sign in to comment.