# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-06-28 10:29
from __future__ import unicode_literals

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


def fix_goals_none_plans(apps, schema_editor):
    """Use a dummy plan for goals with no plans
    """
    Plan = apps.get_model('goals', 'Plan')
    Goal = apps.get_model('goals', 'Goal')

    for goal in Goal.objects.iterator():
        if not goal.plan:
            plan, created = Plan.objects.get_or_create(
                code='dummyplan',
                defaults={'name': 'Dummy Plan', 'code': 'dummyplan'})
            goal.plan = plan
            goal.save()


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('goals', '0024_add_fields_name_and_slug_to_indicator'),
    ]

    operations = [
        migrations.RunPython(fix_goals_none_plans, reverse_code=dummy),
        migrations.AlterField(
            model_name='goal',
            name='plan',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='goals', to='goals.Plan', verbose_name='plan'),
        ),
    ]
