# -*- coding: utf-8 -*-
# Generated by Django 1.11.21 on 2019-10-02 13:21
from __future__ import unicode_literals

from django.contrib.auth.management import create_permissions
from django.db import migrations


new_content_editor_perms = {
    'books': [
        'add_work', 'change_work', 'delete_work',
        'add_edition', 'change_edition', 'delete_edition',
        'add_editioncreator', 'change_editioncreator', 'delete_editioncreator',
    ],
    'djiffy': [
        'add_manifest', 'change_manifest', 'delete_manifest',
        'add_canvas', 'change_canvas', 'delete_canvas',
    ],

}


# NOTE: reused from common 0002

def update_content_editor_group(apps, schema_editor):
    Group = apps.get_model("auth", "Group")
    Permission = apps.get_model("auth", "Permission")

    # make sure permissions are created before loading the fixture
    # which references them
    # (when running migrations all at once, permissions may not yet exist)
    for app_config in apps.get_app_configs():
        app_config.models_module = True
        create_permissions(app_config, apps=apps, verbosity=0)
        app_config.models_module = None

    editor_group = Group.objects.get(name='Content Editor')
    permissions = []
    for model, codenames in new_content_editor_perms.items():
        # using explicit get so that there will be an error if an
        # expected permission is not found
        for codename in codenames:
            try:
                permissions.append(Permission.objects.get(codename=codename))
            except Permission.DoesNotExist:
                # model rename means the permission codename can vary
                # depending on when this migration is run (old db or new setup)
                if 'subscribe' in codename:
                    codename = codename.replace('subscribe', 'subscription')
                    permissions.append(Permission.objects.get(codename=codename))

    # add the new permissions without removing existing ones
    editor_group.permissions.add(*permissions)


class Migration(migrations.Migration):

    dependencies = [
        ('common', '0005_create_script_user'),
    ]

    operations = [
        migrations.RunPython(update_content_editor_group,
                             reverse_code=migrations.RunPython.noop),

    ]