# Generated by Django 3.2.16 on 2023-03-16 10:28

from django.db import migrations

from core.logger import get_logger

logger = get_logger(__name__)


def add_group_page_permissions(apps, schema_editor):
    try:
        types = ["lock", "bulk_delete", "edit", "publish", "unlock", "add"]
        Group = apps.get_model("auth", "Group")
        Site = apps.get_model("wagtailcore", "Site")
        GroupPagePermission = apps.get_model("wagtailcore", "GroupPagePermission")
        group = Group.objects.get(name="CMS-Editors")
        site = Site.objects.get(is_default_site=True)
        page = site.root_page
        for tp in types:
            GroupPagePermission.objects.get_or_create(
                group=group, page=page, permission_type=tp
            )
    except Exception as e:
        logger.error(e)


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0004_auto_20230313_1519"),
        ("wagtailcore", "0083_workflowcontenttype"),
    ]

    operations = [
        migrations.RunPython(add_group_page_permissions, migrations.RunPython.noop)
    ]
