Skip to content

Commit

Permalink
Database filtering:
Browse files Browse the repository at this point in the history
- Generalize the new config so we can use it for other entities down the road.
- Pull the new database key from the config so we apply any given filter method in the databases API
- Adjust our tests with the new config name and structure
  • Loading branch information
Antonio-RiveroMartnez committed Jul 5, 2023
1 parent eaddeae commit a8995d8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
11 changes: 8 additions & 3 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,8 @@ class ExtraRelatedQueryFilters(TypedDict, total=False):

EXTRA_RELATED_QUERY_FILTERS: ExtraRelatedQueryFilters = {}

# Extra dynamic database filter make it possible to limit which databases are shown

# Extra dynamic query filters make it possible to limit which objects are shown
# in the UI before any other filtering is applied. Useful for example when
# considering to filter using Feature Flags along with regular role filters
# that get applied by default in our base_filters.
Expand All @@ -1594,8 +1595,12 @@ class ExtraRelatedQueryFilters(TypedDict, total=False):
# filter = Database.database_name.startswith('b')
# return query.filter(filter)
#
# EXTRA_DYNAMIC_DATABASE_FILTER = initial_database_filter
EXTRA_DYNAMIC_DATABASE_FILTER: Callable[[Query], Query] | None = None
# EXTRA_DYNAMIC_QUERY_FILTERS = {"database": initial_database_filter}
class ExtraDynamicQueryFilters(TypedDict, total=False):
databases: Callable[[Query], Query]


EXTRA_DYNAMIC_QUERY_FILTERS: ExtraDynamicQueryFilters = {}


# -------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions superset/databases/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def apply(self, query: Query, value: Any) -> Query:
filtering.
"""

if dynamic_filter := current_app.config["EXTRA_DYNAMIC_DATABASE_FILTER"]:
query = dynamic_filter(query)
if dynamic_filters := current_app.config["EXTRA_DYNAMIC_QUERY_FILTERS"]:
if dynamic_databases_filter := dynamic_filters.get("databases"):
query = dynamic_databases_filter(query)

# We can proceed with default filtering now
if security_manager.can_access_all_databases():
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3695,7 +3695,7 @@ def _base_filter(query):

with patch.dict(
"superset.views.filters.current_app.config",
{"EXTRA_DYNAMIC_DATABASE_FILTER": base_filter_mock},
{"EXTRA_DYNAMIC_QUERY_FILTERS": {"databases": base_filter_mock}},
):
uri = f"api/v1/database/"
rv = self.client.get(uri)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/databases/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def _base_filter(query):
assert base_filter_mock.call_count == 0

original_config = current_app.config.copy()
original_config["EXTRA_DYNAMIC_DATABASE_FILTER"] = base_filter_mock
original_config["EXTRA_DYNAMIC_QUERY_FILTERS"] = {"databases": base_filter_mock}

mocker.patch("superset.views.filters.current_app.config", new=original_config)
# Get filtered list
Expand Down

0 comments on commit a8995d8

Please sign in to comment.