# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


def set_initial_qualified(apps, schema_editor):
    # need to grab the 'new' model
    Panelist = apps.get_model('panelists', 'Panelist')
    from panelists.qualification import panelist_qualification_values, qualified_check_mean, qualified_check_stdev

    for p in Panelist.objects.all():
        qualvals = panelist_qualification_values(p.pk)
        if qualvals is not None:  # defaults to false anyway
            p.qualified = qualified_check_mean(10 ** qualvals['mean']) and qualified_check_stdev(
                10 ** qualvals['sigma'])
            p.save()


def noop(apps, schema_editor):
    return True


class Migration(migrations.Migration):
    dependencies = [
        ('panelists', '0002_auto_20150716_1739'),
    ]

    operations = [
        migrations.AddField(
            model_name='panelist',
            name='qualified',
            field=models.BooleanField(default=False, verbose_name='qualified'),
            preserve_default=True,
        ),
        migrations.RunPython(code=set_initial_qualified, atomic=True, reverse_code=noop),
    ]
