# Generated by Django 1.10.1 on 2016-10-01 20:53

import django.utils.timezone
import model_utils.fields
from django.db import migrations, models


def forwards_func(apps, schema_editor):
    # We get the model from the versioned app registry;
    # if we directly import it, it'll be the wrong version
    Institution = apps.get_model("institutions", "institution")
    Email = apps.get_model("institutions", "Email")
    db_alias = schema_editor.connection.alias
    for institution in Institution.objects.using(db_alias).all():
        emails = list(
            Email.objects.filter(institution=institution.pk).order_by("priority").all()
        )
        if emails:
            institution.email = max(emails, key=lambda x: x.priority).email
            institution.save()


class Migration(migrations.Migration):
    dependencies = [("institutions", "0007_auto_20160912_2250")]

    operations = [
        migrations.AlterUniqueTogether(name="email", unique_together=set()),
        migrations.AddField(
            model_name="institution",
            name="email",
            field=models.EmailField(
                default="default-email@example.com",
                max_length=254,
                verbose_name="Email of institution",
            ),
            preserve_default=False,
        ),
        migrations.RunPython(forwards_func),
        migrations.RemoveField(model_name="email", name="institution"),
        migrations.AddField(
            model_name="institution",
            name="created",
            field=model_utils.fields.AutoCreatedField(
                default=django.utils.timezone.now,
                editable=False,
                verbose_name="created",
            ),
        ),
        migrations.AddField(
            model_name="institution",
            name="modified",
            field=model_utils.fields.AutoLastModifiedField(
                default=django.utils.timezone.now,
                editable=False,
                verbose_name="modified",
            ),
        ),
        migrations.DeleteModel(name="Email"),
    ]
