diff --git a/api/management/commands/ingest_ns_capacity.py b/api/management/commands/ingest_ns_capacity.py index edffdfcb5..a1237a2de 100644 --- a/api/management/commands/ingest_ns_capacity.py +++ b/api/management/commands/ingest_ns_capacity.py @@ -1,6 +1,7 @@ import requests from django.conf import settings from django.core.management.base import BaseCommand +from django.db import transaction from sentry_sdk.crons import monitor from api.logger import logger @@ -12,6 +13,7 @@ class Command(BaseCommand): help = "Add ns contact details" @monitor(monitor_slug=SentryMonitor.INGEST_NS_CAPACITY) + @transaction.atomic def handle(self, *args, **kwargs): logger.info("Starting NS Contacts") @@ -32,6 +34,7 @@ def handle(self, *args, **kwargs): resp_ocac_data = resp_ocac.json() ocaa_count = 0 + country_capacity_ids = [] for item in resp_ocac_data: ocaa_count += 1 data = { @@ -42,7 +45,22 @@ def handle(self, *args, **kwargs): "country": Country.objects.filter(fdrs=item["NsId"]).first(), "assessment_type": CountryCapacityStrengthening.AssessmentType.OCAC, } - CountryCapacityStrengthening.objects.create(**data) + country_capacity_ocac, created = CountryCapacityStrengthening.objects.get_or_create( + country=data["country"], + assessment_code=data["assessment_code"], + assessment_type=data["assessment_type"], + defaults={ + "submission_date": data["submission_date"], + "url": data["url"], + "year": data["year"], + }, + ) + if not created: + country_capacity_ocac.submission_date = data["submission_date"] + country_capacity_ocac.url = data["url"] + country_capacity_ocac.year = data["year"] + country_capacity_ocac.save(update_fields=["submission_date", "url", "year"]) + country_capacity_ids.append(country_capacity_ocac.pk) text_to_log = "%s Ns capacity added" % ocaa_count logger.info(text_to_log) @@ -70,4 +88,23 @@ def handle(self, *args, **kwargs): "assessment_type": CountryCapacityStrengthening.AssessmentType.BOCA, "branch_name": item["BranchName"], } - CountryCapacityStrengthening.objects.create(**data) + country_capacity_boca, created = CountryCapacityStrengthening.objects.get_or_create( + country=data["country"], + assessment_code=data["assessment_code"], + assessment_type=data["assessment_type"], + defaults={ + "year": data["year"], + "branch_name": data["branch_name"], + "submission_date": data["submission_date"], + "url": data["url"], + }, + ) + if not created: + country_capacity_boca.submission_date = data["submission_date"] + country_capacity_boca.url = data["url"] + country_capacity_boca.year = data["year"] + country_capacity_boca.branch_name = data["branch_name"] + country_capacity_boca.save(update_fields=["submission_date", "url", "year", "branch_name"]) + country_capacity_ids.append(country_capacity_boca.pk) + # Delete the country capacity strengthening which are not available in the source + CountryCapacityStrengthening.objects.exclude(id__in=country_capacity_ids).delete() diff --git a/api/management/commands/ingest_ns_directory.py b/api/management/commands/ingest_ns_directory.py index e7f2b55b1..69c47ab9a 100644 --- a/api/management/commands/ingest_ns_directory.py +++ b/api/management/commands/ingest_ns_directory.py @@ -70,6 +70,11 @@ def postprocessor(path, key, value): first_name__iexact=data["first_name"], last_name__iexact=data["last_name"], position__iexact=data["position"], + defaults={ + "first_name": data["first_name"], + "last_name": data["last_name"], + "position": data["position"], + }, ) created_country_directory_ids.append(country_directory.pk) # NOTE: Deleting the country directory which are not available in the source diff --git a/api/migrations/0212_alter_countrycapacitystrengthening_unique_together.py b/api/migrations/0212_alter_countrycapacitystrengthening_unique_together.py new file mode 100644 index 000000000..3236c09b5 --- /dev/null +++ b/api/migrations/0212_alter_countrycapacitystrengthening_unique_together.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.13 on 2024-07-12 06:26 + +from django.db import migrations + + +def delete_ids(apps, schema_editor): + CountryCapacityStrengthening = apps.get_model("api", "CountryCapacityStrengthening") + CountryCapacityStrengthening.objects.all().delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ("api", "0211_alter_countrydirectory_unique_together_and_more"), + ] + + operations = [ + migrations.RunPython(delete_ids, migrations.RunPython.noop), + migrations.AlterUniqueTogether( + name="countrycapacitystrengthening", + unique_together={("assessment_type", "assessment_code")}, + ), + ] diff --git a/api/models.py b/api/models.py index 576266cc8..2a3b0d927 100644 --- a/api/models.py +++ b/api/models.py @@ -375,6 +375,9 @@ class AssessmentType(models.IntegerChoices): assessment_type = models.IntegerField(verbose_name=_("Country Assessment Type"), choices=AssessmentType.choices) branch_name = models.CharField(verbose_name=_("Branch Name"), max_length=255, null=True, blank=True) + class Meta: + unique_together = ("assessment_type", "assessment_code") + def __str__(self): return f"{self.country.name} - {self.assessment_code} - {self.year}"