new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.10.5 on 2017-01-26 08:42
  from __future__ import unicode_literals
  
  import json
  
  from django.db import migrations
  
  
  def fix_ruleset_categories_open_ended(RuleSet):
      rulesets = list(RuleSet.objects.all())
      if not rulesets:
          return
  
      affected_flows = []
  
      for ruleset in rulesets:
          base_lang = ruleset.flow.base_language
          rules_json = json.loads(ruleset.rules)
          if len(rules_json) == 2 and rules_json[1]['test']['type'] == 'timeout':
              rules_json[0]['category'][base_lang] = 'All Responses'
  
              ruleset.rules = json.dumps(rules_json)
              ruleset.save()
  
              if ruleset.flow.pk not in affected_flows:
                  affected_flows.append(ruleset.flow.pk)
                  print("Adjusted ruleset %d from flow %d" % (ruleset.id, ruleset.flow.id))
  
      print("Update oped ended categories with timeout on %d flows" % len(affected_flows))
  
  
  def apply_as_migration(apps, schema_editor):
      RuleSet = apps.get_model('flows', 'RuleSet')
  
      fix_ruleset_categories_open_ended(RuleSet)
  
  
  def apply_manual():
      from temba.flows.models import RuleSet
  
      fix_ruleset_categories_open_ended(RuleSet)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('flows', '0086_is_squashed'),
      ]
  
      operations = [
          migrations.RunPython(apply_as_migration)
      ]
  
subject: Add data migrations for rulesets