|
1 | | -# Generated by Django 4.2.29 on 2026-05-12 08:28 |
| 1 | +# Generated by Django 4.2.30 on 2026-05-21 04:49 |
2 | 2 |
|
3 | 3 | from django.db import migrations, models |
4 | 4 |
|
5 | 5 |
|
6 | | -class Migration(migrations.Migration): |
| 6 | +def migrate_sources(apps, _): |
| 7 | + """ |
| 8 | + Populate the source field for Event records: |
| 9 | + - Non auto generated events are mapped to MANUAL_INPUT. |
| 10 | + - Events that are auto generated and have auto_generated_source are mapped to the corresponding enum value. |
| 11 | + - Events that are auto generated but do not have auto_generated_source are mapped to MANUAL_INPUT. |
| 12 | + - For WHO, the who_guid field is populated with the id extracted from the auto_generated_source field. |
| 13 | + """ |
| 14 | + |
| 15 | + Event = apps.get_model("api", "Event") |
| 16 | + |
| 17 | + for obj in Event.objects.iterator(): |
| 18 | + update_fields = ["source"] |
| 19 | + |
| 20 | + if not obj.auto_generated: |
| 21 | + source = 100 # MANUAL_INPUT |
| 22 | + |
| 23 | + else: |
| 24 | + raw_source = (obj.auto_generated_source or "").lower() |
| 25 | + if "gdacs" in raw_source: |
| 26 | + source = 110 # GDACS |
| 27 | + |
| 28 | + elif "who.int" in raw_source: |
| 29 | + source = 120 |
| 30 | + |
| 31 | + parts = raw_source.strip().split(".") |
| 32 | + who_id = parts[-1].strip() |
7 | 33 |
|
| 34 | + try: |
| 35 | + obj.who_guid = int(who_id) |
| 36 | + update_fields.append("who_guid") |
| 37 | + except (ValueError, TypeError): |
| 38 | + pass |
| 39 | + elif "field report dmis ingest" in raw_source: |
| 40 | + source = 130 # FIELD_REPORT_DMIS_INGEST |
| 41 | + elif "field report admin" in raw_source: |
| 42 | + source = 140 # FIELD_REPORT_ADMIN |
| 43 | + elif "appeal" in raw_source: |
| 44 | + source = 150 # APPEAL_ADMIN |
| 45 | + elif "new field report" in raw_source: |
| 46 | + source = 160 # NEW_FIELD_REPORT |
| 47 | + else: |
| 48 | + # Using title for backward compatibility, as some events have auto_generated_source empty but title containing "GDACS" |
| 49 | + if obj.title and obj.title.lower().startswith("gdacs"): |
| 50 | + source = 110 # GDACS |
| 51 | + else: |
| 52 | + source = 100 # MANUAL_INPUT |
| 53 | + obj.auto_generated = False |
| 54 | + update_fields.append("auto_generated") |
| 55 | + obj.source = source |
| 56 | + obj.save(update_fields=update_fields) |
| 57 | + |
| 58 | + |
| 59 | +class Migration(migrations.Migration): |
8 | 60 | dependencies = [ |
9 | | - ('api', '0231_alter_export_export_type'), |
| 61 | + ("api", "0231_alter_export_export_type"), |
10 | 62 | ] |
11 | 63 |
|
12 | 64 | operations = [ |
13 | | - migrations.RemoveField( |
14 | | - model_name='event', |
15 | | - name='auto_generated_source', |
16 | | - ), |
17 | 65 | migrations.AddField( |
18 | | - model_name='event', |
19 | | - name='source', |
20 | | - field=models.IntegerField(choices=[(100, 'Manual input'), (110, 'GDACs scraper'), (120, 'WHO scraper'), (130, 'Field report DMIS ingest'), (140, 'Field report admin'), (150, 'Appeal admin'), (160, 'New field report'), (170, 'DREF')], default=100, verbose_name='Event source'), |
| 66 | + model_name="event", |
| 67 | + name="source", |
| 68 | + field=models.IntegerField( |
| 69 | + choices=[ |
| 70 | + (100, "Manual input"), |
| 71 | + (110, "GDACs scraper"), |
| 72 | + (120, "WHO scraper"), |
| 73 | + (130, "Field report DMIS ingest"), |
| 74 | + (140, "Field report admin"), |
| 75 | + (150, "Appeal admin"), |
| 76 | + (160, "New field report"), |
| 77 | + (170, "DREF"), |
| 78 | + ], |
| 79 | + default=100, |
| 80 | + verbose_name="Event source", |
| 81 | + ), |
21 | 82 | ), |
22 | 83 | migrations.AddField( |
23 | | - model_name='event', |
24 | | - name='who_guid', |
25 | | - field=models.IntegerField(blank=True, null=True, verbose_name='Who guid'), |
| 84 | + model_name="event", |
| 85 | + name="who_guid", |
| 86 | + field=models.IntegerField(blank=True, null=True, verbose_name="Who guid"), |
| 87 | + ), |
| 88 | + migrations.RunPython(migrate_sources, reverse_code=migrations.RunPython.noop), |
| 89 | + migrations.RemoveField( |
| 90 | + model_name="event", |
| 91 | + name="auto_generated_source", |
26 | 92 | ), |
27 | 93 | ] |
0 commit comments