Пример #1
0
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))
Пример #2
0
    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)))