# Generated by Django 4.2.16 on 2024-11-13 13:43

from django.db import migrations
from django.db.models import Q
from i18nfield.strings import LazyI18nString


def migrate_placeholders(apps, schema_editor):
    Rule = apps.get_model("sendmail", "Rule")
    for r in Rule.objects.filter(
            Q(template__contains="{event}") | Q(subject__contains="{event}"),
            event__has_subevents=True
    ):
        r.template = LazyI18nString({
            k: v.replace("{event}", "{event_series_name}") for k, v in r.template.data.items()
        })
        r.subject = LazyI18nString({
            k: v.replace("{event}", "{event_series_name}") for k, v in r.subject.data.items()
        })
        r.save(update_fields=["template", "subject"])


class Migration(migrations.Migration):
    dependencies = [
        ("sendmail", "008_remove_scheduled_mails"),
    ]

    operations = [
        migrations.RunPython(
            migrate_placeholders,
            migrations.RunPython.noop,
        )
    ]
