11import requests
22from django .conf import settings
33from django .core .management .base import BaseCommand
4+ from django .db import transaction
45from sentry_sdk .crons import monitor
56
67from 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 ()
0 commit comments