# Generated by Django 4.2.7 on 2024-04-03 17:24

from django.db import migrations
from django.contrib.auth.models import Group, Permission


def update_groups_permissions(apps, schema_editor):

    all_perms = Permission.objects.all()
    moderator_perms = [i for i in all_perms if i.content_type.app_label == 'agenda_culturel' and i.content_type.model in ['event', 'duplicatedevents']]
    read_mod_perms = [i for i in moderator_perms if i.codename.startswith('view_')]

    # set permissions for receptionists
    qanda_perms = [i for i in all_perms if i.content_type.app_label == 'agenda_culturel' and i.content_type.model in ['moderationquestion', 'moderationanswer']]
    Group.objects.get(name="Q&A Manager").permissions.add(*qanda_perms)
    Group.objects.get(name="Q&A Manager").permissions.add(*read_mod_perms)

def no_permission_change(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('agenda_culturel', '0039_moderationquestion_moderationanswer'),
    ]

    operations = [
        migrations.RunPython(update_groups_permissions, reverse_code=no_permission_change),
    ]
