47 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # Generated by Django 3.0.3 on 2020-02-17 16:19
 | |
| 
 | |
| from django.apps.registry import Apps
 | |
| from django.db import migrations
 | |
| 
 | |
| 
 | |
| def cleanup_old_autogenerated(apps, schema_editor):
 | |
|     LDAPPropertyMapping = apps.get_model("passbook_sources_ldap", "LDAPPropertyMapping")
 | |
|     db_alias = schema_editor.connection.alias
 | |
|     LDAPPropertyMapping.objects.using(db_alias).filter(
 | |
|         name__startswith="Autogenerated"
 | |
|     ).delete()
 | |
| 
 | |
| 
 | |
| def create_default_ad_property_mappings(apps: Apps, schema_editor):
 | |
|     LDAPPropertyMapping = apps.get_model("passbook_sources_ldap", "LDAPPropertyMapping")
 | |
|     mapping = {
 | |
|         "name": "{{ ldap.name }}",
 | |
|         "first_name": "{{ ldap.givenName }}",
 | |
|         "last_name": "{{ ldap.sn }}",
 | |
|         "username": "{{ ldap.sAMAccountName }}",
 | |
|         "email": "{{ ldap.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", "0006_auto_20200216_1116"),
 | |
|         ("passbook_core", "0007_auto_20200217_1934"),
 | |
|     ]
 | |
| 
 | |
|     operations = [
 | |
|         migrations.RunPython(cleanup_old_autogenerated),
 | |
|         migrations.RemoveField(model_name="ldappropertymapping", name="ldap_property",),
 | |
|         migrations.RunPython(create_default_ad_property_mappings),
 | |
|     ]
 | 
