Skip to content

Commit

Permalink
Fix NS directory issue and capacity issue
Browse files Browse the repository at this point in the history
Delete CountryDirectory
  • Loading branch information
susilnem committed Jul 12, 2024
1 parent 5063a01 commit 567d7bf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
41 changes: 39 additions & 2 deletions api/management/commands/ingest_ns_capacity.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")

Expand All @@ -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 = {
Expand All @@ -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)
Expand Down Expand Up @@ -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()
5 changes: 5 additions & 0 deletions api/management/commands/ingest_ns_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")},
),
]
3 changes: 3 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down

0 comments on commit 567d7bf

Please sign in to comment.