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

[charts] New, custom filter for name OR description #9492

Merged
merged 3 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ChartUpdateFailedError,
)
from superset.charts.commands.update import UpdateChartCommand
from superset.charts.filters import ChartFilter
from superset.charts.filters import ChartFilter, ChartNameOrDescriptionFilter
from superset.charts.schemas import (
ChartPostSchema,
ChartPutSchema,
Expand Down Expand Up @@ -111,6 +111,7 @@ class ChartRestApi(BaseSupersetModelRestApi):
)
base_order = ("changed_on", "desc")
base_filters = [["id", ChartFilter, lambda: []]]
search_filters = {"slice_name": [ChartNameOrDescriptionFilter]}

# Will just affect _info endpoint
edit_columns = ["slice_name"]
Expand Down
20 changes: 20 additions & 0 deletions superset/charts/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,33 @@
# under the License.
from typing import Any

from flask_babel import lazy_gettext as _
from sqlalchemy import or_
from sqlalchemy.orm.query import Query

from superset import security_manager
from superset.models.slice import Slice
from superset.views.base import BaseFilter


class ChartNameOrDescriptionFilter(
BaseFilter
): # pylint: disable=too-few-public-methods
name = _("Name or Description")
arg_name = "name_or_description"

def apply(self, query: Query, value: Any) -> Query:
if not value:
return query
ilike_value = f"%{value}%"
return query.filter(
or_(
Slice.slice_name.ilike(ilike_value),
Slice.description.ilike(ilike_value),
)
)


class ChartFilter(BaseFilter): # pylint: disable=too-few-public-methods
def apply(self, query: Query, value: Any) -> Query:
if security_manager.all_datasource_access():
Expand Down
37 changes: 37 additions & 0 deletions tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,43 @@ def test_get_charts_filter(self):
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["count"], 5)

def test_get_charts_custom_filter(self):
"""
Chart API: Test get charts custom filter
"""
admin = self.get_user("admin")
chart1 = self.insert_chart("foo", [admin.id], 1, description="ZY_bar")
chart2 = self.insert_chart("zy_foo", [admin.id], 1, description="desc1")
chart3 = self.insert_chart("foo", [admin.id], 1, description="desc1zy_")
chart4 = self.insert_chart("bar", [admin.id], 1, description="foo")

arguments = {
"filters": [
{"col": "slice_name", "opr": "name_or_description", "value": "zy_"}
]
}
self.login(username="admin")
uri = f"api/v1/chart/?q={prison.dumps(arguments)}"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["count"], 3)
dpgaspar marked this conversation as resolved.
Show resolved Hide resolved

self.logout()
self.login(username="gamma")
uri = f"api/v1/chart/?q={prison.dumps(arguments)}"
rv = self.client.get(uri)
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data["count"], 0)

# rollback changes
db.session.delete(chart1)
db.session.delete(chart2)
db.session.delete(chart3)
db.session.delete(chart4)
db.session.commit()

def test_get_charts_page(self):
"""
Chart API: Test get charts filter
Expand Down