-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
feat(dataset API): Add parameter to optionally render Jinja macros in API response #30721
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,16 @@ | |
import logging | ||
from datetime import datetime | ||
from io import BytesIO | ||
from typing import Any | ||
from typing import Any, Optional | ||
from zipfile import is_zipfile, ZipFile | ||
|
||
from flask import request, Response, send_file | ||
from flask_appbuilder.api import expose, protect, rison, safe | ||
from flask_appbuilder.api.schemas import get_item_schema | ||
from flask_appbuilder.const import ( | ||
API_RESULT_RES_KEY, | ||
API_SELECT_COLUMNS_RIS_KEY, | ||
) | ||
from flask_appbuilder.models.sqla.interface import SQLAInterface | ||
from flask_babel import ngettext | ||
from marshmallow import ValidationError | ||
|
@@ -65,6 +70,7 @@ | |
GetOrCreateDatasetSchema, | ||
openapi_spec_methods_override, | ||
) | ||
from superset.jinja_context import BaseTemplateProcessor, get_template_processor | ||
from superset.utils import json | ||
from superset.utils.core import parse_boolean_string | ||
from superset.views.base import DatasourceFilter | ||
|
@@ -1056,3 +1062,123 @@ def warm_up_cache(self) -> Response: | |
return self.response(200, result=result) | ||
except CommandException as ex: | ||
return self.response(ex.status, message=ex.message) | ||
|
||
@expose("/<int:pk>", methods=("GET",)) | ||
@protect() | ||
@safe | ||
@rison(get_item_schema) | ||
@statsd_metrics | ||
@event_logger.log_this_with_context( | ||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}" f".get", | ||
log_to_statsd=False, | ||
) | ||
def get(self, pk: int, **kwargs: Any) -> Response: | ||
"""Get a dataset. | ||
--- | ||
get: | ||
summary: Get a dataset | ||
description: Get a dataset by ID | ||
parameters: | ||
- in: path | ||
schema: | ||
type: integer | ||
description: The dataset ID | ||
name: pk | ||
- in: query | ||
name: q | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/get_item_schema' | ||
- in: query | ||
name: render | ||
description: Should Jinja macros from sql, metrics and columns be rendered | ||
schema: | ||
type: boolean | ||
responses: | ||
200: | ||
description: Dataset object has been returned. | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
id: | ||
description: The item id | ||
type: string | ||
result: | ||
$ref: '#/components/schemas/{{self.__class__.__name__}}.get' | ||
400: | ||
$ref: '#/components/responses/400' | ||
401: | ||
$ref: '#/components/responses/401' | ||
422: | ||
$ref: '#/components/responses/422' | ||
500: | ||
$ref: '#/components/responses/500' | ||
""" | ||
item: Optional[SqlaTable] = self.datamodel.get( | ||
pk, | ||
self._base_filters, | ||
self.show_select_columns, | ||
self.show_outer_default_load, | ||
) | ||
if not item: | ||
return self.response_404() | ||
|
||
response: dict[str, Any] = {} | ||
args = kwargs.get("rison", {}) | ||
select_cols = args.get(API_SELECT_COLUMNS_RIS_KEY, []) | ||
pruned_select_cols = [col for col in select_cols if col in self.show_columns] | ||
self.set_response_key_mappings( | ||
response, | ||
self.get, | ||
args, | ||
**{API_SELECT_COLUMNS_RIS_KEY: pruned_select_cols}, | ||
) | ||
if pruned_select_cols: | ||
show_model_schema = self.model2schemaconverter.convert(pruned_select_cols) | ||
else: | ||
show_model_schema = self.show_model_schema | ||
|
||
response["id"] = pk | ||
response[API_RESULT_RES_KEY] = show_model_schema.dump(item, many=False) | ||
Comment on lines
+1120
to
+1145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is FAB's default implementation for I decided to not simply do
I also thought about just overwriting FAB's |
||
|
||
if parse_boolean_string(request.args.get("render")): | ||
processor = get_template_processor(database=item.database) | ||
response["result"] = self.render_dataset_fields( | ||
response["result"], processor | ||
) | ||
|
||
return self.response(200, **response) | ||
|
||
@staticmethod | ||
def render_dataset_fields( | ||
data: dict[str, Any], processor: BaseTemplateProcessor | ||
) -> dict[str, Any]: | ||
""" | ||
Renders Jinja macros in the ``sql``, ``metrics`` and ``columns`` fields. | ||
|
||
:param data: Dataset info to be rendered | ||
:param processor: A ``TemplateProcessor`` instance | ||
:return: Rendered dataset data | ||
""" | ||
|
||
def render_item_list(item_list: list[dict[str, Any]]) -> list[dict[str, Any]]: | ||
return [ | ||
{**item, "expression": processor.process_template(item["expression"])} | ||
if item.get("expression") | ||
else item | ||
for item in item_list | ||
] | ||
|
||
if metrics := data.get("metrics"): | ||
data["metrics"] = render_item_list(metrics) | ||
|
||
if columns := data.get("columns"): | ||
data["columns"] = render_item_list(columns) | ||
|
||
if sql := data.get("sql"): | ||
data["sql"] = processor.process_template(sql) | ||
|
||
return data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hint: you can do
from __future__ import annotations
and then usea | None
instead ofOptional[a]
.