# Generated by Django 3.2.7 on 2022-03-11 07:23

import django.db.models.deletion
from django.db import migrations, models


def create_registration_groups(apps, schema_editor):
    """Create the registration groups and fill them."""
    Event = apps.get_model("events", "Event")
    Group = apps.get_model("auth", "Group")
    for event in Event.objects.all():
        event.registration_group = group = Group.objects.get_or_create(
            name=f"Registered participants of {event}: pk {event.pk}"
        )[0]
        for registration in event.registration_set.filter(
            member__user__isnull=False
        ):
            registration.member.user.groups.add(group)


def fill_view_connections_groups(apps, schema_editor):
    """Fill the view_connections_groups with view_programme_groups"""
    Event = apps.get_model("events", "Event")
    for event in Event.objects.all():
        event.view_connections_groups.add(*event.view_programme_groups.all())


def dummy_reverse(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("events", "0002_auto_20220305_2137"),
        ("auth", "0012_alter_user_first_name_max_length"),
        ("registrations", "0002_auto_20220111_0901"),
    ]

    operations = [
        migrations.AddField(
            model_name="event",
            name="display_submissions",
            field=models.BooleanField(
                default=False,
                help_text="Control if the submissions page should be available from the menu",
            ),
        ),
        migrations.AddField(
            model_name="event",
            name="single_session_mode",
            field=models.BooleanField(
                default=True,
                help_text="Avoid the programme view and render everything on the landing page of the event.",
            ),
        ),
        migrations.AddField(
            model_name="event",
            name="registration_group",
            field=models.OneToOneField(
                help_text="The registered users as a Django Group",
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="event_registrations",
                to="auth.group",
            ),
        ),
        migrations.AddField(
            model_name="event",
            name="view_connections_groups",
            field=models.ManyToManyField(
                help_text="What groups can view the connection details for meeting rooms?",
                related_name="view_event_connections",
                to="auth.Group",
            ),
        ),
        migrations.AlterField(
            model_name="event",
            name="display_submission_button",
            field=models.BooleanField(
                default=False,
                help_text="Display the button to submit an abstract even if the abstract submission has not yet opened (note, this does only affect the groups mentioned in the <i>submission groups</i>).",
            ),
        ),
        migrations.RunPython(fill_view_connections_groups, dummy_reverse),
        migrations.RunPython(create_registration_groups, dummy_reverse),
    ]
