Skip to content
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: Add Certified filter to Datasets #20136

Merged
merged 5 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions superset-frontend/src/components/ListView/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ export enum FilterOperator {
chartIsFav = 'chart_is_favorite',
chartIsCertified = 'chart_is_certified',
dashboardIsCertified = 'dashboard_is_certified',
datasetIsCertified = 'dataset_is_certified',
}
13 changes: 13 additions & 0 deletions superset-frontend/src/views/CRUD/data/dataset/DatasetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
accessor: 'kind_icon',
disableSortBy: true,
size: 'xs',
id: 'id',
},
{
Cell: ({
Expand Down Expand Up @@ -506,6 +507,18 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
{ label: 'Physical', value: true },
],
},
{
Header: t('Certified'),
id: 'id',
urlDisplay: 'certified',
input: 'select',
operator: FilterOperator.datasetIsCertified,
unfilteredLabel: t('Any'),
selects: [
{ label: t('Yes'), value: true },
{ label: t('No'), value: false },
],
},
{
Header: t('Search'),
id: 'table_name',
Expand Down
8 changes: 6 additions & 2 deletions superset/datasets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from superset.datasets.commands.refresh import RefreshDatasetCommand
from superset.datasets.commands.update import UpdateDatasetCommand
from superset.datasets.dao import DatasetDAO
from superset.datasets.filters import DatasetIsNullOrEmptyFilter
from superset.datasets.filters import DatasetCertifiedFilter, DatasetIsNullOrEmptyFilter
from superset.datasets.schemas import (
DatasetPostSchema,
DatasetPutSchema,
Expand Down Expand Up @@ -195,7 +195,11 @@ class DatasetRestApi(BaseSupersetModelRestApi):
"owners": RelatedFieldFilter("first_name", FilterRelatedOwners),
"database": "database_name",
}
search_filters = {"sql": [DatasetIsNullOrEmptyFilter]}
search_filters = {
"sql": [DatasetIsNullOrEmptyFilter],
"id": [DatasetCertifiedFilter],
}
search_columns = ["id", "database", "owners", "sql", "table_name"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this limits search to only these columns, is that the intention?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this with my filter PR and it broke a lot of things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i don't set the search columns, the new filter won't be referenced. So i'm explicitly setting it here just like the other API classes

filter_rel_fields = {"database": [["id", DatabaseFilter, lambda: []]]}
allowed_rel_fields = {"database", "owners"}
allowed_distinct_fields = {"schema"}
Expand Down
18 changes: 18 additions & 0 deletions superset/datasets/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ def apply(self, query: Query, value: bool) -> Query:
filter_clause = not_(filter_clause)

return query.filter(filter_clause)


class DatasetCertifiedFilter(BaseFilter): # pylint: disable=too-few-public-methods
name = _("Is certified")
arg_name = "dataset_is_certified"

def apply(self, query: Query, value: bool) -> Query:
check_value = '%"certification":%'
if value is True:
return query.filter(SqlaTable.extra.ilike(check_value))
if value is False:
return query.filter(
or_(
SqlaTable.extra.notlike(check_value),
SqlaTable.extra.is_(None),
)
)
return query
31 changes: 31 additions & 0 deletions tests/integration_tests/datasets/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def create_datasets(self):
main_db = get_main_database()
for tables_name in self.fixture_tables_names:
datasets.append(self.insert_dataset(tables_name, [admin.id], main_db))

yield datasets

# rollback changes
Expand Down Expand Up @@ -1811,3 +1812,33 @@ def test_import_dataset_invalid_v0_validation(self):
}
]
}

@pytest.mark.usefixtures("create_datasets")
def test_get_datasets_is_certified_filter(self):
"""
Dataset API: Test custom dataset_is_certified filter
"""
table_w_certification = SqlaTable(
table_name="foo",
schema=None,
owners=[],
database=get_main_database(),
sql=None,
extra='{"certification": 1}',
)
db.session.add(table_w_certification)
db.session.commit()

arguments = {
"filters": [{"col": "id", "opr": "dataset_is_certified", "value": True}]
}
self.login(username="admin")
uri = f"api/v1/dataset/?q={prison.dumps(arguments)}"
rv = self.client.get(uri)

assert rv.status_code == 200
response = json.loads(rv.data.decode("utf-8"))
assert response.get("count") == 1

db.session.delete(table_w_certification)
db.session.commit()