[fix] remove chart id from filter_scopes metadata if chart is not in dash anymore (#9213)

* [fix] remove chart id from filter_scopes metadata if chart is not in dashboard anymore

* fix review comments, and add check for overwrite dash function
This commit is contained in:
Grace Guo 2020-02-26 17:33:01 -08:00 committed by GitHub
parent 4f73f8a1f9
commit ca2bc8b15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 7 deletions

View File

@ -76,11 +76,14 @@ def copy_filter_scopes(
old_to_new_slc_id_dict: Dict[int, int], old_filter_scopes: Dict[str, Dict]
) -> Dict:
new_filter_scopes = {}
for (slice_id, scopes) in old_filter_scopes.items():
new_filter_key = old_to_new_slc_id_dict[int(slice_id)]
new_filter_scopes[str(new_filter_key)] = scopes
for scope in scopes.values():
scope["immune"] = [
old_to_new_slc_id_dict[slice_id] for slice_id in scope.get("immune")
]
for (filter_id, scopes) in old_filter_scopes.items():
new_filter_key = old_to_new_slc_id_dict.get(int(filter_id))
if new_filter_key:
new_filter_scopes[str(new_filter_key)] = scopes
for scope in scopes.values():
scope["immune"] = [
old_to_new_slc_id_dict[int(slice_id)]
for slice_id in scope.get("immune", [])
if int(slice_id) in old_to_new_slc_id_dict
]
return new_filter_scopes

View File

@ -1207,6 +1207,14 @@ class Superset(BaseSupersetView):
data["filter_scopes"] = json.dumps(new_filter_scopes)
else:
dash.slices = original_dash.slices
# remove slice id from filter_scopes metadata if slice is removed from dashboard
if "filter_scopes" in data:
new_filter_scopes = copy_filter_scopes(
old_to_new_slc_id_dict={slc.id: slc.id for slc in dash.slices},
old_filter_scopes=json.loads(data["filter_scopes"] or "{}"),
)
data["filter_scopes"] = json.dumps(new_filter_scopes)
dash.params = original_dash.params
self._set_dash_metadata(dash, data)
@ -1225,6 +1233,12 @@ class Superset(BaseSupersetView):
dash = session.query(Dashboard).get(dashboard_id)
check_ownership(dash, raise_if_false=True)
data = json.loads(request.form.get("data"))
if "filter_scopes" in data:
new_filter_scopes = copy_filter_scopes(
old_to_new_slc_id_dict={slc.id: slc.id for slc in dash.slices},
old_filter_scopes=json.loads(data["filter_scopes"] or "{}"),
)
data["filter_scopes"] = json.dumps(new_filter_scopes)
self._set_dash_metadata(dash, data)
session.merge(dash)
session.commit()