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

Feature: query string API endpoint #3513

Merged
merged 5 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
41 changes: 29 additions & 12 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,20 @@ def slice(self, slice_id):
endpoint += '&standalone=true'
return redirect(endpoint)

def get_query_string_response(self, viz_obj):
try:
query_obj = viz_obj.query_obj()
query = viz_obj.datasource.get_query_str(query_obj)
except Exception as e:
return json_error_response(e)
return Response(
json.dumps({
'query': query,
'language': viz_obj.datasource.query_language,
}),
status=200,
mimetype="application/json")

@log_this
@has_access_api
@expose("/explore_json/<datasource_type>/<datasource_id>/")
Expand All @@ -970,18 +984,7 @@ def explore_json(self, datasource_type, datasource_id):
mimetype="application/csv")

if request.args.get("query") == "true":
try:
query_obj = viz_obj.query_obj()
query = viz_obj.datasource.get_query_str(query_obj)
except Exception as e:
return json_error_response(e)
return Response(
json.dumps({
'query': query,
'language': viz_obj.datasource.query_language,
}),
status=200,
mimetype="application/json")
return self.get_query_string_response(viz_obj)

payload = {}
try:
Expand Down Expand Up @@ -2312,6 +2315,20 @@ def sqllab(self):
entry='sqllab',
bootstrap_data=json.dumps(d, default=utils.json_iso_dttm_ser)
)

@api
@has_access_api
@expose("/slice/<slice_id>/query/")
def sliceQuery(self, slice_id):
Copy link
Member

Choose a reason for hiding this comment

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

NIT: slice_query

"""
This method exposes an API endpoint to
get the database query string for this slice
"""
viz_obj = self.get_viz(slice_id)
if not self.datasource_access(viz_obj.datasource):
return json_error_response(DATASOURCE_ACCESS_ERR, status=401)
return self.get_query_string_response(viz_obj)

appbuilder.add_view_no_menu(Superset)


Expand Down
8 changes: 8 additions & 0 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,14 @@ def test_slice_id_is_always_logged_correctly_on_ajax_request(self):
self.get_json_resp(slc_url)
self.assertEqual(1, qry.count())

def test_slice_query_endpoint(self):
# API endpoint for query string
self.login(username="admin")
slc = self.get_slice("Girls", db.session)
resp = self.get_resp('/superset/slice/{}/query/'.format(slc.id))
assert 'query' in resp
assert 'language' in resp
self.logout();

if __name__ == '__main__':
unittest.main()
33 changes: 32 additions & 1 deletion tests/utils_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from datetime import datetime, date, timedelta, time
from decimal import Decimal
from superset.utils import (
json_int_dttm_ser, json_iso_dttm_ser, base_json_conv, parse_human_timedelta, zlib_compress, zlib_decompress_to_string
json_int_dttm_ser,
json_iso_dttm_ser,
base_json_conv,
parse_human_timedelta,
zlib_compress,
zlib_decompress_to_string,
datetime_f,
JSONEncodedDict,
validate_json,
SupersetException,
)
import unittest
import uuid
Expand Down Expand Up @@ -52,3 +61,25 @@ def test_zlib_compression(self):
got_str = zlib_decompress_to_string(blob)
self.assertEquals(json_str, got_str)

def test_datetime_f(self):
self.assertEquals(datetime_f(datetime(1990, 9, 21, 19, 11, 19, 626096)),
'<nobr>1990-09-21T19:11:19.626096</nobr>')
self.assertEquals(len(datetime_f(datetime.now())), 28)
self.assertEquals(datetime_f(None), '<nobr>None</nobr>')
iso = datetime.now().isoformat()[:10].split('-')
[a, b, c] = [int(v) for v in iso]
self.assertEquals(datetime_f(datetime(a, b, c)), '<nobr>00:00:00</nobr>')

def test_json_encoded_obj(self):
obj = {'a': 5, 'b': ['a', 'g', 5]}
val = '{"a": 5, "b": ["a", "g", 5]}'
jsonObj = JSONEncodedDict()
resp = jsonObj.process_bind_param(obj, 'dialect')
self.assertIn('"a": 5', resp)
self.assertIn('"b": ["a", "g", 5]', resp)
self.assertEquals(jsonObj.process_result_value(val, 'dialect'), obj)

def test_validate_json(self):
invalid = '{"a": 5, "b": [1, 5, ["g", "h]]}'
with self.assertRaises(SupersetException):
validate_json(invalid)