# Generated by Django 3.1.4 on 2020-12-16 14:24

from django.db import migrations, models
import djelectionguard.models


def remove_duplicate_candidates(apps, schema_editor):
    Contest = apps.get_model('djelectionguard.contest')

    for contest in Contest.objects.all():
        names = []
        delete = []
        for candidate in contest.candidate_set.all():
            if candidate.name in names:
                delete.append(candidate)
            names.append(candidate.name)

        for candidate in delete:
            candidate.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('djelectionguard', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(remove_duplicate_candidates),
        migrations.AlterField(
            model_name='contest',
            name='voters_emails',
            field=models.TextField(help_text='The list of allowed voters with one email per line'),
        ),
        migrations.AlterUniqueTogether(
            name='candidate',
            unique_together={('name', 'contest')},
        ),
    ]
