Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to reassign feast data and update chants #1564

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from django.core.management.base import BaseCommand
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):

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

# Calling save method will update 'prefix' field
new_feast.save()

# Reassign chants
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}"
)
)

# 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}"))

self.stdout.write(self.style.SUCCESS("Feast reassignment complete."))
1 change: 0 additions & 1 deletion django/cantusdb_project/main_app/models/feast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading