# Generated by Django 3.2.5 on 2021-08-14 15:40

from treebeard.mp_tree import MP_Node
from django.db import migrations, models
from django_scopes import scopes_disabled
# update if needed
steplen = MP_Node.steplen
alphabet = MP_Node.alphabet
node_order_by = ["name"]


def update_paths(apps, schema_editor):
    with scopes_disabled():
        Node = apps.get_model("cookbook", "Food")
        nodes = Node.objects.all().order_by(*node_order_by)
        for i, node in enumerate(nodes, 1):
            # for default values, this resolves to: "{:04d}".format(i)
            node.path = f"{{:{alphabet[0]}{steplen}d}}".format(i)
        if nodes:
            Node.objects.bulk_update(nodes, ["path"])


def backwards(apps, schema_editor):
    """nothing to do"""


class Migration(migrations.Migration):

    dependencies = [
        ('cookbook', '0149_fix_leading_trailing_spaces'),
    ]

    operations = [
        migrations.AddField(
            model_name='food',
            name='depth',
            field=models.PositiveIntegerField(default=1),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='food',
            name='numchild',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AddField(
            model_name='food',
            name='path',
            field=models.CharField(default=0, max_length=255, unique=False),
            preserve_default=False,
        ),
        migrations.RunPython(update_paths, backwards),
        migrations.AlterField(
            model_name="food",
            name="path",
            field=models.CharField(max_length=255, unique=True),
        ),
    ]
