def submit_address(self): # build a lookup address from the form fields lookup_address = '' for field in ['street','city','country']: val = self.data.get(field,None) if val is not None: lookup_address = '%s %s' % (lookup_address,val) lookup_address = lookup_address.strip() # convert the address to latituse and longitude lat,lng = helpers.convert_address_to_lat_lon(lookup_address) if lat is not None and lng is not None: # create/update a GeoAddress objectand return it return helpers.get_or_create_geo_address(lookup_address,lat,lng) return None
def post_geoaddress(request): if request.method == 'POST': # retrieve the user input data = { 'country': request.data.get('country'), 'city': request.data.get('city'), 'street': request.data.get('street'), } # create an address string address = helpers.create_address(data) # try to convert the address string into coordinates (lat,lng) lat, lng = helpers.convert_address_to_lat_lon(address) # if coordinates do exists, handle the object creation/update if lat is not None and lng is not None: # update existing object or create a new one # the filter is based on the coordinates obj = helpers.get_or_create_geo_address(lat, lng) if obj.id is not None: # updating the object - do not forget to set partial=True partial = True else: # new object partial = False data['latitude'] = lat data['longitude'] = lng # increment the number of visits data['count'] = obj.count + 1 # use serialzer to save the object serializer = GeoAddressSerializer(obj, data=data, partial=partial) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) # on serializtion error return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) # on google conversion error return Response(status=status.HTTP_400_BAD_REQUEST)
def post_geoaddress(request): if request.method == 'POST': # retrieve the user input data = {'country': request.data.get('country'), 'city': request.data.get('city'), 'street': request.data.get('street'), } # create an address string address = helpers.create_address(data) # try to convert the address string into coordinates (lat,lng) lat,lng = helpers.convert_address_to_lat_lon(address) # if coordinates do exists, handle the object creation/update if lat is not None and lng is not None: # update existing object or create a new one # the filter is based on the coordinates obj = helpers.get_or_create_geo_address(lat,lng) if obj.id is not None: # updating the object - do not forget to set partial=True partial = True else: # new object partial = False data['latitude'] = lat data['longitude'] = lng # increment the number of visits data['count'] = obj.count + 1 # use serialzer to save the object serializer = GeoAddressSerializer(obj,data=data,partial=partial) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) # on serializtion error return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) # on google conversion error return Response(status=status.HTTP_400_BAD_REQUEST)