# Generated by Django 3.2.18 on 2023-04-18 09:25

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


def event_speaker_to_event_speakers(apps, schema_editor):
    Event = apps.get_model("events", "Event")
    for event in Event.objects.filter(event_speaker_id__isnull=False):
        event.event_speakers.add(event.event_speaker)


def event_speakers_to_event_speaker(apps, schema_editor):
    Event = apps.get_model("events", "Event")
    for event in Event.objects.filter(event_speakers__isnull=False):
        event.event_speaker = event.event_speakers.first()
        event.save()


class Migration(migrations.Migration):
    atomic = False

    dependencies = [
        ("event_requests", "0007_auto_20230418_1034"),
        ("events", "0028_eventsubtype_unauthorized_message"),
    ]

    operations = [
        migrations.AddField(
            model_name="event",
            name="event_speakers",
            field=models.ManyToManyField(
                blank=True,
                related_name="events",
                related_query_name="event",
                to="event_requests.EventSpeaker",
                verbose_name="Intervenant·es",
            ),
        ),
        migrations.AlterField(
            model_name="event",
            name="event_speaker",
            field=models.ForeignKey(
                blank=True,
                default=None,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="legacy_events",
                related_query_name="legacy_event",
                to="event_requests.eventspeaker",
                verbose_name="Intervenant·e",
            ),
        ),
        migrations.RunPython(
            event_speaker_to_event_speakers, event_speakers_to_event_speaker
        ),
        migrations.RemoveField(
            model_name="event",
            name="event_speaker",
        ),
    ]
