# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-03-16 19:55
# flake8: noqa
from __future__ import unicode_literals

from django.db import migrations


def multiple_to_single(apps, schema_editor):
    """
    Take the first action in a recipe and set it as the single action.
    """
    Recipe = apps.get_model('recipes', 'Recipe')
    for recipe in Recipe.objects.all():
        if recipe.recipeaction_set.count() < 1:
            raise ValueError('Cannot migrate recipe pk={0} as it has no actions. Delete it manually'
                             ' or add an action and re-run this migration.'.format(recipe.pk))
        recipe_action = recipe.recipeaction_set.order_by('order')[0]
        recipe.action = recipe_action.action
        recipe.arguments_json = recipe_action.arguments_json
        recipe.save()


def noop(apps, schema_editor):
    pass  # Not too concerned about going backwards here.


class Migration(migrations.Migration):
    dependencies = [
        ('recipes', '0020_auto_20160316_1947'),
    ]

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