Skip to content

Commit

Permalink
feat: Add Certified filter to Datasets (apache#20136)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh authored and philipher29 committed Jun 9, 2022
1 parent c4c8dba commit 2c4b400
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
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"]
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()

0 comments on commit 2c4b400

Please sign in to comment.