# Generated by Django 2.2.11 on 2020-03-11 18:30

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

data_viewer_perms = {
    'accounts': [
        'view_account',
        "view_address",
        'view_accountaddress',
        'view_borrow',
        'view_event',
        'view_purchase',
        'view_reimbursement',
        'view_subscription',
        'view_subscriptiontype'
    ],
    'books': [
        'view_publisher',
        'view_publisherplace',
        'view_creator',
        'view_creatortype',
        'view_work',
        'view_edition',
        'view_editioncreator',
    ],
    'footnotes': [
        'view_bibliography',
        'view_footnote',
        'view_sourcetype'
    ],
    'people': [
        'view_country',
        'view_infourl',
        'view_person',
        'view_profession',
        'view_relationship',
        'view_relationshiptype'
    ],
    'djiffy': [
        'view_manifest',
        'view_canvas',
    ]
}


def create_data_viewer_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

    data_viewer, created = Group.objects.get_or_create(name='Data Viewer')
    permissions = []
    for app_name, codenames in data_viewer_perms.items():
        # using explicit get so that there will be an error if an
        # expected permission is not found
        for codename in codenames:
            # NOTE: djiffy permissions exist twice, since the app
            # previously defined its own view permissions
            perms = Permission.objects.filter(
                codename=codename, content_type__app_label=app_name)
            permissions.extend(perms)

    data_viewer.permissions.set(permissions)


def remove_data_viewer_group(apps, schema_editor):
    Group = apps.get_model('auth', 'Group')
    Group.objects.filter(name='Data Viewer').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('common', '0006_update_content_editor_perms_works_and_djiffy'),
    ]

    operations = [
        migrations.RunPython(create_data_viewer_group,
                             reverse_code=remove_data_viewer_group)
    ]
