# Generated by Django 3.0.6 on 2020-05-23 19:30

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


def create_default_ad_property_mappings(apps: Apps, schema_editor):
    LDAPPropertyMapping = apps.get_model("passbook_sources_ldap", "LDAPPropertyMapping")
    mapping = {
        "name": "return ldap.get('name')",
        "first_name": "return ldap.get('givenName')",
        "last_name": "return ldap.get('sn')",
        "username": "return ldap.get('sAMAccountName')",
        "email": "return ldap.get('mail')",
    }
    db_alias = schema_editor.connection.alias
    for object_field, expression in mapping.items():
        LDAPPropertyMapping.objects.using(db_alias).get_or_create(
            expression=expression,
            object_field=object_field,
            defaults={
                "name": f"Autogenerated LDAP Mapping: {expression} -> {object_field}"
            },
        )


class Migration(migrations.Migration):

    dependencies = [
        ("passbook_sources_ldap", "0002_ldapsource_sync_users"),
    ]

    operations = [
        migrations.RunPython(create_default_ad_property_mappings),
    ]
