# Generated by Django 4.2.14 on 2024-07-31 03:59
from django.contrib.auth.models import Group, Permission
from django.conf import settings

from django.db import migrations
import logging

from django.db import migrations

logger = logging.getLogger(__name__)

public_group_permissions = {
    settings.DEFAULT_PERMISSIONS_GROUP: [
        "create_documentanalysisrow",
        "read_documentanalysisrow",
        "update_documentanalysisrow",
        "delete_documentanalysisrow",
        "permission_documentanalysisrow",
    ]
}


def add_group_permissions(apps, schema_editor):
    for group in public_group_permissions:
        role, created = Group.objects.get_or_create(name=group)
        logger.info(f'{group} Group created')
        for perm in public_group_permissions[group]:
            try:
                logger.info(f'Permitting {group} to {perm}')
                role.permissions.add(Permission.objects.get(codename=perm))
            except Permission.DoesNotExist:
                logger.warning(f"Permission '{perm}' does not exist yet; skipping assignment.")
        role.save()

    logger.info("Installation of default public access group COMPLETE!")


class Migration(migrations.Migration):
    dependencies = [
        ("users", "0010_setup_query_permissions"),
    ]

    operations = [
        migrations.RunPython(add_group_permissions),
    ]
