msyavuz commented on code in PR #43234:
URL: https://github.com/apache/superset/pull/43234#discussion_r3978533216
##########
superset/connectors/sqla/models.py:
##########
@@ -460,6 +462,10 @@ def verbose_map(self) -> dict[str, str]:
if o.metric_name not in verb_map:
verb_map[o.metric_name] = o.verbose_name or o.metric_name
+ for o in self.filters:
Review Comment:
Filters merged into `verbose_map` ahead of columns means a filter named like
a column (e.g. `status`) relabels that column in every chart on the dataset,
since `client_processing.py` reads `verbose_map` for column labels. Filters
never appear in query results, so they don't belong in `verbose_map` at all.
##########
superset/commands/dataset/sql_filters/delete.py:
##########
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+from functools import partial
+from typing import Optional
+
+from superset import security_manager
+from superset.commands.base import BaseCommand
+from superset.commands.dataset.sql_filters.exceptions import (
+ DatasetFilterDeleteFailedError,
+ DatasetFilterForbiddenError,
+ DatasetFilterNotFoundError,
+)
+from superset.connectors.sqla.models import SqlFilter
+from superset.daos.dataset import DatasetDAO, DatasetFilterDAO
+from superset.exceptions import SupersetSecurityException
+from superset.utils.decorators import on_error, transaction
+
+logger = logging.getLogger(__name__)
Review Comment:
Unused.
##########
superset/commands/dataset/sql_filters/delete.py:
##########
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+from functools import partial
+from typing import Optional
+
+from superset import security_manager
+from superset.commands.base import BaseCommand
+from superset.commands.dataset.sql_filters.exceptions import (
+ DatasetFilterDeleteFailedError,
+ DatasetFilterForbiddenError,
+ DatasetFilterNotFoundError,
+)
+from superset.connectors.sqla.models import SqlFilter
+from superset.daos.dataset import DatasetDAO, DatasetFilterDAO
+from superset.exceptions import SupersetSecurityException
+from superset.utils.decorators import on_error, transaction
+
+logger = logging.getLogger(__name__)
+
+
+class DeleteDatasetFilterCommand(BaseCommand):
+ def __init__(self, dataset_id: int, model_id: int):
+ self._dataset_id = dataset_id
+ self._model_id = model_id
+ self._model: Optional[SqlFilter] = None
+
+ @transaction(on_error=partial(on_error,
reraise=DatasetFilterDeleteFailedError))
+ def run(self) -> None:
+ self.validate()
+ assert self._model
+ DatasetFilterDAO.delete([self._model])
+
+ def validate(self) -> None:
+ # Validate/populate model exists
+ self._model = DatasetDAO.find_dataset_filter(self._dataset_id,
self._model_id)
+ if not self._model:
+ raise DatasetFilterNotFoundError()
+ # Check editorship
+ try:
+ security_manager.raise_for_editorship(self._model)
Review Comment:
`is_editor` only reads `resource.editors`, which SqlFilter doesn't have, so
every non-admin gets 403 here. Metrics delete has the same pre-existing hole,
but the SIP says filter permissions equal metric permissions and this makes
filter deletion admin-only in practice. Gate on `self._model.table` instead?
##########
superset/models/helpers.py:
##########
@@ -1470,6 +1470,10 @@ def database_id(self) -> int:
def metrics(self) -> list[Any]:
return []
+ @property
+ def filters(self) -> list[Any]:
Review Comment:
No consumer: SqlaTable overrides this via the relationship and Query never
calls it. Can go.
##########
superset/datasets/schemas.py:
##########
@@ -136,6 +136,17 @@ class DatasetMetricsPutSchema(Schema):
uuid = fields.UUID(allow_none=True)
+class DatasetFiltersPutSchema(Schema):
+ id = fields.Integer()
+ expression = fields.String(required=True)
+ description = fields.String(allow_none=True)
+ extra = fields.String(allow_none=True)
+ filter_name = fields.String(required=True, validate=Length(1, 255))
+ verbose_name = fields.String(allow_none=True, metadata={Length: (1, 1024)})
Review Comment:
`metadata={Length: (1, 1024)}` validates nothing (same copy-paste bug in the
metrics schema). `validate=Length(1, 1024)` if the column limit is meant to be
enforced, otherwise a 1025-char value becomes a DB error instead of a 400.
##########
tests/integration_tests/datasets/api_tests.py:
##########
@@ -2244,6 +2244,90 @@ def test_delete_dataset_metric_fail(self,
mock_dao_delete):
assert rv.status_code == 422
assert data == {"message": "Dataset metric delete failed."}
+ @pytest.mark.usefixtures("create_datasets")
+ def test_delete_dataset_filter(self):
Review Comment:
Only DELETE is exercised end to end. No PUT test adds, renames, or removes a
filter through the API (metrics have `test_update_dataset_*` for each), so the
new validation error shaping is only covered by mocked unit tests.
##########
superset/commands/dataset/importers/v1/utils.py:
##########
@@ -435,7 +435,13 @@ def import_dataset( # noqa: C901
# uploading it again, so children present in the live DB but absent
# from the upload should be removed, not silently merged. This matches
# what an explicit overwrite would do.
- sync = ["columns", "metrics"] if (overwrite or is_soft_deleted_match) else
[]
+ # Filters are new in SIP-165. Older bundles omit the key, so only
+ # synchronize them when the payload actually includes them.
+ sync: list[str] = []
+ if overwrite or is_soft_deleted_match:
+ sync = ["columns", "metrics"]
+ if "filters" in config:
Review Comment:
No test imports a bundle that actually contains `filters` (import_test.py
stops at metrics), so the uuid-match and overwrite-sync path here is untested.
--
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]