Пример #1
0
def voter_location_retrieve_from_ip_for_api(request, ip_address=''):
    """
    Used by the api
    :param ip_address:
    :return:
    """
    x_forwarded_for = request.META.get('X-Forwarded-For')
    http_x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if not positive_value_exists(ip_address):
        ip_address = get_ip_from_headers(request)

    if not positive_value_exists(ip_address):
        # return HttpResponse('missing ip_address request parameter', status=400)
        response_content = {
            'success': False,
            'status': 'LOCATION_RETRIEVE_IP_ADDRESS_REQUEST_PARAMETER_MISSING',
            'voter_location_found': False,
            'voter_location': '',
            'ip_address': ip_address,
            'x_forwarded_for': x_forwarded_for,
            'http_x_forwarded_for': http_x_forwarded_for,
        }

        return response_content

    g = GeoIP()
    location = g.city(ip_address)
    if location is None:
        # Consider this alternate way of responding to front end:
        # return HttpResponse('no matching location for IP address {}'.format(ip_address), status=400)
        response_content = {
            'success': True,
            'status': 'LOCATION_NOT_FOUND',
            'voter_location_found': False,
            'voter_location': '',
            'ip_address': ip_address,
            'x_forwarded_for': x_forwarded_for,
            'http_x_forwarded_for': http_x_forwarded_for,
        }
    else:
        response_content = {
            'success': True,
            'status': 'LOCATION_FOUND',
            'voter_location_found': True,
            'voter_location': '{0[city]}, {0[region]} {0[postal_code]}'.format(location),
            'ip_address': ip_address,
            'x_forwarded_for': x_forwarded_for,
            'http_x_forwarded_for': http_x_forwarded_for,
        }

    return response_content
Пример #2
0
def voter_location_retrieve_from_ip_view(request):
    """
    Take the IP address and return a location (voterLocationRetrieveFromIP)
    :param request:
    :return:
    """
    ip_address = request.GET.get('ip_address') or get_ip_from_headers(request)
    if ip_address is None:
        # return HttpResponse('missing ip_address request parameter', status=400)
        response_content = {
            'success': False,
            'status': 'missing ip_address request parameter',
            'voter_location_found': False,
            'voter_location': '',
            'ip_address': ip_address,
        }

        return HttpResponse(json.dumps(response_content), content_type='application/json')

    return voter_location_retrieve_from_ip_for_api(ip_address=ip_address)
def voter_location_retrieve_from_ip_for_api(request, ip_address=''):
    """
    Used by the api voterLocationRetrieveFromIP
    :param ip_address:
    :return:
    """
    x_forwarded_for = request.META.get('X-Forwarded-For')
    http_x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')

    if not positive_value_exists(ip_address):
        ip_address = get_ip_from_headers(request)

    if not positive_value_exists(ip_address):
        # return HttpResponse('missing ip_address request parameter', status=400)
        response_content = {
            'success': False,
            'status': 'LOCATION_RETRIEVE_IP_ADDRESS_REQUEST_PARAMETER_MISSING',
            'voter_location_found': False,
            'voter_location': '',
            'ip_address': ip_address,
            'x_forwarded_for': x_forwarded_for,
            'http_x_forwarded_for': http_x_forwarded_for,
        }

        return response_content

    g = GeoIP()
    location = g.city(ip_address)
    if location is None:
        # Consider this alternate way of responding to front end:
        # return HttpResponse('no matching location for IP address {}'.format(ip_address), status=400)
        response_content = {
            'success': True,
            'status': 'LOCATION_NOT_FOUND',
            'voter_location_found': False,
            'voter_location': '',
            'ip_address': ip_address,
            'x_forwarded_for': x_forwarded_for,
            'http_x_forwarded_for': http_x_forwarded_for,
        }
    else:
        voter_location = ''
        if 'city' in location and location['city']:
            voter_location += location['city']
            if ('region' in location and location['region']) or \
                    ('postal_code' in location and location['postal_code']):
                voter_location += ', '
        if 'region' in location and location['region']:
            voter_location += location['region']
            if 'postal_code' in location and location['postal_code']:
                voter_location += ' '
        if 'postal_code' in location and location['postal_code']:
            voter_location += location['postal_code']
        if positive_value_exists(voter_location):
            status = 'LOCATION_FOUND'
            voter_location_found = True
        else:
            status = 'IP_FOUND_BUT_LOCATION_NOT_RETURNED'
            voter_location_found = False
        response_content = {
            'success': True,
            'status': status,
            'voter_location_found': voter_location_found,
            'voter_location': voter_location,
            'ip_address': ip_address,
            'x_forwarded_for': x_forwarded_for,
            'http_x_forwarded_for': http_x_forwarded_for,
        }

    return response_content