# Generated by Django 3.1.7 on 2021-03-18 23:12

from django.db import migrations
from django_scopes import scopes_disabled


def remove_empty_food_unit(apps, schema_editor):
    with scopes_disabled():
        Ingredient = apps.get_model('cookbook', 'Ingredient')
        ShoppingListEntry = apps.get_model('cookbook', 'ShoppingListEntry')

        Food = apps.get_model('cookbook', 'Food')
        Unit = apps.get_model('cookbook', 'Unit')

        for f in Food.objects.filter(name='').all():
            for o in Ingredient.objects.filter(food=f):
                o.food = None
                o.save()

            for o in ShoppingListEntry.objects.filter(food=f):
                o.delete()
            f.delete()
        for u in Unit.objects.filter(name='').all():
            for o in Ingredient.objects.filter(unit=u):
                o.unit = None
                o.save()

            for o in ShoppingListEntry.objects.filter(unit=u):
                o.unit = None
                o.save()
            u.delete()


class Migration(migrations.Migration):
    dependencies = [
        ('cookbook', '0115_telegrambot'),
    ]

    operations = [
        migrations.RunPython(remove_empty_food_unit),
    ]
