Skip to content

Commit

Permalink
Change filter endpoint to match with new url format
Browse files Browse the repository at this point in the history
  • Loading branch information
vera-liu committed Mar 13, 2017
1 parent 0779da6 commit 57b1455
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 47 deletions.
15 changes: 7 additions & 8 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,32 +1134,31 @@ def filter(self, datasource_type, datasource_id, column):
:return:
"""
# TODO: Cache endpoint by user, datasource and column
form_data = self.get_form_data()
viz_type = form_data.get('viz_type', 'table')
datasource = ConnectorRegistry.get_datasource(
datasource_type, datasource_id, db.session)
error_redirect = '/slicemodelview/list/'
datasource_class = ConnectorRegistry.sources[datasource_type]

datasource = db.session.query(
datasource_class).filter_by(id=datasource_id).first()

if not datasource:
flash(DATASOURCE_MISSING_ERR, "alert")
return json_error_response(DATASOURCE_MISSING_ERR)
if not self.datasource_access(datasource):
flash(get_datasource_access_error_msg(datasource.name), "danger")
return json_error_response(DATASOURCE_ACCESS_ERR)

viz_type = request.args.get("viz_type")
if not viz_type and datasource.default_endpoint:
return redirect(datasource.default_endpoint)
if not viz_type:
viz_type = "table"
try:
obj = viz.viz_types[viz_type](
datasource,
form_data=request.args,
slice_=None)
form_data=form_data,
)
except Exception as e:
flash(str(e), "danger")
return redirect(error_redirect)
return json_error_response(utils.error_msg_from_exception(e))
return json_success(obj.get_values_for_column(column))

def save_or_overwrite_slice(
Expand Down
34 changes: 0 additions & 34 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,6 @@ def __init__(self, datasource, form_data, slice_=None):
self.status = None
self.error_message = None

def get_filter_url(self):
"""Returns the URL to retrieve column values used in the filter"""
data = self.orig_form_data.copy()
# Remove unchecked checkboxes because HTML is weird like that
ordered_data = MultiDict()
for key in sorted(data.keys()):
# if MultiDict is initialized with MD({key:[emptyarray]}),
# key is included in d.keys() but accessing it throws
try:
if data[key] is False:
del data[key]
continue
except IndexError:
pass

if isinstance(data, (MultiDict, ImmutableMultiDict)):
v = data.getlist(key)
else:
v = data.get(key)
if not isinstance(v, list):
v = [v]
for item in v:
ordered_data.add(key, item)
href = Href(
'/superset/filter/{self.datasource.type}/'
'{self.datasource.id}/'.format(**locals()))
return href(ordered_data)

def get_df(self, query_obj=None):
"""Returns a pandas dataframe based on the query object"""
if not query_obj:
Expand Down Expand Up @@ -268,7 +240,6 @@ def get_payload(self, force=False):
'cache_timeout': cache_timeout,
'data': data,
'error': self.error_message,
'filter_endpoint': self.filter_endpoint,
'form_data': self.form_data,
'query': self.query,
'status': self.status,
Expand Down Expand Up @@ -303,7 +274,6 @@ def data(self):
"""This is the data object serialized to the js layer"""
content = {
'form_data': self.form_data,
'filter_endpoint': self.filter_endpoint,
'token': self.token,
'viz_name': self.viz_type,
'filter_select_enabled': self.datasource.filter_select_enabled,
Expand Down Expand Up @@ -345,10 +315,6 @@ def get_values_for_column(self, column):
def get_data(self, df):
return []

@property
def filter_endpoint(self):
return self.get_filter_url()

@property
def json_data(self):
return json.dumps(self.data)
Expand Down
15 changes: 10 additions & 5 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,19 @@ def test_filter_endpoint(self):
tbl_id = self.table_ids.get('energy_usage')
table = db.session.query(SqlaTable).filter(SqlaTable.id == tbl_id)
table.filter_select_enabled = True
form_data = {
'viz_type': 'sankey',
'groupby': 'source',
'groupby': 'target',
'metric': 'sum__value',
'row_limit': 5000,
'slice_id': slice_id,
}
url = (
"/superset/filter/table/{}/target/?viz_type=sankey&groupby=source"
"&metric=sum__value&flt_col_0=source&flt_op_0=in&flt_eq_0=&"
"slice_id={}&datasource_name=energy_usage&"
"datasource_id=1&datasource_type=table")
"/superset/filter/table/{}/target/?form_data={}")

# Changing name
resp = self.get_resp(url.format(tbl_id, slice_id))
resp = self.get_resp(url.format(tbl_id, json.dumps(form_data)))
assert len(resp) > 0
assert 'Carbon Dioxide' in resp

Expand Down

0 comments on commit 57b1455

Please sign in to comment.