# Generated by Django 4.2.16 on 2025-08-20 11:35

from django.db import migrations
from django.db.models import Exists, OuterRef


def activate_plugin(apps, schema_editor):
    Event = apps.get_model('pretixbase', 'Event')
    Organizer = apps.get_model('pretixbase', 'Organizer')
    qs = Organizer.objects.filter(
        Exists(Event.objects.filter(organizer_id=OuterRef("pk"), plugins__contains="pretix.plugins.banktransfer"))
    )
    for org in qs:
        if "pretix.plugins.banktransfer" not in org.plugins:
            org.plugins = ",".join(org.plugins.split(",") + ["pretix.plugins.banktransfer"])
            org.save(update_fields=["plugins"])


class Migration(migrations.Migration):
    dependencies = [
        ("banktransfer", "0011_banktransaction_external_id"),
        ("pretixbase", "0287_organizer_plugins"),
    ]

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