# Generated by Django 3.2.5 on 2022-02-28 09:03

from django.db import migrations

from bkuser_core.categories.constants import CategoryType


def forwards_func(apps, schema_editor):
    """创建密码规则配置"""
    SettingMeta = apps.get_model("user_settings", "SettingMeta")
    Setting = apps.get_model("user_settings", "Setting")
    ProfileCategory = apps.get_model("categories", "ProfileCategory")

    meta, _ = SettingMeta.objects.get_or_create(
        key="exclude_elements_config",
        namespace="password",
        category_type=CategoryType.LOCAL.value,
        defaults={"default": {}, "example": {"alphabet_seq": 3}},
    )

    for c in ProfileCategory.objects.filter(type=CategoryType.LOCAL.value):
        Setting.objects.get_or_create(meta=meta, category_id=c.id, value=meta.default)


def backwards_func(apps, schema_editor):
    """回滚密码规则配置"""
    SettingMeta = apps.get_model("user_settings", "SettingMeta")
    Setting = apps.get_model("user_settings", "Setting")

    Setting.objects.filter(meta__key="exclude_elements_config").delete()
    SettingMeta.objects.filter(key="exclude_elements_config").delete()


class Migration(migrations.Migration):

    dependencies = [
        ("user_settings", "0011_alter_extend_fields_connection_settings"),
    ]

    operations = [migrations.RunPython(forwards_func, backwards_func)]
