Skip to content

Commit d2c54bf

Browse files
Merge pull request #1537 from IFRCGo/fix/dref-import
Fix/dref import
2 parents 9f891f4 + eb55210 commit d2c54bf

File tree

4 files changed

+480
-255
lines changed

4 files changed

+480
-255
lines changed

dref/assessment_utils.py

Lines changed: 107 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from api.models import (
1313
DisasterType,
1414
Country,
15+
District,
1516
)
1617

1718
from .common_utils import (
@@ -23,6 +24,10 @@
2324
get_table_data,
2425
get_paragraphs_data,
2526
parse_disaster_category,
27+
parse_contact_information,
28+
parse_people,
29+
parse_country,
30+
parse_currency,
2631
)
2732

2833

@@ -117,6 +122,7 @@ def get_nth(items: List[Any], index=0):
117122

118123
def get_table_cells(table_idx):
119124
table = tables[table_idx]
125+
120126
def f(r, c, i=0, as_list=False):
121127
if as_list:
122128
return table[r][c]
@@ -131,24 +137,42 @@ def f(r, c, i=0, as_list=False):
131137

132138
cells = get_table_cells(0)
133139
data['appeal_code'] = cells(1, 0)
134-
data['amount_requested'] = parse_string_to_int(cells(1, 1, 1))
140+
data['amount_requested'] = parse_currency(cells(1, 1))
135141
data['disaster_category'] = parse_disaster_category(cells(1, 2))
136142
data['disaster_type'] = parse_disaster_type(cells(1, 4))
137143
data['glide_code'] = cells(3, 0)
138-
data['num_affected'] = parse_string_to_int(cells(3, 1))
139-
data['num_assisted'] = parse_string_to_int(cells(3, 2))
144+
data['num_affected'] = parse_people(cells(3, 1))
145+
data['num_assisted'] = parse_people(cells(3, 2))
140146
data['type_of_onset'] = parse_type_of_onset(cells(5, 0))
141147
data['date_of_approval'] = parse_date(cells(5, 1))
142148
data['end_date'] = parse_date(cells(5, 2))
143-
data['operation_timeframe'] = parse_int(cells(5, 3))
144-
country = Country.objects.filter(name__icontains=cells(6, 0, 1)).first()
149+
data['operation_timeframe'] = parse_people(cells(5, 3))
150+
country = Country.objects.filter(name__icontains=parse_country(cells(6, 0))).first()
145151
if country is None:
146152
raise serializers.ValidationError('A valid country name is required')
147153
data['country'] = country
148154

149-
# TODO: Check for District
150-
description = paragraphs[7] or []
151-
data['event_description'] = ''.join(description) if description else None
155+
district = cells(6, 2)
156+
district_list = []
157+
if district:
158+
new_district = district.split(',')
159+
for d in new_district:
160+
try:
161+
district = District.objects.filter(
162+
country__name__icontains=parse_country(cells(6, 0)),
163+
name__icontains=d
164+
).first()
165+
except District.DoesNotExist:
166+
pass
167+
if district is None:
168+
continue
169+
district_list.append(district)
170+
try:
171+
if paragraphs[6][0] == 'What happened, where and when?':
172+
description = paragraphs[7] or []
173+
data['event_description'] = ''.join(description) if description else None
174+
except IndexError:
175+
pass
152176

153177
# National Society Actions
154178
cells = get_table_cells(1)
@@ -207,26 +231,42 @@ def f(r, c, i=0, as_list=False):
207231
national_authorities = cells(1, 1, as_list=True)
208232
data['national_authorities'] = ''.join(national_authorities) if national_authorities else None
209233

210-
un_and_other_actors = cells(2, 1, as_list=True)
234+
un_and_other_actors = cells(2, 0, as_list=True)
211235
data['un_or_other_actor'] = ''.join(un_and_other_actors) if un_and_other_actors else None
212236

213237
coordination_mechanism = cells(3, 1, as_list=True)
214238
data['major_coordination_mechanism'] = ''.join(coordination_mechanism) if coordination_mechanism else None
215239

216240
# Operational Strategy
217-
operation_description = paragraphs[20] or []
218-
data['operation_objective'] = ''.join(operation_description) if operation_description else None
241+
try:
242+
if paragraphs[18][0] == 'Overall objective of the operation':
243+
operation_description = paragraphs[19] or []
244+
data['operation_objective'] = ''.join(operation_description) if operation_description else None
245+
except IndexError:
246+
pass
219247

220248
# targeting strategy
221-
response_description = paragraphs[23] or []
222-
data['response_strategy'] = ''.join(response_description) if response_description else None
249+
try:
250+
if paragraphs[20][0] == 'Response strategy rationale':
251+
response_description = paragraphs[21] or []
252+
data['response_strategy'] = ''.join(response_description) if response_description else None
253+
except IndexError:
254+
pass
223255

224256
# Targeting Strategy
225-
people_assisted_description = paragraphs[27] or []
226-
data['people_assisted'] = ''.join(people_assisted_description) if people_assisted_description else None
257+
try:
258+
if paragraphs[23][0] == 'Who will be targeted through this operation?':
259+
people_assisted_description = paragraphs[24] or []
260+
data['people_assisted'] = ''.join(people_assisted_description) if people_assisted_description else None
261+
except IndexError:
262+
pass
227263

228-
selection_criteria_description = paragraphs[31] or []
229-
data['selection_criteria'] = ''.join(selection_criteria_description) if selection_criteria_description else None
264+
try:
265+
if paragraphs[26][0] == 'Explain the selection criteria for the targeted population':
266+
selection_criteria_description = paragraphs[27] or []
267+
data['selection_criteria'] = ''.join(selection_criteria_description) if selection_criteria_description else None
268+
except IndexError:
269+
pass
230270

231271
# Targeting Population
232272
cells = get_table_cells(4)
@@ -291,45 +331,62 @@ def f(r, c, i=0, as_list=False):
291331
planned_intervention.append(planned)
292332

293333
# About Support Service
294-
human_resource_description = paragraphs[55] or []
295-
data['human_resource'] = ''.join(human_resource_description) if human_resource_description else None
296-
297-
surge_personnel_deployed_description = paragraphs[57] or []
298-
data['surge_personnel_deployed'] = ''.join(surge_personnel_deployed_description) if surge_personnel_deployed_description else None
299-
300-
national_society_contact = paragraphs[63] or []
301-
data['national_society_contact_title'] = get_nth(national_society_contact, 3)
302-
data['national_society_contact_email'] = get_nth(national_society_contact, 5)
303-
data['national_society_contact_phone_number'] = get_nth(national_society_contact, 7)
304-
data['national_society_contact_name'] = get_nth(national_society_contact, 0)
305-
306-
ifrc_appeal_manager = paragraphs[64] or []
307-
data['ifrc_appeal_manager_title'] = get_nth(ifrc_appeal_manager, 3)
308-
data['ifrc_appeal_manager_email'] = get_nth(ifrc_appeal_manager, 5)
309-
data['ifrc_appeal_manager_phone_number'] = get_nth(ifrc_appeal_manager, 7)
310-
data['ifrc_appeal_manager_name'] = get_nth(ifrc_appeal_manager, 0)
311-
312-
ifrc_project_manager = paragraphs[65] or []
313-
data['ifrc_project_manager_title'] = get_nth(ifrc_project_manager, 3)
314-
data['ifrc_project_manager_email'] = get_nth(ifrc_project_manager, 5)
315-
data['ifrc_project_manager_phone_number'] = get_nth(ifrc_project_manager, 7)
316-
data['ifrc_project_manager_name'] = get_nth(ifrc_project_manager, 0)
334+
try:
335+
if paragraphs[50][0] == 'How many volunteers and staff involved in the response? Briefly describe their role.':
336+
human_resource_description = paragraphs[51] or []
337+
data['human_resource'] = ''.join(human_resource_description) if human_resource_description else None
338+
except IndexError:
339+
pass
340+
341+
try:
342+
if paragraphs[52][0] == 'Will surge personnel be deployed? Please provide the role profile needed.':
343+
surge_personnel_deployed_description = paragraphs[53] or []
344+
data['surge_personnel_deployed'] = ''.join(surge_personnel_deployed_description) if surge_personnel_deployed_description else None
345+
except IndexError:
346+
pass
347+
348+
try:
349+
national_society_contact = parse_contact_information(paragraphs[60] or [])
350+
data['national_society_contact_title'] = get_nth(national_society_contact, 2)
351+
data['national_society_contact_email'] = get_nth(national_society_contact, 4)
352+
data['national_society_contact_phone_number'] = get_nth(national_society_contact, 6)
353+
data['national_society_contact_name'] = get_nth(national_society_contact, 0)
354+
except IndexError:
355+
pass
356+
357+
try:
358+
ifrc_appeal_manager = parse_contact_information(paragraphs[61] or [])
359+
data['ifrc_appeal_manager_title'] = get_nth(ifrc_appeal_manager, 2)
360+
data['ifrc_appeal_manager_email'] = get_nth(ifrc_appeal_manager, 4)
361+
data['ifrc_appeal_manager_phone_number'] = get_nth(ifrc_appeal_manager, 6)
362+
data['ifrc_appeal_manager_name'] = get_nth(ifrc_appeal_manager, 0)
363+
except IndexError:
364+
pass
365+
366+
try:
367+
ifrc_project_manager = parse_contact_information(paragraphs[62] or [])
368+
data['ifrc_project_manager_title'] = get_nth(ifrc_project_manager, 2)
369+
data['ifrc_project_manager_email'] = get_nth(ifrc_project_manager, 4)
370+
data['ifrc_project_manager_phone_number'] = get_nth(ifrc_project_manager, 6)
371+
data['ifrc_project_manager_name'] = get_nth(ifrc_project_manager, 0)
372+
except IndexError:
373+
pass
317374

318375
try:
319-
ifrc_emergency = paragraphs[66] or []
320-
data['ifrc_emergency_title'] = get_nth(ifrc_emergency, 3)
321-
data['ifrc_emergency_email'] = get_nth(ifrc_emergency, 5)
322-
data['ifrc_emergency_phone_number'] = get_nth(ifrc_emergency, 7)
376+
ifrc_emergency = parse_contact_information(paragraphs[63] or [])
377+
data['ifrc_emergency_title'] = get_nth(ifrc_emergency, 2)
378+
data['ifrc_emergency_email'] = get_nth(ifrc_emergency, 4)
379+
data['ifrc_emergency_phone_number'] = get_nth(ifrc_emergency, 6)
323380
data['ifrc_emergency_name'] = get_nth(ifrc_emergency, 0)
324381
except IndexError:
325382
# FIXME: raise validation error
326383
pass
327384

328385
try:
329-
media = paragraphs[67] or []
330-
data['media_contact_title'] = get_nth(media, 3)
331-
data['media_contact_email'] = get_nth(media, 5)
332-
data['media_contact_phone_number'] = get_nth(media, 7)
386+
media = parse_contact_information(paragraphs[64] or [])
387+
data['media_contact_title'] = get_nth(media, 2)
388+
data['media_contact_email'] = get_nth(media, 4)
389+
data['media_contact_phone_number'] = get_nth(media, 6)
333390
data['media_contact_name'] = get_nth(media, 0)
334391
except IndexError:
335392
# FIXME: raise validation error
@@ -343,4 +400,6 @@ def f(r, c, i=0, as_list=False):
343400
dref.planned_interventions.add(*planned_intervention)
344401
dref.national_society_actions.add(*national_societies)
345402
dref.risk_security.add(*mitigation_list)
403+
if len(district_list) > 0 and None not in district_list:
404+
dref.district.add(*district_list)
346405
return dref

dref/common_utils.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def get_table_data(doc):
7373
tables.append(rowdata)
7474
return tables
7575

76+
7677
def get_paragraphs_data(doc):
7778
document = docx.Document(doc)
7879
return [[y.text for y in x._element.xpath('.//w:t')] for x in document.paragraphs]
@@ -86,3 +87,50 @@ def parse_disaster_category(disaster_category):
8687
elif disaster_category == 'Red':
8788
return Dref.DisasterCategory.RED
8889
return None
90+
91+
92+
def parse_contact_information(items: List[Any]):
93+
result = []
94+
for data in items:
95+
if data in [
96+
'National Society contact: ',
97+
'IFRC Appeal Manager: ',
98+
'IFRC Project Manager: ',
99+
'IFRC focal point for the emergency: ',
100+
'Media Contact: '
101+
', ',
102+
'• ',
103+
'Media Contact: ',
104+
'• IFRC Project Manager: ',
105+
'• IFRC Project Manager:',
106+
]:
107+
continue
108+
result.append(data)
109+
return result
110+
111+
112+
def parse_people(string):
113+
try:
114+
if string:
115+
people = string.split(' ')
116+
return int(people[0])
117+
except (IndexError, ValueError):
118+
pass
119+
120+
121+
def parse_country(country):
122+
try:
123+
if country:
124+
new_country = country.split(' ')
125+
return new_country[1]
126+
except (IndexError, ValueError):
127+
pass
128+
129+
130+
def parse_currency(currency):
131+
try:
132+
if currency:
133+
new_currency = currency.split(' ')
134+
return int(new_currency[1])
135+
except (IndexError, ValueError):
136+
pass

0 commit comments

Comments
 (0)