# Generated by Django 3.1.6 on 2021-02-03 09:24

from django.apps.registry import Apps
from django.db import migrations

scope_uid_map = {
    "openid": "goauthentik.io/providers/oauth2/scope-openid",
    "email": "goauthentik.io/providers/oauth2/scope-email",
    "profile": "goauthentik.io/providers/oauth2/scope-profile",
    "ak_proxy": "goauthentik.io/providers/proxy/scope-proxy",
}


def set_managed_flag(apps: Apps, schema_editor):
    ScopeMapping = apps.get_model("authentik_providers_oauth2", "ScopeMapping")
    db_alias = schema_editor.connection.alias
    for mapping in ScopeMapping.objects.using(db_alias).filter(
        name__startswith="Autogenerated "
    ):
        mapping.managed = scope_uid_map[mapping.scope_name]
        mapping.save()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_core", "0017_managed"),
        ("authentik_providers_oauth2", "0010_auto_20201227_1804"),
    ]

    operations = [
        migrations.RunPython(set_managed_flag),
    ]
