mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
chore: Migrate /superset/csv/<client_id> to API v1 (#22913)
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
# under the License.
|
||||
import logging
|
||||
from typing import Any, cast, Dict, Optional
|
||||
from urllib import parse
|
||||
|
||||
import simplejson as json
|
||||
from flask import request
|
||||
@@ -32,6 +33,7 @@ from superset.queries.dao import QueryDAO
|
||||
from superset.sql_lab import get_sql_results
|
||||
from superset.sqllab.command_status import SqlJsonExecutionStatus
|
||||
from superset.sqllab.commands.execute import CommandResult, ExecuteSqlCommand
|
||||
from superset.sqllab.commands.export import SqlResultExportCommand
|
||||
from superset.sqllab.commands.results import SqlExecutionResultsCommand
|
||||
from superset.sqllab.exceptions import (
|
||||
QueryIsForbiddenToAccessException,
|
||||
@@ -53,7 +55,7 @@ from superset.sqllab.sqllab_execution_context import SqlJsonExecutionContext
|
||||
from superset.sqllab.validators import CanAccessQueryValidatorImpl
|
||||
from superset.superset_typing import FlaskResponse
|
||||
from superset.utils import core as utils
|
||||
from superset.views.base import json_success
|
||||
from superset.views.base import CsvResponse, generate_download_headers, json_success
|
||||
from superset.views.base_api import BaseSupersetApi, requires_json, statsd_metrics
|
||||
|
||||
config = app.config
|
||||
@@ -79,6 +81,69 @@ class SqlLabRestApi(BaseSupersetApi):
|
||||
QueryExecutionResponseSchema,
|
||||
)
|
||||
|
||||
@expose("/export/<string:client_id>/")
|
||||
@protect()
|
||||
@statsd_metrics
|
||||
@event_logger.log_this_with_context(
|
||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
|
||||
f".export_csv",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
def export_csv(self, client_id: str) -> CsvResponse:
|
||||
"""Exports the SQL query results to a CSV
|
||||
---
|
||||
get:
|
||||
summary: >-
|
||||
Exports the SQL query results to a CSV
|
||||
parameters:
|
||||
- in: path
|
||||
schema:
|
||||
type: integer
|
||||
name: client_id
|
||||
description: The SQL query result identifier
|
||||
responses:
|
||||
200:
|
||||
description: SQL query results
|
||||
content:
|
||||
text/csv:
|
||||
schema:
|
||||
type: string
|
||||
400:
|
||||
$ref: '#/components/responses/400'
|
||||
401:
|
||||
$ref: '#/components/responses/401'
|
||||
403:
|
||||
$ref: '#/components/responses/403'
|
||||
404:
|
||||
$ref: '#/components/responses/404'
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
result = SqlResultExportCommand(client_id=client_id).run()
|
||||
|
||||
query = result.get("query")
|
||||
data = result.get("data")
|
||||
row_count = result.get("count")
|
||||
|
||||
quoted_csv_name = parse.quote(query.name)
|
||||
response = CsvResponse(
|
||||
data, headers=generate_download_headers("csv", quoted_csv_name)
|
||||
)
|
||||
event_info = {
|
||||
"event_type": "data_export",
|
||||
"client_id": client_id,
|
||||
"row_count": row_count,
|
||||
"database": query.database.name,
|
||||
"schema": query.schema,
|
||||
"sql": query.sql,
|
||||
"exported_format": "csv",
|
||||
}
|
||||
event_rep = repr(event_info)
|
||||
logger.debug(
|
||||
"CSV exported: %s", event_rep, extra={"superset_event": event_info}
|
||||
)
|
||||
return response
|
||||
|
||||
@expose("/results/")
|
||||
@protect()
|
||||
@statsd_metrics
|
||||
|
||||
Reference in New Issue
Block a user