@@ -200,16 +200,7 @@ def _parse_integer(integer):
200
200
201
201
row_errors = {}
202
202
project_districts = []
203
- if len (district_names ) == 0 :
204
- project_country = Country .objects .filter (name__iexact = country_name ).first ()
205
- if project_country is None :
206
- row_errors ['project_country' ] = [f'Given country "{ country_name } " is not available.' ]
207
- else :
208
- project_districts = list (project_country .district_set .all ())
209
-
210
- if len (project_districts ) == 0 :
211
- row_errors ['project_districts' ] = [f'There is no district for given country "{ country_name } " in database.' ]
212
- else :
203
+ if district_names :
213
204
project_districts = list (District .objects .filter (
214
205
reduce (
215
206
lambda acc , item : acc | item ,
@@ -223,16 +214,22 @@ def _parse_integer(integer):
223
214
if len (project_districts ) == len (district_names ):
224
215
project_country = project_districts [0 ].country
225
216
else :
217
+ # District list can be empty. If not empty, we get country name from the first one.
226
218
project_country = None
227
- # A validation error will be raised. This is just a custom message
228
- row_errors ['project_districts' ] = ['Given districts/regions are not available.' ]
219
+ else :
220
+ project_country = Country .objects .filter (name__iexact = country_name ).first ()
221
+
222
+ # A validation error will be raised. This is just a custom message
223
+ if project_country is None :
224
+ row_errors ['project_country' ] = [f'Country "{ country_name } " is not available.' ]
225
+
229
226
230
227
project_sectortags = []
231
228
if tag_names :
232
229
project_sectortags = list (SectorTag .objects .filter (
233
230
reduce (lambda acc , item : acc | item ,
234
231
[Q (title = title ) for title in tag_names ],
235
- )
232
+ )
236
233
).all ())
237
234
# Check if all tag_names is available in db
238
235
if len (project_sectortags ) != len (tag_names ):
@@ -241,7 +238,8 @@ def _parse_integer(integer):
241
238
242
239
if reporting_ns is None :
243
240
row_errors ['reporting_ns' ] = [f'Given country "{ reporting_ns_name } " is not available.' ]
244
- if disaster_type is None :
241
+ # Optional, but can be invalid
242
+ if disaster_type is None and disaster_type_name != '' :
245
243
row_errors ['disaster_type' ] = [f'Given disaster type "{ disaster_type_name } " is not available.' ]
246
244
247
245
project = Project (
0 commit comments