# Generated by Django 2.0.13 on 2019-08-18 21:55

from django.db import migrations


def forwards(apps, schema_editor):
    'Move recipe and previous_recipe from NewsItem.payload to relations on the model'
    NewsItem = apps.get_model("core", "NewsItem")
    Recipe = apps.get_model("core", "Recipe")

    for ni in NewsItem.objects.all():
        recipe = ni.payload.get('recipe')
        previous_recipe = ni.payload.get('previous_recipe')
        if recipe:
            ni.recipe = Recipe.objects.get(id=recipe)
            del ni.payload['recipe']
        if previous_recipe:
            ni.previous_recipe = Recipe.objects.get(id=previous_recipe)
            del ni.payload['previous_recipe']
        ni.save()


def backwards(apps, schema_editor):
    NewsItem = apps.get_model("core", "NewsItem")

    for ni in NewsItem.objects.all():
        if ni.recipe:
            ni.payload['recipe'] = ni.recipe.id
        if ni.previous_recipe:
            ni.payload['previous_recipe'] = ni.previous_recipe.id
        ni.save()


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0024_auto_20190818_2155'),
    ]

    operations = [
        migrations.RunPython(forwards, backwards),
    ]
