# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-03-07 11:32
from __future__ import unicode_literals

from django.db import migrations


def set_contact_persons(apps, schema_editor):
    ContactPerson = apps.get_model('climatemodels', 'ContactPerson')
    User = apps.get_model('auth', 'User')
    UserProfile = apps.get_model('contrib', 'UserProfile')
    for contact_person in ContactPerson.objects.all():
        first_name = ''
        last_name = ''
        first_last_name = contact_person.name.split(' ', 1)
        if len(first_last_name) > 0:
            first_name = first_last_name[0]
        if len(first_last_name) > 1:
            last_name = first_last_name[1]
        user, created = User.objects.get_or_create(
            email=contact_person.email,
            defaults={'username': contact_person.email},
        )
        if created or not hasattr(user, 'userprofile'):
            UserProfile.objects.create(user=user)
        user.first_name = first_name
        user.last_name = last_name
        user.userprofile.institute = contact_person.institute
        user.userprofile.owner.add(contact_person.base_impact_model)
        user.userprofile.sector.add(contact_person.base_impact_model.sector)
        user.save()
        user.userprofile.save()


class Migration(migrations.Migration):

    dependencies = [
        ('climatemodels', '0078_set_userprofile_20170307_1152'),
        ('contrib', '0002_auto_20170307_1140'),
    ]

    operations = [
        migrations.RunPython(
            set_contact_persons
        ),
    ]

@@ -750,16 +750,24 @@
    email
+__iexact
 =contact
@@ -779,16 +779,24 @@
 on.email
+.lower()
 ,%0A      
@@ -843,16 +843,24 @@
 on.email
+.lower()
 %7D,%0A     

user lowercase