Skip to content

Commit 5a3677a

Browse files
committed
feat(dref): add script to migrate related event to dref
1 parent 2c0a0fb commit 5a3677a

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.core.management.base import BaseCommand
2+
3+
from api.models import Appeal, AppealType
4+
from dref.models import Dref
5+
6+
7+
class Command(BaseCommand):
8+
help = "Migrate related Event to Dref"
9+
10+
def handle(self, *args, **options):
11+
self.stdout.write(self.style.NOTICE("Starting migration of events to Dref..."))
12+
13+
appeal_qs = Appeal.objects.filter(atype=AppealType.DREF).exclude(code__isnull=True)
14+
15+
appeal_map = {appeal.code: appeal.event for appeal in appeal_qs if appeal.event}
16+
17+
drefs = Dref.objects.exclude(appeal_code__isnull=True)
18+
self.stdout.write(self.style.NOTICE(f"Total Dref records with appeal code:{drefs.count()}"))
19+
count = 0
20+
21+
for dref in drefs:
22+
event_id = appeal_map.get(dref.appeal_code)
23+
if event_id:
24+
dref.event = event_id
25+
count += 1
26+
27+
Dref.objects.bulk_update(drefs, ["event"])
28+
self.stdout.write(self.style.SUCCESS(f"Updated {count} Dref records with related Event"))

0 commit comments

Comments
 (0)