Beispiel #1
0
 def query(self):
     """
     Takes a query_obj constructed in the client and returns payload data response
     for the given query_obj.
     params: query_context: json_blob
     """
     query_context = QueryContext(**json.loads(request.form.get("query_context")))
     security_manager.assert_query_context_permission(query_context)
     payload_json = query_context.get_payload()
     return json.dumps(
         payload_json, default=utils.json_int_dttm_ser, ignore_nan=True
     )
Beispiel #2
0
    def query(self) -> FlaskResponse:
        """
        Takes a query_obj constructed in the client and returns payload data response
        for the given query_obj.

        raises SupersetSecurityException: If the user cannot access the resource
        """
        query_context = QueryContext(**json.loads(request.form["query_context"]))
        query_context.raise_for_access()
        result = query_context.get_payload()
        payload_json = result["queries"]
        return json.dumps(
            payload_json, default=utils.json_int_dttm_ser, ignore_nan=True
        )
 def test_query_response_type(self):
     """
     Ensure that query result type works
     """
     self.login(username="******")
     table_name = "birth_names"
     table = self.get_table_by_name(table_name)
     payload = get_query_context(table.name, table.id, table.type)
     payload["result_type"] = ChartDataResultType.QUERY.value
     query_context = QueryContext(**payload)
     responses = query_context.get_payload()
     self.assertEqual(len(responses), 1)
     response = responses[0]
     self.assertEqual(len(response), 2)
     self.assertEqual(response["language"], "sql")
     self.assertIn("SELECT", response["query"])
 def test_csv_response_format(self):
     """
     Ensure that CSV result format works
     """
     self.login(username="******")
     table_name = "birth_names"
     table = self.get_table_by_name(table_name)
     payload = get_query_context(table.name, table.id, table.type)
     payload["result_format"] = ChartDataResultFormat.CSV.value
     payload["queries"][0]["row_limit"] = 10
     query_context = QueryContext(**payload)
     responses = query_context.get_payload()
     self.assertEqual(len(responses), 1)
     data = responses[0]["data"]
     self.assertIn("name,sum__num\n", data)
     self.assertEqual(len(data.split("\n")), 12)
 def test_samples_response_type(self):
     """
     Ensure that samples result type works
     """
     self.login(username="******")
     table_name = "birth_names"
     table = self.get_table_by_name(table_name)
     payload = get_query_context(table.name, table.id, table.type)
     payload["result_type"] = ChartDataResultType.SAMPLES.value
     payload["queries"][0]["row_limit"] = 5
     query_context = QueryContext(**payload)
     responses = query_context.get_payload()
     self.assertEqual(len(responses), 1)
     data = responses[0]["data"]
     self.assertIsInstance(data, list)
     self.assertEqual(len(data), 5)
     self.assertNotIn("sum__num", data[0])
Beispiel #6
0
 def data(self) -> Response:
     """
     Takes a query context constructed in the client and returns payload
     data response for the given query.
     ---
     post:
       description: >-
         Takes a query context constructed in the client and returns payload data
         response for the given query.
       requestBody:
         description: Query context schema
         required: true
         content:
           application/json:
             schema:
               type: object
               properties:
                 datasource:
                   type: object
                   description: The datasource where the query will run
                   properties:
                     id:
                       type: integer
                     type:
                       type: string
                 queries:
                   type: array
                   items:
                     type: object
                     properties:
                       granularity:
                         type: string
                       groupby:
                         type: array
                         items:
                           type: string
                       metrics:
                         type: array
                         items:
                           type: object
                       filters:
                         type: array
                         items:
                           type: string
                       row_limit:
                         type: integer
       responses:
         200:
           description: Query result
           content:
             application/json:
               schema:
                 type: array
                 items:
                   type: object
                   properties:
                     cache_key:
                       type: string
                     cached_dttm:
                       type: string
                     cache_timeout:
                       type: integer
                     error:
                       type: string
                     is_cached:
                       type: boolean
                     query:
                       type: string
                     status:
                       type: string
                     stacktrace:
                       type: string
                     rowcount:
                       type: integer
                     data:
                       type: array
                       items:
                         type: object
         400:
           $ref: '#/components/responses/400'
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         500:
           $ref: '#/components/responses/500'
     """
     if not request.is_json:
         return self.response_400(message="Request is not JSON")
     try:
         query_context = QueryContext(**request.json)
     except KeyError:
         return self.response_400(message="Request is incorrect")
     try:
         security_manager.assert_query_context_permission(query_context)
     except SupersetSecurityException:
         return self.response_401()
     payload_json = query_context.get_payload()
     response_data = simplejson.dumps(payload_json,
                                      default=json_int_dttm_ser,
                                      ignore_nan=True)
     resp = make_response(response_data, 200)
     resp.headers["Content-Type"] = "application/json; charset=utf-8"
     return resp