From 6fd9d8cc5a70d579368d90db3c1d585181091bbc Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 16 Jul 2024 14:44:39 +0000 Subject: [PATCH 1/5] feat(management): add command to reassign feast data and update chants - Ensure data from old feasts are transferred to new feasts - Update chants and sequences to use new feast IDs - Delete old feasts after reassignment --- .../management/commands/reassign_feasts.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 django/cantusdb_project/main_app/management/commands/reassign_feasts.py diff --git a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py new file mode 100644 index 000000000..c986b5bb6 --- /dev/null +++ b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py @@ -0,0 +1,44 @@ +from django.core.management.base import BaseCommand +from main_app.models import Feast, Chant + + +class Command(BaseCommand): + help = "Reassign feasts and update chants accordingly" + + def handle(self, *args, **options): + feast_mapping = { + 2456: 4474, + 2094: 4475, + } + + for old_feast_id, new_feast_id in feast_mapping.items(): + try: + old_feast = Feast.objects.get(id=old_feast_id) + new_feast = Feast.objects.get(id=new_feast_id) + except Feast.DoesNotExist as e: + self.stderr.write(self.style.ERROR(f"Feast not found: {e}")) + continue + + # Transfer data (if necessary) + new_feast.name = new_feast.name or old_feast.name + new_feast.description = new_feast.description or old_feast.description + new_feast.feast_code = new_feast.feast_code or old_feast.feast_code + new_feast.notes = new_feast.notes or old_feast.notes + new_feast.month = new_feast.month or old_feast.month + new_feast.day = new_feast.day or old_feast.day + new_feast.save() + + # Reassign chants and sequences + chants_updated = Chant.objects.filter(feast=old_feast).update( + feast=new_feast + ) + self.stdout.write( + self.style.SUCCESS( + f"Reassigned {chants_updated} chants from feast {old_feast_id} to {new_feast_id}" + ) + ) + + old_feast.delete() + self.stdout.write(self.style.SUCCESS(f"Deleted old feast {old_feast_id}")) + + self.stdout.write(self.style.SUCCESS("Feast reassignment complete.")) From 8c24c2e7add8e4331ec7e0589f3445f5fac9503a Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 16 Jul 2024 15:11:17 +0000 Subject: [PATCH 2/5] style(feast): remove comment - add_prefix management command no longer exists --- django/cantusdb_project/main_app/models/feast.py | 1 - 1 file changed, 1 deletion(-) diff --git a/django/cantusdb_project/main_app/models/feast.py b/django/cantusdb_project/main_app/models/feast.py index 7caaf0041..b35f8cbcc 100644 --- a/django/cantusdb_project/main_app/models/feast.py +++ b/django/cantusdb_project/main_app/models/feast.py @@ -17,7 +17,6 @@ class Feast(BaseModel): blank=True, null=True, validators=[MinValueValidator(1), MaxValueValidator(31)] ) - # the `prefix` field can be automatically populated by running `python manage.py add_prefix` prefix = models.CharField(max_length=2, blank=True, null=True, editable=False) class Meta: From 68321f921bd7eececa62343845f8428941cdfc8c Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 16 Jul 2024 15:12:26 +0000 Subject: [PATCH 3/5] style(management): add comment --- .../main_app/management/commands/reassign_feasts.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py index c986b5bb6..81be56680 100644 --- a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py +++ b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py @@ -26,6 +26,8 @@ def handle(self, *args, **options): new_feast.notes = new_feast.notes or old_feast.notes new_feast.month = new_feast.month or old_feast.month new_feast.day = new_feast.day or old_feast.day + + # Call save method to update 'prefix' field new_feast.save() # Reassign chants and sequences From ca66179156cb67f654643e7554289130241bd507 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 16 Jul 2024 15:15:33 +0000 Subject: [PATCH 4/5] fix(management): reassign sequences --- .../management/commands/reassign_feasts.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py index 81be56680..14b1d37c1 100644 --- a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py +++ b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py @@ -1,5 +1,5 @@ from django.core.management.base import BaseCommand -from main_app.models import Feast, Chant +from main_app.models import Feast, Chant, Sequence class Command(BaseCommand): @@ -27,10 +27,10 @@ def handle(self, *args, **options): new_feast.month = new_feast.month or old_feast.month new_feast.day = new_feast.day or old_feast.day - # Call save method to update 'prefix' field + # Calling save method will update 'prefix' field new_feast.save() - # Reassign chants and sequences + # Reassign chants chants_updated = Chant.objects.filter(feast=old_feast).update( feast=new_feast ) @@ -40,6 +40,16 @@ def handle(self, *args, **options): ) ) + # Reassign sequences + sequences_updated = Sequence.objects.filter(feast=old_feast).update( + feast=new_feast + ) + self.stdout.write( + self.style.SUCCESS( + f"Reassigned {sequences_updated} sequences from feast {old_feast_id} to {new_feast_id}" + ) + ) + old_feast.delete() self.stdout.write(self.style.SUCCESS(f"Deleted old feast {old_feast_id}")) From f62481ce089ae85cfca090c7cf415510bdfae268 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 17 Jul 2024 13:35:19 +0000 Subject: [PATCH 5/5] refactor(command): change feast mapping to a global variable --- .../main_app/management/commands/reassign_feasts.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py index 14b1d37c1..5ad542595 100644 --- a/django/cantusdb_project/main_app/management/commands/reassign_feasts.py +++ b/django/cantusdb_project/main_app/management/commands/reassign_feasts.py @@ -2,16 +2,18 @@ from main_app.models import Feast, Chant, Sequence +FEAST_MAPPING = { + 2456: 4474, + 2094: 4475, +} + + class Command(BaseCommand): help = "Reassign feasts and update chants accordingly" def handle(self, *args, **options): - feast_mapping = { - 2456: 4474, - 2094: 4475, - } - for old_feast_id, new_feast_id in feast_mapping.items(): + for old_feast_id, new_feast_id in FEAST_MAPPING.items(): try: old_feast = Feast.objects.get(id=old_feast_id) new_feast = Feast.objects.get(id=new_feast_id)