Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. if 'REMOTE_ADDR' in request.META:
  2.             # We get latitude and geoip from GeoIP using the IP
  3.             # in the request
  4.             g = GeoIP()
  5.             coords = g.lat_lon(request.META['REMOTE_ADDR'])
  6.  
  7.             if coords is None:
  8.                 # Return first city
  9.                 # TODO: in debug we need to set a fixed value
  10.                 return self.all()[0]
  11.             else:
  12.                 latitude, longitude = coords
  13.  
  14.             distance = constants.MAX_CITY_DISTANCE
  15.             for city in self.all():
  16.                 # we need to search in all the cities the lat/long data
  17.                 # and compare to get the closer city
  18.                 distance_to_city = sqrt((city.latitude-latitude)**2 +
  19.                         (city.longitude-longitude)**2)
  20.  
  21.                 # If this city is closer to the closest distance
  22.                 # Then set this city as closest
  23.                 if distance_to_city < distance:
  24.                     closer_city = city
  25.                     distance = distance_to_city
  26.  
  27.             # Return closest city
  28.             return closer_city
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement