# Generated by Django 5.1.7 on 2025-04-03 11:42

import aleksis.core.mixins
import django.db.models.deletion
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.db import migrations, models


def migrate_announcements(apps, schema_editor):
    CalendarEvent = apps.get_model("core", "CalendarEvent")
    Announcement = apps.get_model("core", "Announcement")
    announcement_ctype = ContentType.objects.get_for_model(Announcement)

    db_alias = schema_editor.connection.alias

    for announcement in Announcement.objects.all():
        event = CalendarEvent.objects.create(
            datetime_start=announcement.valid_from,
            datetime_end=announcement.valid_until,
            polymorphic_ctype_id = announcement_ctype.pk,
            extended_data=announcement.extended_data,
            managed_by_app_label=announcement.managed_by_app_label,
        )

        announcement.calendarevent_ptr_id = event.pk
        announcement.save()


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0080_remove_guardians'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='announcement',
            name='calendarevent_ptr',
            field=models.OneToOneField(auto_created=True, null=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, serialize=False, to='core.calendarevent'),
        ),
        migrations.RunPython(migrate_announcements),
    ]
