@@ -200,16 +200,7 @@ def _parse_integer(integer):
200200
201201 row_errors = {}
202202 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 :
213204 project_districts = list (District .objects .filter (
214205 reduce (
215206 lambda acc , item : acc | item ,
@@ -223,16 +214,22 @@ def _parse_integer(integer):
223214 if len (project_districts ) == len (district_names ):
224215 project_country = project_districts [0 ].country
225216 else :
217+ # District list can be empty. If not empty, we get country name from the first one.
226218 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+
229226
230227 project_sectortags = []
231228 if tag_names :
232229 project_sectortags = list (SectorTag .objects .filter (
233230 reduce (lambda acc , item : acc | item ,
234231 [Q (title = title ) for title in tag_names ],
235- )
232+ )
236233 ).all ())
237234 # Check if all tag_names is available in db
238235 if len (project_sectortags ) != len (tag_names ):
@@ -241,7 +238,8 @@ def _parse_integer(integer):
241238
242239 if reporting_ns is None :
243240 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 != '' :
245243 row_errors ['disaster_type' ] = [f'Given disaster type "{ disaster_type_name } " is not available.' ]
246244
247245 project = Project (
0 commit comments