from __future__ import unicode_literals

from django.db import models, migrations
from django.contrib.contenttypes.management import update_all_contenttypes


def add_impersonate_permission(apps, schema_editor):
    update_all_contenttypes()  # Fixes tests
    ContentType = apps.get_model('contenttypes.ContentType')
    Permission = apps.get_model('auth.Permission')
    content_type = ContentType.objects.get(app_label='auth', model='user')
    Permission.objects.create(content_type=content_type,
                              codename='impersonate',
                              name='Can impersonate other user')


def delete_impersonate_permission(apps, schema_editor):
    apps.get_model('auth.Permission').objects.get(
        codename='impersonate').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0007_auto_20150319_0929'),
    ]

    operations = [
        migrations.RunPython(add_impersonate_permission,
                             delete_impersonate_permission),
    ]
from __future__ import unicode_literals

from django.db import models, migrations
from django.contrib.contenttypes.management import update_all_contenttypes


def add_impersonate_permission(apps, schema_editor):
    update_all_contenttypes()  # Fixes tests
    ContentType = apps.get_model('contenttypes.ContentType')
    Permission = apps.get_model('auth.Permission')
    content_type = ContentType.objects.using(
        schema_editor.connection.alias).get(app_label='auth', model='user')
    Permission.objects.using(schema_editor.connection.alias).create(
        content_type=content_type,
        codename='impersonate',
        name='Can impersonate other user'
    )


def delete_impersonate_permission(apps, schema_editor):
    perm = apps.get_model('auth.Permission').objects.using(
        schema_editor.connection.alias).get(codename='impersonate')
    perm.delete(using=schema_editor.connection.alias)


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0007_auto_20150319_0929'),
    ]

    operations = [
        migrations.RunPython(add_impersonate_permission,
                             delete_impersonate_permission),
    ]
Update latest migration to use the database provided to the migrate management command
Update latest migration to use the database provided to the migrate management command

  from __future__ import unicode_literals
  
  from django.db import models, migrations
  from django.contrib.contenttypes.management import update_all_contenttypes
  
  
  def add_impersonate_permission(apps, schema_editor):
      update_all_contenttypes()  # Fixes tests
      ContentType = apps.get_model('contenttypes.ContentType')
      Permission = apps.get_model('auth.Permission')
-     content_type = ContentType.objects.get(app_label='auth', model='user')
-     Permission.objects.create(content_type=content_type,
-                               codename='impersonate',
+     content_type = ContentType.objects.using(
+         schema_editor.connection.alias).get(app_label='auth', model='user')
+     Permission.objects.using(schema_editor.connection.alias).create(
+         content_type=content_type,
+         codename='impersonate',
-                               name='Can impersonate other user')
+         name='Can impersonate other user'
+     )
  
  
  def delete_impersonate_permission(apps, schema_editor):
-     apps.get_model('auth.Permission').objects.get(
+     perm = apps.get_model('auth.Permission').objects.using(
-         codename='impersonate').delete()
+         schema_editor.connection.alias).get(codename='impersonate')
+     perm.delete(using=schema_editor.connection.alias)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('core', '0007_auto_20150319_0929'),
      ]
  
      operations = [
          migrations.RunPython(add_impersonate_permission,
                               delete_impersonate_permission),
      ]
Update latest migration to use the database provided to the migrate management command
## Code Before:
from __future__ import unicode_literals

from django.db import models, migrations
from django.contrib.contenttypes.management import update_all_contenttypes


def add_impersonate_permission(apps, schema_editor):
    update_all_contenttypes()  # Fixes tests
    ContentType = apps.get_model('contenttypes.ContentType')
    Permission = apps.get_model('auth.Permission')
    content_type = ContentType.objects.get(app_label='auth', model='user')
    Permission.objects.create(content_type=content_type,
                              codename='impersonate',
                              name='Can impersonate other user')


def delete_impersonate_permission(apps, schema_editor):
    apps.get_model('auth.Permission').objects.get(
        codename='impersonate').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0007_auto_20150319_0929'),
    ]

    operations = [
        migrations.RunPython(add_impersonate_permission,
                             delete_impersonate_permission),
    ]
## Instruction:
Update latest migration to use the database provided to the migrate management command
## Code After:
from __future__ import unicode_literals

from django.db import models, migrations
from django.contrib.contenttypes.management import update_all_contenttypes


def add_impersonate_permission(apps, schema_editor):
    update_all_contenttypes()  # Fixes tests
    ContentType = apps.get_model('contenttypes.ContentType')
    Permission = apps.get_model('auth.Permission')
    content_type = ContentType.objects.using(
        schema_editor.connection.alias).get(app_label='auth', model='user')
    Permission.objects.using(schema_editor.connection.alias).create(
        content_type=content_type,
        codename='impersonate',
        name='Can impersonate other user'
    )


def delete_impersonate_permission(apps, schema_editor):
    perm = apps.get_model('auth.Permission').objects.using(
        schema_editor.connection.alias).get(codename='impersonate')
    perm.delete(using=schema_editor.connection.alias)


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0007_auto_20150319_0929'),
    ]

    operations = [
        migrations.RunPython(add_impersonate_permission,
                             delete_impersonate_permission),
    ]