text: # Generated by Django 2.1.7 on 2019-05-02 18:19
  
  import sys
  
  from django.contrib.auth.management import create_permissions
  from django.db import migrations
  
  
  def copy_to_reports(apps, schema):
  
      if any("test" in a for a in sys.argv):
          return
  
      from django.apps import apps
      from django.contrib.auth.management import create_permissions
  
      for app in ['reports']:
          app_config = apps.get_app_config(app)
          app_config.models_module = True
          create_permissions(app_config, verbosity=0)
          app_config.models_module = None
  
      Permission = apps.get_model("auth", "Permission")
  
      perms = ['can_run_sql_reports', 'can_create_sql_reports']
      for perm in perms:
          try:
              pfrom = Permission.objects.get(codename=perm, content_type__model="testinstance")
          except:
              # if running during tests, this perm won't exist so we don't need to copy perms
              continue
          pto = Permission.objects.get(codename=perm, content_type__model="savedreport")
          for u in pfrom.user_set.all():
              pto.user_set.add(pto)
  
          for g in pfrom.group_set.all():
              pto.group_set.add(pto)
  
  
  def copy_to_testinstance(apps, schema):
  
      Permission = apps.get_model("auth", "Permission")
  
      perms = ['can_run_sql_reports', 'can_create_sql_reports']
      for perm in perms:
          pfrom = Permission.objects.get(codename=perm, content_type__model="savedreport")
          pto = Permission.objects.get(codename=perm, content_type__model="testinstance")
          for u in pfrom.user_set.all():
              pto.user_set.add(pto)
  
          for g in pfrom.group_set.all():
              pto.group_set.add(pto)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('qa', '0029_auto_20181217_0945'),
          ('reports', '0002_auto_20190502_1418'),
      ]
  
      operations = [
          migrations.RunPython(copy_to_reports, copy_to_testinstance),
      ]
  