# Generated by Django 3.1.1 on 2020-09-25 23:32

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


# First stage for default-source-enrollment flow (prompt stage)
# needs to have its policy re-evaluated
def update_default_source_enrollment_flow_binding(
    apps: Apps, schema_editor: BaseDatabaseSchemaEditor
):
    Flow = apps.get_model("authentik_flows", "Flow")
    FlowStageBinding = apps.get_model("authentik_flows", "FlowStageBinding")
    db_alias = schema_editor.connection.alias

    flows = Flow.objects.using(db_alias).filter(slug="default-source-enrollment")
    if not flows.exists():
        return
    flow = flows.first()

    binding = FlowStageBinding.objects.get(target=flow, order=0)
    binding.re_evaluate_policies = True
    binding.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_flows", "0013_auto_20200924_1605"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="flowstagebinding",
            options={
                "ordering": ["target", "order"],
                "verbose_name": "Flow Stage Binding",
                "verbose_name_plural": "Flow Stage Bindings",
            },
        ),
        migrations.AlterField(
            model_name="flowstagebinding",
            name="re_evaluate_policies",
            field=models.BooleanField(
                default=False,
                help_text="When this option is enabled, the planner will re-evaluate policies bound to this binding.",
            ),
        ),
        migrations.RunPython(update_default_source_enrollment_flow_binding),
    ]
