# Generated by Django 2.2.15 on 2020-08-11 19:44

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


# note: using assign_perm from guardian.shortcuts doesn't work in migrations
# https://github.com/django-guardian/django-guardian/issues/281#issuecomment-156264129
# The following function is adapted from https://gist.github.com/xuhcc/67871719116bdc0fee6c
def assign_perm(apps, perm, owner, obj=None):
    Permission = apps.get_model('auth', 'Permission')
    UserObjectPermission = apps.get_model('guardian', 'UserObjectPermission')
    GroupObjectPermission = apps.get_model('guardian', 'GroupObjectPermission')
    app_label, codename = perm.split('.', 1)

    perm = Permission.objects.get(
        content_type__app_label=app_label,
        codename=codename)
    kwargs = {
        'permission': perm,
        'content_type': perm.content_type,
        'object_pk': obj.pk,
    }
    kwargs['group'] = owner
    return GroupObjectPermission.objects.get_or_create(**kwargs)



def convert_namespace_groups_to_permissions(apps, schema_editor):
    NamespaceModel = apps.get_model('galaxy', 'Namespace')

    for namespace in NamespaceModel.objects.all():
        for group in namespace.groups.all():
            assign_perm(apps, 'galaxy.change_namespace', group, namespace)
            assign_perm(apps, 'galaxy.upload_to_namespace', group, namespace)


def convert_synclist_groups_to_permissions(apps, schema_editor):
    SyncListModel = apps.get_model('galaxy', 'SyncList')

    for synclist in SyncListModel.objects.all():
        for group in synclist.groups.all():
            assign_perm(apps, 'galaxy.change_synclist', group, synclist)
            assign_perm(apps, 'galaxy.view_synclist', group, synclist)


# Ensures that the new permissions are added to the database so that they can
# be assigned during migrations
# based off of https://stackoverflow.com/a/40092780
def initialize_permissions(apps, schema_editor):
    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


class Migration(migrations.Migration):

    dependencies = [
        ('galaxy', '0003_inbound_repo_per_namespace'),
        ('guardian', '0002_generic_permissions_index'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='namespace',
            options={'permissions': (('upload_to_namespace', 'Can upload collections to namespace'),)},
        ),
        migrations.RunPython(
            code=initialize_permissions,
        ),
        migrations.RunPython(
            code=convert_namespace_groups_to_permissions,
        ),
        migrations.RemoveField(
            model_name='namespace',
            name='groups',
        ),
        migrations.RunPython(
            code=convert_synclist_groups_to_permissions,
        ),
        migrations.RemoveField(
            model_name='synclist',
            name='groups',
        ),
        migrations.RemoveField(
            model_name='synclist',
            name='users',
        ),
    ]
