Skip to content

Commit 561df68

Browse files
committed
Use Don_Code for country identification in ingest_ns_directory job
1 parent e96df48 commit 561df68

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

api/management/commands/ingest_ns_directory.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,44 @@ def postprocessor(path, key, value):
4747
created_country_directory_ids = []
4848
dict_data = xmltodict.parse(response.content, postprocessor=postprocessor)
4949
for data in dict_data["ArrayOfNationalSocietiesContacts"]["NationalSocietiesContacts"]:
50-
country_name = data["CON_country"] if isinstance(data["CON_country"], str) else None
51-
if country_name is not None:
50+
Don_Code = data.get("Don_Code") if isinstance(data.get("Don_Code"), str) else None
51+
country_name = data.get("CON_country") if isinstance(data.get("CON_country"), str) else None
52+
country = None
53+
if Don_Code:
54+
# Prefer matching country by Don_Code via fdrs field.
55+
country = Country.objects.filter(fdrs=Don_Code).first()
56+
if not country:
57+
logger.info("No country matched Don_Code %s; falling back to CON_country", Don_Code)
58+
if not country and country_name is not None:
5259
country = Country.objects.filter(name__icontains=country_name).first()
53-
if country:
54-
added += 1
55-
data = {
56-
"first_name": (
57-
data["CON_firstName"]
58-
if isinstance(data["CON_firstName"], str) and data["CON_firstName"] is not None
59-
else None
60-
),
61-
"last_name": (
62-
data["CON_lastName"]
63-
if isinstance(data["CON_lastName"], str) and data["CON_lastName"] is not None
64-
else None
65-
),
66-
"position": data["CON_title"],
67-
"country": country,
68-
}
69-
country_directory, _ = CountryDirectory.objects.get_or_create(
70-
country=country,
71-
first_name__iexact=data["first_name"],
72-
last_name__iexact=data["last_name"],
73-
position__iexact=data["position"],
74-
defaults={
75-
"first_name": data["first_name"],
76-
"last_name": data["last_name"],
77-
"position": data["position"],
78-
},
79-
)
80-
created_country_directory_ids.append(country_directory.pk)
60+
if country:
61+
added += 1
62+
data = {
63+
"first_name": (
64+
data["CON_firstName"]
65+
if isinstance(data["CON_firstName"], str) and data["CON_firstName"] is not None
66+
else None
67+
),
68+
"last_name": (
69+
data["CON_lastName"]
70+
if isinstance(data["CON_lastName"], str) and data["CON_lastName"] is not None
71+
else None
72+
),
73+
"position": data["CON_title"],
74+
"country": country,
75+
}
76+
country_directory, _ = CountryDirectory.objects.get_or_create(
77+
country=country,
78+
first_name__iexact=data["first_name"],
79+
last_name__iexact=data["last_name"],
80+
position__iexact=data["position"],
81+
defaults={
82+
"first_name": data["first_name"],
83+
"last_name": data["last_name"],
84+
"position": data["position"],
85+
},
86+
)
87+
created_country_directory_ids.append(country_directory.pk)
8188
# NOTE: Deleting the country directory which are not available in the source
8289
CountryDirectory.objects.exclude(id__in=created_country_directory_ids).delete()
8390
text_to_log = "%s Ns Directory added" % added

0 commit comments

Comments
 (0)