# Generated by Django 3.2.15 on 2023-01-27 08:35

from django.db import migrations, models


def update_view_permissions(apps, schema_editor):
    """Make material available to members prior to removing members_only."""
    TopicMaterialRelation = apps.get_model("topics", "TopicMaterialRelation")
    Group = apps.get_model("auth", "Group")

    for relation in TopicMaterialRelation.objects.filter(members_only=False):
        members_group = Group.objects.get_or_create(name="Community members")[
            0
        ]
        relation.material.group_view_permission.add(members_group)


def set_material_permissions(apps, schema_editor):
    TopicMaterialRelation = apps.get_model("topics", "TopicMaterialRelation")
    Topic = apps.get_model("topics", "Topic")
    ContentType = apps.get_model("contenttypes", "ContentType")
    UserObjectPermission = apps.get_model("guardian", "UserObjectPermission")
    Permission = apps.get_model("auth", "Permission")
    Material = apps.get_model("uploaded_material", "Material")

    material_ct = ContentType.objects.get_for_model(Material)
    topic_ct = ContentType.objects.get_for_model(Topic)

    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 TopicMaterialRelation.objects.filter(
        symbolic_relation=False
    ):
        relation.is_default = True
        relation.save()
        for perm_map in UserObjectPermission.objects.filter(
            object_pk=relation.topic.pk,
            content_type=topic_ct,
            permission__codename="change_topic",
            permission__content_type=topic_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,
                )


class Migration(migrations.Migration):

    dependencies = [
        ("topics", "0015_auto_20221223_1638"),
    ]

    operations = [
        migrations.RunPython(
            update_view_permissions, migrations.RunPython.noop
        ),
        migrations.RemoveField(
            model_name="topicmaterialrelation",
            name="members_only",
        ),
        migrations.AddField(
            model_name="topicmaterialrelation",
            name="is_default",
            field=models.BooleanField(
                default=False,
                help_text="Is this relation the default entry point?",
            ),
        ),
        migrations.AddField(
            model_name="topicmaterialrelation",
            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
        ),
    ]
