# Generated by Django 3.1.7 on 2021-03-30 19:42

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", "Keyword")
        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', '0146_alter_userpreference_use_fractions'),
    ]

    operations = [
        migrations.AddField(
            model_name='keyword',
            name='depth',
            field=models.PositiveIntegerField(default=1),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='keyword',
            name='numchild',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AddField(
            model_name='keyword',
            name='path',
            field=models.CharField(default="", max_length=255, unique=False),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='userpreference',
            name='use_fractions',
            field=models.BooleanField(default=True),
        ),
        migrations.RunPython(update_paths, backwards),
        migrations.AlterField(
            model_name="keyword",
            name="path",
            field=models.CharField(max_length=255, unique=True),
        ),
        migrations.AlterUniqueTogether(
            name='keyword',
            unique_together=set(),
        ),
        migrations.AddConstraint(
            model_name='keyword',
            constraint=models.UniqueConstraint(fields=('space', 'name'), name='unique_name_per_space'),
        ),
    ]
