Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if 'REMOTE_ADDR' in request.META:
- # We get latitude and geoip from GeoIP using the IP
- # in the request
- g = GeoIP()
- coords = g.lat_lon(request.META['REMOTE_ADDR'])
- if coords is None:
- # Return first city
- # TODO: in debug we need to set a fixed value
- return self.all()[0]
- else:
- latitude, longitude = coords
- distance = constants.MAX_CITY_DISTANCE
- for city in self.all():
- # we need to search in all the cities the lat/long data
- # and compare to get the closer city
- distance_to_city = sqrt((city.latitude-latitude)**2 +
- (city.longitude-longitude)**2)
- # If this city is closer to the closest distance
- # Then set this city as closest
- if distance_to_city < distance:
- closer_city = city
- distance = distance_to_city
- # Return closest city
- return closer_city
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement