# Generated by Django 3.2.15 on 2023-01-04 07:53

from django.db import migrations, models


def set_material_permissions(apps, schema_editor):
    ChannelMaterialRelation = apps.get_model(
        "chats", "ChannelMaterialRelation"
    )
    Channel = apps.get_model("chats", "Channel")
    ContentType = apps.get_model("contenttypes", "ContentType")
    UserObjectPermission = apps.get_model("guardian", "UserObjectPermission")
    GroupObjectPermission = apps.get_model("guardian", "GroupObjectPermission")
    Permission = apps.get_model("auth", "Permission")
    Material = apps.get_model("uploaded_material", "Material")

    material_ct = ContentType.objects.get_for_model(Material)
    channel_ct = ContentType.objects.get_for_model(Channel)

    permissions = []
    for codename in [
        "view_material",
        "change_material",
        "delete_material",
    ]:
        try:
            permissions.append(
                Permission.objects.get(
                    codename=codename, content_type=material_ct
                )
            )
        except Permission.DoesNotExist:
            pass

    for relation in ChannelMaterialRelation.objects.filter(
        symbolic_relation=False
    ):
        for perm_map in UserObjectPermission.objects.filter(
            object_pk=relation.channel.pk,
            content_type=channel_ct,
            permission__codename="change_channel",
            permission__content_type=channel_ct,
        ):
            for permission in permissions:
                UserObjectPermission.objects.get_or_create(
                    permission=permission,
                    content_type=material_ct,
                    object_pk=relation.material.pk,
                    user=perm_map.user,
                )
        for perm_map in UserObjectPermission.objects.filter(
            object_pk=relation.channel.pk,
            content_type=channel_ct,
            permission__codename="view_channel",
            permission__content_type=channel_ct,
        ):
            UserObjectPermission.objects.get_or_create(
                permission=permissions[0],
                content_type=material_ct,
                object_pk=relation.material.pk,
                user=perm_map.user,
            )
        for group_perm_map in GroupObjectPermission.objects.filter(
            object_pk=relation.channel.pk,
            content_type=channel_ct,
            permission__codename="change_channel",
            permission__content_type=channel_ct,
        ):
            for permission in permissions:
                GroupObjectPermission.objects.get_or_create(
                    permission=permission,
                    content_type=material_ct,
                    object_pk=relation.material.pk,
                    group=group_perm_map.group,
                )
        for group_perm_map in GroupObjectPermission.objects.filter(
            object_pk=relation.channel.pk,
            content_type=channel_ct,
            permission__codename="view_channel",
            permission__content_type=channel_ct,
        ):
            GroupObjectPermission.objects.get_or_create(
                permission=permissions[0],
                content_type=material_ct,
                object_pk=relation.material.pk,
                group=group_perm_map.group,
            )


class Migration(migrations.Migration):

    dependencies = [
        (
            "chats",
            "0006_channelmaterialrelation_unique_channel_relation_for_material",
        ),
    ]

    operations = [
        migrations.AddField(
            model_name="channelmaterialrelation",
            name="is_default",
            field=models.BooleanField(
                default=False,
                help_text="Is this relation the default entry point?",
            ),
        ),
        migrations.AddField(
            model_name="channelmaterialrelation",
            name="symbolic_relation",
            field=models.BooleanField(
                default=False,
                help_text="If this is a symbolic relation, this relation does not have any influences on the permissions.",
            ),
        ),
        migrations.RunPython(
            set_material_permissions, migrations.RunPython.noop
        ),
    ]
