# Generated by Django 3.0.7 on 2020-06-29 19:27

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


def convert_old_specials(apps, schema_editor):
    with scopes_disabled():
        Ingredient = apps.get_model('cookbook', 'Ingredient')
        Food = apps.get_model('cookbook', 'Food')
        Unit = apps.get_model('cookbook', 'Unit')

        for i in Ingredient.objects.all():
            if i.amount == 0:
                i.no_amount = True
            if i.unit.name == 'Special:Header':
                i.header = True
                i.unit = None
                i.food = None
            i.save()

        try:
            Unit.objects.filter(name='Special:Header').delete()
            Food.objects.filter(name='Header').delete()
        except Exception:
            pass


class Migration(migrations.Migration):
    dependencies = [
        ('cookbook', '0067_auto_20200629_1508'),
    ]

    operations = [
        migrations.AddField(
            model_name='ingredient',
            name='header',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='ingredient',
            name='no_amount',
            field=models.BooleanField(default=False),
        ),
        migrations.AlterField(
            model_name='ingredient',
            name='food',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='cookbook.Food'),
        ),
        migrations.AlterField(
            model_name='ingredient',
            name='unit',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='cookbook.Unit'),
        ),
        migrations.RunPython(convert_old_specials)
    ]
