# Generated by Django 2.1.1 on 2018-09-11 19:56

from django.db import migrations


def add_members_to_emails(apps, schema_editor):
    Member = apps.get_model("members", "Member")
    EMail = apps.get_model("mails", "EMail")

    for email in EMail.objects.all():
        for address in email.to.split(","):
            members = Member.objects.filter(email__iexact=address.strip().lower())
            if members.count() == 1:
                email.members.add(members.first())
        email.save()


class Migration(migrations.Migration):
    dependencies = [("mails", "0005_email_members")]

    operations = [
        migrations.RunPython(add_members_to_emails, migrations.RunPython.noop)
    ]
