# Generated by Django 3.2.16 on 2023-02-10 07:57

from django.db import migrations


def add_permissions(apps, schema_editor):
    """
    Create "can_approve_estimated_completion_date" permission
    and add it to the "Admin" group.
    """

    Permission = apps.get_model("auth", "Permission")
    Group = apps.get_model("auth", "Group")
    ContentType = apps.get_model("contenttypes", "ContentType")

    permission, created = Permission.objects.get_or_create(
        codename="can_approve_estimated_completion_date",
        defaults={
            "name": "Can approve estimated completion date",
            "content_type": ContentType.objects.get_for_model(
                apps.get_model("barriers", "Barrier")
            ),
        },
    )

    admin_group = Group.objects.get(name="Administrator")
    admin_group.permissions.add(permission)

    print(
        'Permission "can_approve_estimated_completion_date" added to the "Admin" group.'
    )


def remove_permissions(apps, schema_editor):
    """
    Remove "can_approve_estimated_completion_date" permission
    and remove it from the "Admin" group.
    """

    Permission = apps.get_model("auth", "Permission")
    Group = apps.get_model("auth", "Group")

    permission = Permission.objects.get(
        codename="can_approve_estimated_completion_date",
    )

    admin_group = Group.objects.get(name="Administrator")
    admin_group.permissions.remove(permission)
    permission.delete()

    print(
        'Permission "can_approve_estimated_completion_date" removed from the "Admin" group.'
    )


class Migration(migrations.Migration):

    dependencies = [
        ("barriers", "0137_auto_20230209_1620"),
    ]

    operations = [
        migrations.RunPython(add_permissions, remove_permissions),
    ]
