3131)
3232
3333
34+ class LocationSerializer (serializers .Serializer ):
35+ lat = serializers .FloatField (required = True )
36+ lng = serializers .FloatField (required = True )
37+
38+
3439class GeneralMedicalServiceSerializer (serializers .ModelSerializer ):
3540 class Meta :
3641 model = GeneralMedicalService
@@ -191,7 +196,7 @@ class LocalUnitDetailSerializer(serializers.ModelSerializer):
191196 type_details = LocalUnitTypeSerializer (source = "type" , read_only = True )
192197 level_details = LocalUnitLevelSerializer (source = "level" , read_only = True )
193198 health = HealthDataSerializer (required = False , allow_null = True )
194- location_details = serializers .SerializerMethodField ()
199+ location_geojson = serializers .SerializerMethodField ()
195200 visibility_display = serializers .CharField (source = "get_visibility_display" , read_only = True )
196201 validated = serializers .BooleanField (read_only = True )
197202
@@ -220,13 +225,13 @@ class Meta:
220225 "level" ,
221226 "health" ,
222227 "visibility_display" ,
223- "location_details " ,
228+ "location_geojson " ,
224229 "type_details" ,
225230 "level_details" ,
226231 "country_details" ,
227232 )
228233
229- def get_location_details (self , unit ) -> dict :
234+ def get_location_geojson (self , unit ) -> dict :
230235 return json .loads (unit .location .geojson )
231236
232237
@@ -235,10 +240,11 @@ class PrivateLocalUnitDetailSerializer(NestedCreateMixin, NestedUpdateMixin):
235240 type_details = LocalUnitTypeSerializer (source = "type" , read_only = True )
236241 level_details = LocalUnitLevelSerializer (source = "level" , read_only = True )
237242 health = HealthDataSerializer (required = False , allow_null = True )
238- location_details = serializers .SerializerMethodField (read_only = True )
243+ # NOTE: location_geojson contains the geojson of the location
244+ location_geojson = serializers .SerializerMethodField (read_only = True )
239245 visibility_display = serializers .CharField (source = "get_visibility_display" , read_only = True )
240246 validated = serializers .BooleanField (read_only = True )
241- location_json = serializers . JSONField (required = True , write_only = True )
247+ location_json = LocationSerializer (required = True )
242248 location = serializers .CharField (required = False )
243249 modified_by_details = LocalUnitMiniUserSerializer (source = "modified_by" , read_only = True )
244250 created_by_details = LocalUnitMiniUserSerializer (source = "created_by" , read_only = True )
@@ -272,7 +278,7 @@ class Meta:
272278 "level" ,
273279 "health" ,
274280 "visibility_display" ,
275- "location_details " ,
281+ "location_geojson " ,
276282 "type_details" ,
277283 "level_details" ,
278284 "country_details" ,
@@ -288,7 +294,7 @@ class Meta:
288294 "is_locked" ,
289295 )
290296
291- def get_location_details (self , unit ) -> dict :
297+ def get_location_geojson (self , unit ) -> dict :
292298 return json .loads (unit .location .geojson )
293299
294300 def get_version_id (self , resource ):
@@ -319,8 +325,6 @@ def create(self, validated_data):
319325 location_json = validated_data .pop ("location_json" )
320326 lat = location_json .get ("lat" )
321327 lng = location_json .get ("lng" )
322- if not lat and not lng :
323- raise serializers .ValidationError (gettext ("Combination of lat/lon is required" ))
324328 input_point = Point (lng , lat )
325329 if country .bbox :
326330 country_json = json .loads (country .countrygeoms .geom .geojson )
@@ -349,8 +353,6 @@ def update(self, instance, validated_data):
349353 location_json = validated_data .pop ("location_json" )
350354 lat = location_json .get ("lat" )
351355 lng = location_json .get ("lng" )
352- if not lat and not lng :
353- raise serializers .ValidationError (gettext ("Combination of lat/lon is required" ))
354356 input_point = Point (lng , lat )
355357 if country .bbox :
356358 country_json = json .loads (country .countrygeoms .geom .geojson )
@@ -377,7 +379,8 @@ def update(self, instance, validated_data):
377379
378380
379381class LocalUnitSerializer (serializers .ModelSerializer ):
380- location_details = serializers .SerializerMethodField ()
382+ # NOTE: location_geojson contains the geojson of the location
383+ location_geojson = serializers .SerializerMethodField ()
381384 country_details = LocalUnitCountrySerializer (source = "country" , read_only = True )
382385 type_details = LocalUnitTypeSerializer (source = "type" , read_only = True )
383386 health_details = MiniHealthDataSerializer (read_only = True , source = "health" )
@@ -390,7 +393,7 @@ class Meta:
390393 "country" ,
391394 "local_branch_name" ,
392395 "english_branch_name" ,
393- "location_details " ,
396+ "location_geojson " ,
394397 "type" ,
395398 "validated" ,
396399 "address_loc" ,
@@ -401,12 +404,13 @@ class Meta:
401404 "health_details" ,
402405 )
403406
404- def get_location_details (self , unit ) -> dict :
407+ def get_location_geojson (self , unit ) -> dict :
405408 return json .loads (unit .location .geojson )
406409
407410
408411class PrivateLocalUnitSerializer (serializers .ModelSerializer ):
409- location_details = serializers .SerializerMethodField ()
412+ # NOTE: location_geojson contains the geojson of the location
413+ location_geojson = serializers .SerializerMethodField ()
410414 country_details = LocalUnitCountrySerializer (source = "country" , read_only = True )
411415 type_details = LocalUnitTypeSerializer (source = "type" , read_only = True )
412416 health_details = MiniHealthDataSerializer (read_only = True , source = "health" )
@@ -421,7 +425,7 @@ class Meta:
421425 "country" ,
422426 "local_branch_name" ,
423427 "english_branch_name" ,
424- "location_details " ,
428+ "location_geojson " ,
425429 "type" ,
426430 "validated" ,
427431 "address_loc" ,
@@ -439,7 +443,7 @@ class Meta:
439443 "is_locked" ,
440444 )
441445
442- def get_location_details (self , unit ) -> dict :
446+ def get_location_geojson (self , unit ) -> dict :
443447 return json .loads (unit .location .geojson )
444448
445449
0 commit comments