parameters: (json_flow, flow=None)
return_statement: return new_flow_json
docstring: Remove actions and rulesets that have references to invalid flows (is_active=False, is_archived=True)
docstring_summary: Remove actions and rulesets that have references to invalid flows (is_active=False, is_archived=True)
docstring_tokens: Remove actions and rulesets that have references to invalid flows ( is_active = False is_archived = True )
function: def migrate_to_version_11_9(json_flow, flow=None):
      """
      Remove actions and rulesets that have references to invalid flows (is_active=False, is_archived=True)
      """
  
      # this migration only matters for existing flows
      # we don't want to migrate flows which are about to be imported
      if not flow:
          return json_flow
  
      main_flow_uuid = json_flow.get("metadata", {}).get("uuid", None)
      action_sets = json_flow.get("action_sets", [])
      rule_sets = json_flow.get("rule_sets", [])
  
      detected_flows = set()
  
      for action_set in action_sets:
          for action in action_set["actions"]:
              if action["type"] == "flow":
                  flow_uuid = action["flow"]["uuid"]
                  detected_flows.add(flow_uuid)
  
              if action["type"] == "trigger-flow":
                  flow_uuid = action["flow"]["uuid"]
                  detected_flows.add(flow_uuid)
  
      for rule_set in rule_sets:
          if rule_set["ruleset_type"] == "subflow":
              flow_uuid = rule_set["config"]["flow"]["uuid"]
              detected_flows.add(flow_uuid)
  
      invalid_flow_uuids = set()
      if detected_flows:
          valid_flow_uuids = {
              flow_uuid
              for flow_uuid in Flow.objects.filter(
                  uuid__in=detected_flows, is_active=True, is_archived=False
              ).values_list("uuid", flat=True)
          }
          invalid_flow_uuids = detected_flows.difference(valid_flow_uuids)
  
      # get the copy of the flow
      new_flow_json = json_flow.copy()
      total_removed_actions = 0
      total_removed_rulesets = 0
  
      if invalid_flow_uuids:
          # remove invalid actions and rulesets
          for actionset_index, action_set in enumerate(action_sets):
              for action_index, action in enumerate(action_set["actions"]):
                  if action["type"] == "flow":
                      flow_uuid = action["flow"]["uuid"]
                      if flow_uuid in invalid_flow_uuids:
  
                          del new_flow_json["action_sets"][actionset_index]["actions"][action_index]
                          total_removed_actions += 1
  
                  if action["type"] == "trigger-flow":
                      flow_uuid = action["flow"]["uuid"]
                      if flow_uuid in invalid_flow_uuids:
  
                          del new_flow_json["action_sets"][actionset_index]["actions"][action_index]
                          total_removed_actions += 1
  
          for ruleset_index, rule_set in enumerate(rule_sets):
              if rule_set["ruleset_type"] == "subflow":
                  flow_uuid = rule_set["config"]["flow"]["uuid"]
  
                  if flow_uuid in invalid_flow_uuids:
  
                      del new_flow_json["rule_sets"][ruleset_index]
                      total_removed_rulesets += 1
  
      if total_removed_actions + total_removed_rulesets > 0:
          print(f"Flow {main_flow_uuid}: removed {total_removed_actions} actions and {total_removed_rulesets} rulesets")
  
      return new_flow_json
function_tokens: def migrate_to_version_11_9 ( json_flow , flow = None ) : # this migration only matters for existing flows # we don't want to migrate flows which are about to be imported if not flow : return json_flow main_flow_uuid = json_flow . get ( "metadata" , { } ) . get ( "uuid" , None ) action_sets = json_flow . get ( "action_sets" , [ ] ) rule_sets = json_flow . get ( "rule_sets" , [ ] ) detected_flows = set ( ) for action_set in action_sets : for action in action_set [ "actions" ] : if action [ "type" ] == "flow" : flow_uuid = action [ "flow" ] [ "uuid" ] detected_flows . add ( flow_uuid ) if action [ "type" ] == "trigger-flow" : flow_uuid = action [ "flow" ] [ "uuid" ] detected_flows . add ( flow_uuid ) for rule_set in rule_sets : if rule_set [ "ruleset_type" ] == "subflow" : flow_uuid = rule_set [ "config" ] [ "flow" ] [ "uuid" ] detected_flows . add ( flow_uuid ) invalid_flow_uuids = set ( ) if detected_flows : valid_flow_uuids = { flow_uuid for flow_uuid in Flow . objects . filter ( uuid__in = detected_flows , is_active = True , is_archived = False ) . values_list ( "uuid" , flat = True ) } invalid_flow_uuids = detected_flows . difference ( valid_flow_uuids ) # get the copy of the flow new_flow_json = json_flow . copy ( ) total_removed_actions = 0 total_removed_rulesets = 0 if invalid_flow_uuids : # remove invalid actions and rulesets for actionset_index , action_set in enumerate ( action_sets ) : for action_index , action in enumerate ( action_set [ "actions" ] ) : if action [ "type" ] == "flow" : flow_uuid = action [ "flow" ] [ "uuid" ] if flow_uuid in invalid_flow_uuids : del new_flow_json [ "action_sets" ] [ actionset_index ] [ "actions" ] [ action_index ] total_removed_actions += 1 if action [ "type" ] == "trigger-flow" : flow_uuid = action [ "flow" ] [ "uuid" ] if flow_uuid in invalid_flow_uuids : del new_flow_json [ "action_sets" ] [ actionset_index ] [ "actions" ] [ action_index ] total_removed_actions += 1 for ruleset_index , rule_set in enumerate ( rule_sets ) : if rule_set [ "ruleset_type" ] == "subflow" : flow_uuid = rule_set [ "config" ] [ "flow" ] [ "uuid" ] if flow_uuid in invalid_flow_uuids : del new_flow_json [ "rule_sets" ] [ ruleset_index ] total_removed_rulesets += 1 if total_removed_actions + total_removed_rulesets > 0 : print ( f"Flow {main_flow_uuid}: removed {total_removed_actions} actions and {total_removed_rulesets} rulesets" ) return new_flow_json