From 376525d92c321988baf636e7d0d2d98466b817b5 Mon Sep 17 00:00:00 2001 From: Gytha Ogg Date: Tue, 23 Apr 2024 06:00:04 +0200 Subject: [PATCH] Migrate notes to comments followed by a line break if the comments field is not empty --- .../commands/migrate_notes_to_comments.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 apis_ontology/management/commands/migrate_notes_to_comments.py diff --git a/apis_ontology/management/commands/migrate_notes_to_comments.py b/apis_ontology/management/commands/migrate_notes_to_comments.py new file mode 100644 index 0000000..278800f --- /dev/null +++ b/apis_ontology/management/commands/migrate_notes_to_comments.py @@ -0,0 +1,48 @@ +import logging +import pandas as pd +from django.core.management.base import BaseCommand +from apis_ontology.models import Person, Place, Work, Instance + +logger = logging.getLogger(__name__) +pd.set_option("display.max_colwidth", None) + + +class Command(BaseCommand): + """ + Migrates values from Legacy Notes field to Comments + for Person, Place, Instance and Work objects + """ + + def handle(self, *args, **kwargs): + """ + copies values from Notes to Comments + """ + + def copy_to_comments(obj): + if not obj.notes: + return + + logging.debug("Copying notes for obj [%s]: %s", obj.pk, obj.notes) + + if not obj.comments: + obj.comments = "" + else: + obj.comments += "\n" + logging.debug("Existing comments: %s", obj.comments) + + obj.comments += obj.notes + obj.notes = "" + obj.save() + logging.debug("Saved obj %s", obj.pk) + + for obj in Person.objects.all(): + copy_to_comments(obj) + + for obj in Place.objects.all(): + copy_to_comments(obj) + + for obj in Work.objects.all(): + copy_to_comments(obj) + + for obj in Instance.objects.all(): + copy_to_comments(obj)