You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2017/09/26 16:03:06 UTC
[incubator-superset] branch master updated: Feature: query string
API endpoint (#3513)
This is an automated email from the ASF dual-hosted git repository.
maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git
The following commit(s) were added to refs/heads/master by this push:
new 7d934e7 Feature: query string API endpoint (#3513)
7d934e7 is described below
commit 7d934e7246dd9a239d1197623a6b4a986f0a69fd
Author: Jeff Niu <je...@gmail.com>
AuthorDate: Tue Sep 26 09:03:03 2017 -0700
Feature: query string API endpoint (#3513)
* exposed API endpoint to get querystring for a slice
* Added unit tests for endpoint
* fixed test case for python3
* moved get querystring logic into its own func
* renamed query string endpoint
---
superset/views/core.py | 41 +++++++++++++++++++++++++++++------------
tests/core_tests.py | 8 ++++++++
tests/utils_tests.py | 33 ++++++++++++++++++++++++++++++++-
3 files changed, 69 insertions(+), 13 deletions(-)
diff --git a/superset/views/core.py b/superset/views/core.py
index 86e5221..40c3741 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -944,6 +944,20 @@ class Superset(BaseSupersetView):
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>/")
@@ -970,18 +984,7 @@ class Superset(BaseSupersetView):
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:
@@ -2324,6 +2327,20 @@ class Superset(BaseSupersetView):
entry='sqllab',
bootstrap_data=json.dumps(d, default=utils.json_iso_dttm_ser)
)
+
+ @api
+ @has_access_api
+ @expose("/slice_query/<slice_id>/")
+ def sliceQuery(self, slice_id):
+ """
+ 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)
diff --git a/tests/core_tests.py b/tests/core_tests.py
index 159d16d..f79dec9 100644
--- a/tests/core_tests.py
+++ b/tests/core_tests.py
@@ -758,6 +758,14 @@ class CoreTests(SupersetTestCase):
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()
diff --git a/tests/utils_tests.py b/tests/utils_tests.py
index e07d959..1e40d92 100644
--- a/tests/utils_tests.py
+++ b/tests/utils_tests.py
@@ -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
@@ -52,3 +61,25 @@ class UtilsTestCase(unittest.TestCase):
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)
--
To stop receiving notification emails like this one, please contact
['"commits@superset.apache.org" <co...@superset.apache.org>'].