betodealmeida commented on code in PR #38638:
URL: https://github.com/apache/superset/pull/38638#discussion_r2953891761
##########
superset/charts/data/api.py:
##########
@@ -156,6 +180,63 @@ def get_data( # noqa: C901
json_body["result_type"] = request.args.get("type",
ChartDataResultType.FULL)
json_body["force"] = request.args.get("force")
+ # Apply dashboard filter context when filters_dashboard_id is provided
+ dashboard_filter_context: DashboardFilterContext | None = None
+ if "filters_dashboard_id" in request.args:
+ raw = request.args.get("filters_dashboard_id")
+ try:
+ filters_dashboard_id = int(raw)
+ except (ValueError, TypeError):
+ return self.response_400(
+ message="filters_dashboard_id must be an integer"
+ )
+ else:
+ filters_dashboard_id = None
+
+ if filters_dashboard_id is not None:
+ try:
+ dashboard_filter_context = get_dashboard_filter_context(
+ dashboard_id=filters_dashboard_id,
+ chart_id=pk,
+ )
+ except ValueError as error:
+ return self.response_400(message=str(error))
+ except SupersetSecurityException:
+ return self.response_403()
+
+ if dashboard_filter_context.extra_form_data:
+ efd = dashboard_filter_context.extra_form_data
+ extra_filters = efd.get("filters", [])
+
+ for query in json_body.get("queries", []):
+ if extra_filters:
+ existing = query.get("filters") or []
+ query["filters"] = existing + [
+ {**f, "isExtra": True} for f in extra_filters
+ ]
+
+ extras = query.get("extras") or {}
+ for key in EXTRA_FORM_DATA_OVERRIDE_EXTRA_KEYS:
+ if key in efd:
+ extras[key] = efd[key]
+ if extras:
+ query["extras"] = extras
+
+ for (
+ src_key,
+ target_key,
+ ) in EXTRA_FORM_DATA_OVERRIDE_REGULAR_MAPPINGS.items():
+ if src_key in efd:
+ query[target_key] = efd[src_key]
+
+ query["extra_form_data"] = efd
+
+ # We need to apply the form data to the global context as jinga
Review Comment:
Nit: typo
```suggestion
# We need to apply the form data to the global context as
jinja
```
##########
docs/static/resources/openapi.json:
##########
@@ -1739,6 +1739,45 @@
"$ref": "#/components/schemas/ChartDataResponseResult"
},
"type": "array"
+ },
Review Comment:
This should do it:
```bash
$ superset update_api_docs
```
But it could be that someone removed or changed some of the APIs without
regenerating the spec, and your run is incorporating their changes (which
should be fine).
##########
superset/charts/data/api.py:
##########
@@ -156,6 +180,53 @@ def get_data( # noqa: C901
json_body["result_type"] = request.args.get("type",
ChartDataResultType.FULL)
json_body["force"] = request.args.get("force")
+ # Apply dashboard filter context when filters_dashboard_id is provided
+ dashboard_filter_context: DashboardFilterContext | None = None
+ filters_dashboard_id = request.args.get("filters_dashboard_id",
type=int)
+ if filters_dashboard_id is not None:
+ try:
+ dashboard_filter_context = get_dashboard_filter_context(
+ dashboard_id=filters_dashboard_id,
+ chart_id=pk,
+ )
+ except ValueError as error:
+ return self.response_400(message=str(error))
+ except SupersetSecurityException:
+ return self.response_403()
+
+ if dashboard_filter_context.extra_form_data:
+ efd = dashboard_filter_context.extra_form_data
+ extra_filters = efd.get("filters", [])
+
+ for query in json_body.get("queries", []):
+ if extra_filters:
+ existing = query.get("filters", [])
+ query["filters"] = existing + [
+ {**f, "isExtra": True} for f in extra_filters
+ ]
+
+ extras = query.get("extras", {})
+ for key in EXTRA_FORM_DATA_OVERRIDE_EXTRA_KEYS:
+ if key in efd:
+ extras[key] = efd[key]
+ if extras:
+ query["extras"] = extras
+
+ for (
+ src_key,
+ target_key,
+ ) in EXTRA_FORM_DATA_OVERRIDE_REGULAR_MAPPINGS.items():
+ if src_key in efd:
+ query[target_key] = efd[src_key]
+
+ query["extra_form_data"] = efd
Review Comment:
Yeah, I think this is fine. We're planning to cleanup the query API
(https://github.com/apache/superset/issues/37535) for many reasons, one of them
being how complicated and repeated the logic is right now.
##########
superset/charts/data/api.py:
##########
@@ -156,6 +180,53 @@ def get_data( # noqa: C901
json_body["result_type"] = request.args.get("type",
ChartDataResultType.FULL)
json_body["force"] = request.args.get("force")
+ # Apply dashboard filter context when filters_dashboard_id is provided
+ dashboard_filter_context: DashboardFilterContext | None = None
+ filters_dashboard_id = request.args.get("filters_dashboard_id",
type=int)
+ if filters_dashboard_id is not None:
+ try:
+ dashboard_filter_context = get_dashboard_filter_context(
+ dashboard_id=filters_dashboard_id,
+ chart_id=pk,
+ )
+ except ValueError as error:
+ return self.response_400(message=str(error))
+ except SupersetSecurityException:
+ return self.response_403()
+
+ if dashboard_filter_context.extra_form_data:
+ efd = dashboard_filter_context.extra_form_data
+ extra_filters = efd.get("filters", [])
+
+ for query in json_body.get("queries", []):
+ if extra_filters:
+ existing = query.get("filters", [])
+ query["filters"] = existing + [
+ {**f, "isExtra": True} for f in extra_filters
+ ]
+
+ extras = query.get("extras", {})
+ for key in EXTRA_FORM_DATA_OVERRIDE_EXTRA_KEYS:
+ if key in efd:
+ extras[key] = efd[key]
+ if extras:
+ query["extras"] = extras
+
+ for (
+ src_key,
+ target_key,
+ ) in EXTRA_FORM_DATA_OVERRIDE_REGULAR_MAPPINGS.items():
+ if src_key in efd:
+ query[target_key] = efd[src_key]
+
+ query["extra_form_data"] = efd
+
+ # We need to apply the form data to the global context as jinga
+ # templating pulls form data from the request globally, so this
+ # fallback ensures it has the filters and extra_form_data
applied
+ # when used in get_sqla_query which constructs the final query.
+ g.form_data = json_body
Review Comment:
Ugh, sorry for that.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]