From 575e54fe99363320c44ebd806e21c09c44eb746c Mon Sep 17 00:00:00 2001 From: SkanderBM Date: Mon, 27 Jan 2020 19:06:59 +0100 Subject: [PATCH 1/3] add option to force ip #25 --- README.md | 2 ++ django_ip_geolocation/settings.py | 1 + django_ip_geolocation/utils.py | 7 +++++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index db1bdb5..d792091 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ IP_GEOLOCATION_SETTINGS = { 'ENABLE_REQUEST_HOOK': True, 'ENABLE_RESPONSE_HOOK': True, 'ENABLE_COOKIE': False, + 'FORCE_IP_ADDR': None, } ``` @@ -70,6 +71,7 @@ Those are the default settings, that will be overwritten by those set in `settin | `ENABLE_REQUEST_HOOK` | Enable or disable hook on request | `True` (bool) | | `ENABLE_RESPONSE_HOOK` | Enable or disable hook on request | `True` (bool) | | `ENABLE_COOKIE` | Enable or disable geolocation data in cookie | `False` (bool) | +| `FORCE_IP_ADDR` | Force ip address, radher than using visitor ip | `None` (string) | ### Available Backends * `django_ip_geolocation.backends.IPGeolocationAPI` : (Default) Using https://ipgeolocationapi.com/ diff --git a/django_ip_geolocation/settings.py b/django_ip_geolocation/settings.py index 27a8fac..0cdd180 100644 --- a/django_ip_geolocation/settings.py +++ b/django_ip_geolocation/settings.py @@ -10,6 +10,7 @@ 'ENABLE_REQUEST_HOOK': True, 'ENABLE_RESPONSE_HOOK': True, 'ENABLE_COOKIE': False, + 'FORCE_IP_ADDR': None, } CUSTOM_IP_GEOLOCATION_SETTINGS = getattr(settings, 'IP_GEOLOCATION_SETTINGS', {}) # noqa: E501 diff --git a/django_ip_geolocation/utils.py b/django_ip_geolocation/utils.py index 93ee25a..a0a24f2 100644 --- a/django_ip_geolocation/utils.py +++ b/django_ip_geolocation/utils.py @@ -1,6 +1,6 @@ """Helper functions.""" -from django_ip_geolocation import settings from django.utils.module_loading import import_string # noqa: E501 # pylint: disable=import-error +from django_ip_geolocation import settings def _get_remote_ip_from_request(request): @@ -38,7 +38,10 @@ def get_geolocation(request): :return: Geolocation data :rtype: dict """ - ip_addr = _get_remote_ip_from_request(request) + ip_addr = settings.IP_GEOLOCATION_SETTINGS.get('FORCE_IP_ADDR', None) + if not ip_addr: + ip_addr = _get_remote_ip_from_request(request) + backend_cls = _get_geolocation_backend_cls() backend_instance = backend_cls(ip_addr) From 5238adb7c0bdf675de154b318f7884c1eff7766e Mon Sep 17 00:00:00 2001 From: SkanderBM Date: Mon, 27 Jan 2020 19:09:58 +0100 Subject: [PATCH 2/3] add option to force ip - typo #25 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d792091..923c8ef 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Those are the default settings, that will be overwritten by those set in `settin | `ENABLE_REQUEST_HOOK` | Enable or disable hook on request | `True` (bool) | | `ENABLE_RESPONSE_HOOK` | Enable or disable hook on request | `True` (bool) | | `ENABLE_COOKIE` | Enable or disable geolocation data in cookie | `False` (bool) | -| `FORCE_IP_ADDR` | Force ip address, radher than using visitor ip | `None` (string) | +| `FORCE_IP_ADDR` | Force ip address, rather than using visitor ip | `None` (string) | ### Available Backends * `django_ip_geolocation.backends.IPGeolocationAPI` : (Default) Using https://ipgeolocationapi.com/ From d853112e7b1e064f938b9d5d706395a7c641ab90 Mon Sep 17 00:00:00 2001 From: SkanderBM Date: Mon, 27 Jan 2020 19:36:49 +0100 Subject: [PATCH 3/3] fix enable cookie settings for decorator #26 --- django_ip_geolocation/decorators.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/django_ip_geolocation/decorators.py b/django_ip_geolocation/decorators.py index 5d01911..3508ffc 100644 --- a/django_ip_geolocation/decorators.py +++ b/django_ip_geolocation/decorators.py @@ -17,7 +17,9 @@ def inner(request): # noqa: E501 try: enable_request = _settings.get('ENABLE_REQUEST_HOOK') enable_response = _settings.get('ENABLE_RESPONSE_HOOK') - if not enable_request and not enable_response: + 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) @@ -31,7 +33,7 @@ def inner(request): # noqa: E501 header = _settings.get('RESPONSE_HEADER') response[header] = geolocation - if _settings.get('ENABLE_COOKIE', False): + if enable_cookie: set_cookie(response, geolocation) return response