# Generated by Django 3.2.3 on 2021-05-25 14:01

from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def assign_sources(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    db_alias = schema_editor.connection.alias

    IdentificationStage = apps.get_model(
        "authentik_stages_identification", "identificationstage"
    )
    Source = apps.get_model("authentik_core", "source")

    sources = Source.objects.all()
    for stage in IdentificationStage.objects.all().using(db_alias):
        stage.sources.set(sources)
        stage.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_core", "0021_alter_application_slug"),
        (
            "authentik_stages_identification",
            "0008_alter_identificationstage_user_fields",
        ),
    ]

    operations = [
        migrations.AddField(
            model_name="identificationstage",
            name="sources",
            field=models.ManyToManyField(
                default=list,
                help_text="Specify which sources should be shown.",
                to="authentik_core.Source",
            ),
        ),
        migrations.RunPython(assign_sources),
    ]
