def redir(request, slug): d = datetime.date.today() # if there's a ref tag = "" if request.GET.has_key('ref'): tag = request.GET["ref"].strip() # catch referer domain referer = "" if request.META.has_key('HTTP_REFERER'): referer = urlparse.urlparse(request.META['HTTP_REFERER'].strip()).netloc # find the media we're linking to media = get_object_or_404(Media.objects.all(),slug=slug) r = Redis(settings.REDIS_SERVER) # create a set of keys token = "%s/%s/%s/%s" % (slug, d.isoformat(), referer, tag) if r.sadd("daily:%s:tokens" % d.isoformat(), token): # new token # add to set of tokens for this slug r.sadd("slug:%s:tokens" % slug, token) token_id = r.incr('next.token.id') # store the token_id for this token r.set('token:%s:id' % token, token_id) # store the token by token_id r.set('token:%s:token' % token_id, token) # increment clicks for this token r.incr("token:%s:clicks" % token) # encode hash_code = "%s" % media.encode(r.get("token:%s:id" % token)) redir_url = media.affil_url.replace("HASH_CODE",hash_code) # GeoIP g = GeoIP() loc = g.country(request.META['REMOTE_ADDR']) country = loc['country_code'].lower() #raise Exception("%s" % redir_url) if (country == "us"): return HttpResponseRedirect(redir_url) else: country_url = "/%s/" % country redir_url = media.url.replace('/us/',country_url) return HttpResponseRedirect(redir_url)
def get_geoip_coords(ip): """ Returns (lat, lon) for the ip if it's valid or None """ if not settings.DOLPHIN_USE_GIS: return None #assume that GIS is set up properly with a setting for the path #if not, it'll raise an exception from django.contrib.gis.utils.geoip import GeoIP gip = GeoIP() return gip.lat_lon(ip)
def find_users_near_me(request): from django.contrib.gis.utils.geoip import GeoIP g = GeoIP() remote_ip = request.META['REMOTE_ADDR'] if remote_ip == '127.0.0.1': remote_ip = get_my_ip() remote_location = g.city(remote_ip) map_position = {} map_position['lat'] = remote_location['latitude'] map_position['lng'] = remote_location['longitude'] map_position['zoom'] = 11 return render_to_response("geo/find_users_near_me.html", { "city": remote_location['city'], "map_position": map_position, }, context_instance=RequestContext(request))
real_ip = request.META['REMOTE_ADDR'] except Exception: # Unknown IP pass # real_ip = "198.245.113.94" if real_ip == "127.0.0.1": real_ip = "198.245.113.94" if real_ip.startswith("10.2."): logger.warning("Bad ip. Request = %s" % request.META) return real_ip geoip = GeoIP() class LocationByIP(object): def __init__(self, request): self.request = request self.ip = get_real_ip(request) @property def country(self): code = geoip.country_code(self.ip) return get_or_none(Country, code=code) @property def canadian_region(self): region_data = geoip.region_by_addr(self.ip)
def test_general(self): us = Country.objects.get(code='US') us_group = LocationGroup(name='US-CA/NY') us_group.save() us_group.locations.create(country=us, us_state='CA') us_group.locations.create(country=us, us_state='NY') handler = LocationHandler() handler.save() handler.location_groups.add(us_group) handler.prepare() req = HttpRequest() self.assertFalse(handler.check(req)) geo = GeoIP() for ip, result in [('', False ), ('127.0.0.1', False), ('212.58.244.70', False), #bbc ('173.194.37.104', True), #google, CA ('207.17.33.246', True), #gs, NY? ('72.32.191.88', False), #rackspace, TX? ]: req.META = {'REMOTE_ADDR': ip } self.assertTrue(handler.check(req) == result, msg='%s expected to return %s (%s)' % (ip, result, geo.city(ip))) uk_group = LocationGroup(name='UK') uk_group.save() uk_group.locations.create(country=Country.objects.get(code='GB')) handler = LocationHandler() handler.save() handler.location_groups.add(us_group) handler.location_groups.add(uk_group) handler.prepare() for ip, result in [('212.58.244.70', True), #bbc ('173.194.37.104', True), #google, CA ('207.17.33.246', True), #gs, NY? ('72.32.191.88', False), #rackspace, TX? ]: req.META = {'HTTP_X_FORWARDED_FOR': ip } self.assertTrue(handler.check(req) == result, msg='%s expected to return %s (%s)' % (ip, result, geo.city(ip))) handler_gb = LocationHandler() handler_gb.save() handler_gb.location_groups.add(uk_group) handler_gb.prepare() for ip, result in [('212.58.244.70', True), #bbc ('173.194.37.104', False), #google, CA ('207.17.33.246', False), #gs, NY? ('72.32.191.88', False), #rackspace, TX? ]: req.META = {'REMOTE_ADDR': ip } self.assertTrue(handler_gb.check(req) == result, msg='%s expected to return %s (%s)' % (ip, result, geo.city(ip))) us_all = LocationGroup(name='US-ALL') us_all.save() us_all.locations.create(country=us) handler = LocationHandler() handler.save() handler.location_groups.add(us_all) handler.prepare() for ip, result in [('212.58.244.70', False), #bbc ('173.194.37.104', True), #google, CA ('207.17.33.246', True), #gs, NY? ('72.32.191.88', True), #rackspace, TX? ]: req.META = {'REMOTE_ADDR': ip } self.assertTrue(handler.check(req) == result, msg='%s expected to return %s (%s)' % (ip, result, geo.city(ip)))