Skip to content

Commit

Permalink
Show scheduled queries (#7545)
Browse files Browse the repository at this point in the history
* Show scheduled queries

* Remove column

* Secure views

* Add import

* Fix unit tests

* Reuse existing db connection from view

* Remove unnecessary import
  • Loading branch information
betodealmeida authored May 18, 2019
1 parent f0f719c commit dcafabd
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
43 changes: 43 additions & 0 deletions superset/assets/src/showSavedQuery/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import ReactDom from 'react-dom';
import Form from 'react-jsonschema-form';

const scheduleInfoContainer = document.getElementById('schedule-info');
const bootstrapData = JSON.parse(scheduleInfoContainer.getAttribute('data-bootstrap'));
const schemas = bootstrapData.common.feature_flags.SCHEDULED_QUERIES;
const scheduleInfo = bootstrapData.common.extra_json.schedule_info;

if (scheduleInfo && schemas) {
// hide instructions when showing schedule info
schemas.JSONSCHEMA.description = '';

ReactDom.render(
<Form
schema={schemas.JSONSCHEMA}
uiSchema={schemas.UISCHEMA}
formData={scheduleInfo}
disabled
>
<br />
</Form>,
scheduleInfoContainer,
);
}
1 change: 1 addition & 0 deletions superset/assets/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const config = {
sqllab: addPreamble('/src/SqlLab/index.jsx'),
welcome: addPreamble('/src/welcome/index.jsx'),
profile: addPreamble('/src/profile/index.jsx'),
showSavedQuery: [path.join(APP_DIR, '/src/showSavedQuery/index.jsx')],
},
output,
optimization: {
Expand Down
35 changes: 35 additions & 0 deletions superset/templates/superset/models/savedquery/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{#
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
#}
{% extends "appbuilder/general/model/show.html" %}

{% block show_form %}
{{ super() }}
<div
id="schedule-info"
style="padding: 0 10px;"
data-bootstrap="{{ bootstrap_data }}"
></div>
{% endblock %}

{% block tail_js %}
{{ super() }}
{% with filename="showSavedQuery" %}
{% include "superset/partials/_script_tag.html" %}
{% endwith %}
{% endblock %}
2 changes: 1 addition & 1 deletion superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def json_response(self, obj, status=200):
status=status,
mimetype='application/json')

def common_bootsrap_payload(self):
def common_bootstrap_payload(self):
"""Common data always sent to the client"""
messages = get_flashed_messages(with_categories=True)
locale = str(get_locale())
Expand Down
10 changes: 5 additions & 5 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ def explore(self, datasource_type=None, datasource_id=None):
'standalone': standalone,
'user_id': user_id,
'forced_height': request.args.get('height'),
'common': self.common_bootsrap_payload(),
'common': self.common_bootstrap_payload(),
}
table_name = datasource.table_name \
if datasource_type == 'table' \
Expand Down Expand Up @@ -2231,7 +2231,7 @@ def dashboard(**kwargs): # noqa
'user_id': g.user.get_id(),
'dashboard_data': dashboard_data,
'datasources': {ds.uid: ds.data for ds in datasources},
'common': self.common_bootsrap_payload(),
'common': self.common_bootstrap_payload(),
'editMode': edit_mode,
}

Expand Down Expand Up @@ -2919,7 +2919,7 @@ def welcome(self):

payload = {
'user': bootstrap_user_data(),
'common': self.common_bootsrap_payload(),
'common': self.common_bootstrap_payload(),
}

return self.render_template(
Expand All @@ -2938,7 +2938,7 @@ def profile(self, username):

payload = {
'user': bootstrap_user_data(username, include_perms=True),
'common': self.common_bootsrap_payload(),
'common': self.common_bootstrap_payload(),
}

return self.render_template(
Expand All @@ -2954,7 +2954,7 @@ def sqllab(self):
"""SQL Editor"""
d = {
'defaultDbId': config.get('SQLLAB_DEFAULT_DBID'),
'common': self.common_bootsrap_payload(),
'common': self.common_bootstrap_payload(),
}
return self.render_template(
'superset/basic.html',
Expand Down
35 changes: 33 additions & 2 deletions superset/views/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
from flask import g, redirect
from flask_appbuilder import expose
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.security.decorators import has_access
from flask_appbuilder.security.decorators import has_access, has_access_api
from flask_babel import gettext as __
from flask_babel import lazy_gettext as _
from flask_sqlalchemy import BaseQuery
import simplejson as json

from superset import appbuilder, security_manager
from superset import appbuilder, get_feature_flags, security_manager
from superset.models.sql_lab import Query, SavedQuery
from superset.utils import core as utils
from .base import BaseSupersetView, DeleteMixin, SupersetFilter, SupersetModelView


Expand Down Expand Up @@ -107,12 +109,36 @@ class SavedQueryView(SupersetModelView, DeleteMixin):
'changed_on': _('Changed on'),
}

show_template = 'superset/models/savedquery/show.html'

def pre_add(self, obj):
obj.user = g.user

def pre_update(self, obj):
self.pre_add(obj)

@has_access
@expose('show/<pk>')
def show(self, pk):
pk = self._deserialize_pk_if_composite(pk)
widgets = self._show(pk)
extra_json = self.datamodel.get(pk).extra_json
payload = {
'common': {
'feature_flags': get_feature_flags(),
'extra_json': json.loads(extra_json),
},
}

return self.render_template(
self.show_template,
pk=pk,
title=self.show_title,
widgets=widgets,
related_views=self._related_views,
bootstrap_data=json.dumps(payload, default=utils.json_iso_dttm_ser),
)


class SavedQueryViewApi(SavedQueryView):
list_columns = [
Expand All @@ -123,6 +149,11 @@ class SavedQueryViewApi(SavedQueryView):
add_columns = show_columns
edit_columns = add_columns

@has_access_api
@expose('show/<pk>')
def show(self, pk):
return super().show(pk)


appbuilder.add_view_no_menu(SavedQueryViewApi)
appbuilder.add_view_no_menu(SavedQueryView)
Expand Down

0 comments on commit dcafabd

Please sign in to comment.