From 48f37ca1dbf9935a0878e6c487b0fde72b51fd09 Mon Sep 17 00:00:00 2001 From: Grace Guo Date: Wed, 27 Jan 2021 10:28:17 -0800 Subject: [PATCH] fix: Prevent dashboard with filter_values template cause incompatible indicator (#12742) * fix: Prevent dashboard using filter_values to add incompatible indicator * fix review comments * fix review comments * add extra fix per comments --- superset/viz.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/superset/viz.py b/superset/viz.py index 4250022c67d18..f0935633214e6 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -102,6 +102,11 @@ "size", ] +# This regex is to get user defined filter column name, which is the first param in the filter_values function. +# see the definition of filter_values template: +# https://github.com/apache/superset/blob/24ad6063d736c1f38ad6f962e586b9b1a21946af/superset/jinja_context.py#L63 +FILTER_VALUES_REGEX = re.compile(r"filter_values\(['\"](\w+)['\"]\,") + class BaseViz: @@ -467,17 +472,27 @@ def get_payload(self, query_obj: Optional[QueryObjectDict] = None) -> VizPayload filters = self.form_data.get("filters", []) filter_columns = [flt.get("col") for flt in filters] columns = set(self.datasource.column_names) + filter_values_columns = [] + + # if using virtual datasource, check filter_values + if self.datasource.sql: + filter_values_columns = ( + re.findall(FILTER_VALUES_REGEX, self.datasource.sql) + ) or [] + applied_time_extras = self.form_data.get("applied_time_extras", {}) applied_time_columns, rejected_time_columns = utils.get_time_filter_status( self.datasource, applied_time_extras ) payload["applied_filters"] = [ - {"column": col} for col in filter_columns if col in columns + {"column": col} + for col in filter_columns + if col in columns or col in filter_values_columns ] + applied_time_columns payload["rejected_filters"] = [ {"reason": "not_in_datasource", "column": col} for col in filter_columns - if col not in columns + if col not in columns and col not in filter_values_columns ] + rejected_time_columns return payload