Skip to content

Commit 5289ecc

Browse files
committed
Update data read from table
1 parent 08bc421 commit 5289ecc

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

dref/common_utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,30 @@ def parse_contact_information(items: List[Any]):
107107
continue
108108
result.append(data)
109109
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

dref/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
get_paragraphs_data,
3030
parse_disaster_category,
3131
parse_contact_information,
32+
parse_people,
33+
parse_country,
34+
parse_currency,
3235
)
3336

3437

@@ -146,7 +149,7 @@ def f(r, c, i=0, as_list=False):
146149
except(IndexError, ValueError):
147150
pass
148151
try:
149-
data['amount_requested'] = parse_string_to_int(cells(1, 1, 1))
152+
data['amount_requested'] = parse_currency(cells(1, 1, 1))
150153
except(IndexError, ValueError):
151154
pass
152155
try:
@@ -162,11 +165,11 @@ def f(r, c, i=0, as_list=False):
162165
except(IndexError, ValueError):
163166
pass
164167
try:
165-
data['num_affected'] = parse_string_to_int(cells(3, 1))
168+
data['num_affected'] = parse_people(cells(3, 1))
166169
except(IndexError, ValueError):
167170
pass
168171
try:
169-
data['num_assisted'] = parse_string_to_int(cells(3, 2))
172+
data['num_assisted'] = parse_people(cells(3, 2))
170173
except(IndexError, ValueError):
171174
pass
172175
try:
@@ -182,10 +185,11 @@ def f(r, c, i=0, as_list=False):
182185
except(IndexError, ValueError):
183186
pass
184187
try:
185-
data['operation_timeframe'] = parse_int(cells(5, 3))
188+
data['operation_timeframe'] = parse_people(cells(5, 3))
186189
except(IndexError, ValueError):
187190
pass
188-
country = Country.objects.filter(name__icontains=cells(6, 0, 1)).first()
191+
print(parse_country(cells(6, 0)), "cccc")
192+
country = Country.objects.filter(name__icontains=parse_country(cells(6, 0))).first()
189193
if country is None:
190194
raise serializers.ValidationError('A valid country name is required')
191195
data['country'] = country
@@ -195,7 +199,7 @@ def f(r, c, i=0, as_list=False):
195199
for d in new_district:
196200
try:
197201
district = District.objects.filter(
198-
country__name__icontains=cells(6, 0, 1),
202+
country__name__icontains=parse_country(cells(6, 0)),
199203
name__icontains=d
200204
).first()
201205
except District.DoesNotExist:

0 commit comments

Comments
 (0)