Skip to content

Commit 631d73e

Browse files
Merge pull request #2214 from IFRCGo/fix/country_directory_issue
Fix NS directory and Capacity Issue
2 parents 279b899 + 29cc202 commit 631d73e

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

api/management/commands/ingest_ns_capacity.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
from django.conf import settings
33
from django.core.management.base import BaseCommand
4+
from django.db import transaction
45
from sentry_sdk.crons import monitor
56

67
from api.logger import logger
@@ -12,6 +13,7 @@ class Command(BaseCommand):
1213
help = "Add ns contact details"
1314

1415
@monitor(monitor_slug=SentryMonitor.INGEST_NS_CAPACITY)
16+
@transaction.atomic
1517
def handle(self, *args, **kwargs):
1618
logger.info("Starting NS Contacts")
1719

@@ -32,6 +34,7 @@ def handle(self, *args, **kwargs):
3234

3335
resp_ocac_data = resp_ocac.json()
3436
ocaa_count = 0
37+
country_capacity_ids = []
3538
for item in resp_ocac_data:
3639
ocaa_count += 1
3740
data = {
@@ -42,7 +45,22 @@ def handle(self, *args, **kwargs):
4245
"country": Country.objects.filter(fdrs=item["NsId"]).first(),
4346
"assessment_type": CountryCapacityStrengthening.AssessmentType.OCAC,
4447
}
45-
CountryCapacityStrengthening.objects.create(**data)
48+
country_capacity_ocac, created = CountryCapacityStrengthening.objects.get_or_create(
49+
country=data["country"],
50+
assessment_code=data["assessment_code"],
51+
assessment_type=data["assessment_type"],
52+
defaults={
53+
"submission_date": data["submission_date"],
54+
"url": data["url"],
55+
"year": data["year"],
56+
},
57+
)
58+
if not created:
59+
country_capacity_ocac.submission_date = data["submission_date"]
60+
country_capacity_ocac.url = data["url"]
61+
country_capacity_ocac.year = data["year"]
62+
country_capacity_ocac.save(update_fields=["submission_date", "url", "year"])
63+
country_capacity_ids.append(country_capacity_ocac.pk)
4664

4765
text_to_log = "%s Ns capacity added" % ocaa_count
4866
logger.info(text_to_log)
@@ -70,4 +88,23 @@ def handle(self, *args, **kwargs):
7088
"assessment_type": CountryCapacityStrengthening.AssessmentType.BOCA,
7189
"branch_name": item["BranchName"],
7290
}
73-
CountryCapacityStrengthening.objects.create(**data)
91+
country_capacity_boca, created = CountryCapacityStrengthening.objects.get_or_create(
92+
country=data["country"],
93+
assessment_code=data["assessment_code"],
94+
assessment_type=data["assessment_type"],
95+
defaults={
96+
"year": data["year"],
97+
"branch_name": data["branch_name"],
98+
"submission_date": data["submission_date"],
99+
"url": data["url"],
100+
},
101+
)
102+
if not created:
103+
country_capacity_boca.submission_date = data["submission_date"]
104+
country_capacity_boca.url = data["url"]
105+
country_capacity_boca.year = data["year"]
106+
country_capacity_boca.branch_name = data["branch_name"]
107+
country_capacity_boca.save(update_fields=["submission_date", "url", "year", "branch_name"])
108+
country_capacity_ids.append(country_capacity_boca.pk)
109+
# Delete the country capacity strengthening which are not available in the source
110+
CountryCapacityStrengthening.objects.exclude(id__in=country_capacity_ids).delete()

api/management/commands/ingest_ns_directory.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def postprocessor(path, key, value):
7070
first_name__iexact=data["first_name"],
7171
last_name__iexact=data["last_name"],
7272
position__iexact=data["position"],
73+
defaults={
74+
"first_name": data["first_name"],
75+
"last_name": data["last_name"],
76+
"position": data["position"],
77+
},
7378
)
7479
created_country_directory_ids.append(country_directory.pk)
7580
# NOTE: Deleting the country directory which are not available in the source
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 4.2.13 on 2024-07-12 06:26
2+
3+
from django.db import migrations
4+
5+
6+
def delete_ids(apps, schema_editor):
7+
CountryCapacityStrengthening = apps.get_model("api", "CountryCapacityStrengthening")
8+
CountryCapacityStrengthening.objects.all().delete()
9+
10+
11+
class Migration(migrations.Migration):
12+
13+
dependencies = [
14+
("api", "0211_alter_countrydirectory_unique_together_and_more"),
15+
]
16+
17+
operations = [
18+
migrations.RunPython(delete_ids, migrations.RunPython.noop),
19+
migrations.AlterUniqueTogether(
20+
name="countrycapacitystrengthening",
21+
unique_together={("assessment_type", "assessment_code")},
22+
),
23+
]

api/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ class AssessmentType(models.IntegerChoices):
375375
assessment_type = models.IntegerField(verbose_name=_("Country Assessment Type"), choices=AssessmentType.choices)
376376
branch_name = models.CharField(verbose_name=_("Branch Name"), max_length=255, null=True, blank=True)
377377

378+
class Meta:
379+
unique_together = ("assessment_type", "assessment_code")
380+
378381
def __str__(self):
379382
return f"{self.country.name} - {self.assessment_code} - {self.year}"
380383

0 commit comments

Comments
 (0)