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

[add] Save filters to dashboard #3183

Merged
merged 12 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 12 additions & 1 deletion superset/assets/javascripts/dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export function dashboardContainer(dashboard, datasources, userid) {
}
},
effectiveExtraFilters(sliceId) {
// Don't filter the filter_box itself by preselect_filters
if (this.getSlice(sliceId).formData.viz_type === 'filter_box') {
return [];
}
const f = [];
const immuneSlices = this.metadata.filter_immune_slices || [];
if (sliceId && immuneSlices.includes(sliceId)) {
Expand Down Expand Up @@ -195,6 +199,13 @@ export function dashboardContainer(dashboard, datasources, userid) {
return f;
},
addFilter(sliceId, col, vals, merge = true, refresh = true) {
// If slice doesn't exist, remove the related parameters in preselect_filters.
Copy link
Member

Choose a reason for hiding this comment

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

this function calls this.updateFilterParamsInUrl 3 times. Perhaps clearer logic would be an if statement that validates that there is a slice and column, put all the logic within that block, and call this.updateFilterParamsInUrl once at the very end

if (!this.getSlice(sliceId)) {
return this.updateFilterParamsInUrl();
} else if (col !== '__from' && col !== '__to' && (this.getSlice(sliceId).formData.groupby.indexOf(col) === -1)) {
// If col doesn't exist, remove the related parameters in preselect_filters.
return this.updateFilterParamsInUrl();
}
if (!(sliceId in this.filters)) {
this.filters[sliceId] = {};
}
Expand All @@ -211,7 +222,7 @@ export function dashboardContainer(dashboard, datasources, userid) {
if (refresh) {
this.refreshExcept(sliceId);
}
this.updateFilterParamsInUrl();
return this.updateFilterParamsInUrl();
},
readFilters() {
// Returns a list of human readable active filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class SaveModal extends React.PureComponent {
css: this.state.css,
expanded_slices: expandedSlices,
dashboard_title: dashboard.dashboard_title,
default_filters: dashboard.readFilters(),
};
let url = null;
if (saveType === 'overwrite') {
Expand Down
18 changes: 18 additions & 0 deletions superset/assets/visualizations/filter_box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ class FilterBox extends React.Component {
);
});
}
// Add created options to filtersChoices, even though it doesn't exist,
// or these options will exist in query sql but invisible to end user.
if (this.state.selectedValues.hasOwnProperty()) {
for (const filterKey of this.state.selectedValues) {
const existValues = this.props.filtersChoices[filterKey].map(f => f.id);
for (const v of this.state.selectedValues[filterKey]) {
if (existValues.indexOf(v) === -1) {
const addChoice = {
filter: filterKey,
id: v,
text: v,
metric: 0,
};
this.props.filtersChoices[filterKey].push(addChoice);
}
}
}
}
const filters = Object.keys(this.props.filtersChoices).map((filter) => {
const data = this.props.filtersChoices[filter];
const maxes = {};
Expand Down
13 changes: 13 additions & 0 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from superset.viz import viz_types
from superset.models.helpers import AuditMixinNullable, ImportMixin, set_perm
install_aliases()
import urllib
from urllib import parse # noqa

config = app.config
Expand Down Expand Up @@ -322,6 +323,18 @@ def table_names(self):

@property
def url(self):
if self.json_metadata:
# add default_filters to the preselect_filters of dashboard
json_metadata = json.loads(self.json_metadata)
default_filters = json_metadata.get('default_filters')
if default_filters:
filters = ''
if isinstance(default_filters, str):
filters = urllib.quote(default_filters)
elif isinstance(default_filters, unicode):
Copy link
Member

Choose a reason for hiding this comment

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

is this py3 compatible? can we add test coverage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I will check these later.

filters = urllib.quote(default_filters.encode('utf8'))
return "/superset/dashboard/{}/?preselect_filters={}".format(
self.slug or self.id, filters)
return "/superset/dashboard/{}/".format(self.slug or self.id)

@property
Expand Down
1 change: 1 addition & 0 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,7 @@ def _set_dash_metadata(dashboard, data):
if 'filter_immune_slice_fields' not in md:
md['filter_immune_slice_fields'] = {}
md['expanded_slices'] = data['expanded_slices']
md['default_filters'] = data.get('default_filters', '')
dashboard.json_metadata = json.dumps(md, indent=4)

@api
Expand Down