diff --git a/cookbook/migrations/0148_fix_leading_trailing_spaces.py b/cookbook/migrations/0148_fix_leading_trailing_spaces.py new file mode 100644 index 000000000..6d07f3ed3 --- /dev/null +++ b/cookbook/migrations/0148_fix_leading_trailing_spaces.py @@ -0,0 +1,31 @@ +from django.db import migrations, models +from django_scopes import scopes_disabled +models = ["Keyword", "Food", "Unit"] + +def update_paths(apps, schema_editor): + with scopes_disabled(): + for model in models: + Node = apps.get_model("cookbook", model) + nodes = Node.objects.all().filter(name__startswith=" ") + for i in nodes: + i.name = "_" + i.name + i.save() + nodes = Node.objects.all().filter(name__endswith=" ") + for i in nodes: + i.name = i.name + "_" + i.save() + + +def backwards(apps, schema_editor): + """nothing to do""" + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0147_auto_20210813_1829'), + ] + + operations = [ + migrations.RunPython(update_paths, backwards), + ]