From 51f15c0ccb01a43b3fc19375e480f107f1f911d7 Mon Sep 17 00:00:00 2001 From: smilerz Date: Mon, 16 Aug 2021 15:15:38 -0500 Subject: [PATCH] fix leading and trailing spaces --- .../0148_fix_leading_trailing_spaces.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cookbook/migrations/0148_fix_leading_trailing_spaces.py 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), + ]