def process_response(self, request, response):
        """Process the response."""
        try:
            if not _settings.get('ENABLE_RESPONSE_HOOK') and \
                    not _settings.get('ENABLE_COOKIE'):
                return response

            if not is_user_consented(request):
                return response

            if self._geolocation_data is None:
                self._get_geolocation(request)

            if _settings.get('ENABLE_RESPONSE_HOOK'):
                # Response hook is enabled
                header = _settings.get('RESPONSE_HEADER')
                response[header] = self._geolocation_data

            if _settings.get('ENABLE_COOKIE'):
                cleaned_geolocation_data = clean_geolocation_data(
                    self._geolocation_data, ['raw_data'])
                set_cookie(response, cleaned_geolocation_data)

        except Exception:
            logging.error("Couldn't geolocate ip", exc_info=True)

        return response
Example #2
0
    def inner(request):  # noqa: E501
        try:
            enable_request = _settings.get('ENABLE_REQUEST_HOOK')
            enable_response = _settings.get('ENABLE_RESPONSE_HOOK')
            enable_cookie = _settings.get('ENABLE_COOKIE', False)

            if not enable_request and not (enable_response or enable_cookie):
                return view_func(request)

            geolocation = get_geolocation(request)

            if enable_request:
                request.geolocation = geolocation

            response = view_func(request)

            if enable_response:
                header = _settings.get('RESPONSE_HEADER')
                response[header] = geolocation

            if enable_cookie:
                cleaned_geolocation_data = clean_geolocation_data(
                    geolocation, ['raw_data'])
                set_cookie(response, cleaned_geolocation_data)

            return response
        except Exception:
            logging.error('Django Ip Geolocation Error', exc_info=True)
            return view_func(request)
Example #3
0
    def process_request(self, request):
        """Process the request."""
        try:
            if not _settings.get('ENABLE_REQUEST_HOOK'):
                return

            self._get_geolocation(request)
            request.geolocation = self._geolocation_data
        except Exception:
            logging.error("Couldn't geolocate ip", exc_info=True)
    def geolocate(self):
        """Call ip2location.com api."""
        api_key = _settings.get('BACKEND_API_KEY')
        extra_params = _settings.get('BACKEND_EXTRA_PARAMS', {})

        if not api_key:
            msg = "BACKEND_API_KEY is required. Please provide an API_KEY in IP_GEOLOCATION_SETTINGS"  # noqa: E501
            raise Exception(msg)

        payload = {
            'key': api_key,
            'ip': self._ip,
            'package': extra_params.get('package', 'WS24')
        }

        url = 'https://api.ip2location.com/v2/'
        res = requests.get(url, params=payload)
        if res.ok:
            self._raw_data = res.json()
        else:
            raise Exception(res.text)
 def wrapper(*args, **kwargs):
     default_settings = args[0]._default_settings
     IP_GEOLOCATION_SETTINGS.update(settings)
     func(*args, **kwargs)
     IP_GEOLOCATION_SETTINGS.update(default_settings)
 def setUp(self):
     self._default_settings = IP_GEOLOCATION_SETTINGS.copy()