# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-04-06 07:04
from __future__ import unicode_literals

from django.core.management.sql import emit_post_migrate_signal
from django.db import migrations


def add_perm(apps, schema_editor):
    # Workaround to ensure the permission added in 0084_auto_20170406_0858
    # is actually created, see https://code.djangoproject.com/ticket/23422
    emit_post_migrate_signal(0, False, schema_editor.connection.alias, intermediate=True)

    GroupACL = apps.get_model('permissions', 'GroupACL')
    Group = apps.get_model('auth', 'Group')
    Permission = apps.get_model('auth', 'Permission')
    permission = Permission.objects.get(codename='mass_add_translation')
    groupacls = GroupACL.objects.filter(
        language=None, subproject=None
    ).exclude(
        project=None
    )
    for acl in groupacls.iterator():
        acl.permissions.add(permission)
    for group in Group.objects.filter(name__endswith='@Administration'):
        group.permissions.add(permission)
    for group in Group.objects.filter(name__in=('Owners', 'Managers')):
        group.permissions.add(permission)


class Migration(migrations.Migration):

    dependencies = [
        ('trans', '0084_auto_20170406_0858'),
        ('permissions', '0006_auto_20170404_1637'),
    ]

    operations = [
        migrations.RunPython(add_perm),
    ]
