# Generated by Django 5.1.6 on 2025-02-17 13:05

import aleksis.core.mixins
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
from recurrence import serialize


def _migrate_recurrences(apps, schema_editor):
    CalendarEvent = apps.get_model('core', 'CalendarEvent')
    for event in CalendarEvent.objects.using(schema_editor.connection.alias).filter(recurrences__isnull=False):
        if not event.recurrences.rrules:
            continue
        rule = event.recurrences.rrules[0]
        # We only store until in the rrule_until field
        rule.until = None
        event.rrule = serialize(rule)
        if event.rrule.startswith("RRULE:"):
            event.rrule = event.rrule[6:]
        event.save()

class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('core', '0072_birthdayevent_view'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.RemoveConstraint(
            model_name='calendarevent',
            name='datetime_start_before_end',
        ),
        migrations.RemoveConstraint(
            model_name='calendarevent',
            name='date_start_before_end',
        ),
        migrations.AddConstraint(
            model_name='calendarevent',
            constraint=models.CheckConstraint(condition=models.Q(('datetime_end__gte', models.F('datetime_start')), ('datetime_start__isnull', True), ('datetime_end__isnull', True), _connector='OR'), name='datetime_start_before_end'),
        ),
        migrations.AddConstraint(
            model_name='calendarevent',
            constraint=models.CheckConstraint(condition=models.Q(('date_end__gte', models.F('date_start')), ('date_start__isnull', True), ('date_end__isnull', True), _connector='OR'), name='date_start_before_end'),
        ),
        migrations.RunPython(_migrate_recurrences)
    ]
