# Generated by Django 3.0.5 on 2020-04-15 09:43

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


def create_plugin_permission(apps):
    app_config = apps.get_app_config("plugins")
    app_config.models_module = True
    create_permissions(app_config, apps=apps, verbosity=0)
    app_config.models_module = None


def change_extension_permission_to_plugin_permission(apps, schema_editor):
    create_plugin_permission(apps)

    permission = apps.get_model("auth", "Permission")
    users = apps.get_model("account", "User")
    service_account = apps.get_model("account", "ServiceAccount")

    plugin_permission = permission.objects.filter(
        codename="manage_plugins", content_type__app_label="plugins"
    ).first()
    extension_permission = permission.objects.filter(
        codename="manage_plugins", content_type__app_label="extensions"
    ).first()
    users = users.objects.filter(
        user_permissions__content_type__app_label="extensions",
        user_permissions__codename="manage_plugins",
    )
    service_accounts = service_account.objects.filter(
        permissions__content_type__app_label="extensions",
        permissions__codename="manage_plugins",
    )
    if not plugin_permission or not extension_permission:
        return

    for user in users:
        user.user_permissions.remove(extension_permission)
        user.user_permissions.add(plugin_permission)

    for service_account in service_accounts:
        service_account.permissions.remove(extension_permission)
        service_account.permissions.add(plugin_permission)

    if extension_permission:
        extension_permission.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("account", "0039_auto_20200221_0257"),
        ("plugins", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(change_extension_permission_to_plugin_permission),
    ]
