This is an automated email from the ASF dual-hosted git repository.

graceguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new ca2bc8b  [fix] remove chart id from filter_scopes metadata if chart is 
not in dash anymore (#9213)
ca2bc8b is described below

commit ca2bc8b15f0a69fabdcfd7a2f57a4057bfd6723d
Author: Grace Guo <grace....@airbnb.com>
AuthorDate: Wed Feb 26 17:33:01 2020 -0800

    [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
---
 superset/utils/dashboard_filter_scopes_converter.py | 17 ++++++++++-------
 superset/views/core.py                              | 14 ++++++++++++++
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/superset/utils/dashboard_filter_scopes_converter.py 
b/superset/utils/dashboard_filter_scopes_converter.py
index 6546479..f28370b 100644
--- a/superset/utils/dashboard_filter_scopes_converter.py
+++ b/superset/utils/dashboard_filter_scopes_converter.py
@@ -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
diff --git a/superset/views/core.py b/superset/views/core.py
index d876643..ba20534 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -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()

Reply via email to