Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
rednaks committed Jan 27, 2020
2 parents 1264b51 + ed504d5 commit 15a5516
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ IP_GEOLOCATION_SETTINGS = {
'ENABLE_REQUEST_HOOK': True,
'ENABLE_RESPONSE_HOOK': True,
'ENABLE_COOKIE': False,
'FORCE_IP_ADDR': None,
}

```
Expand All @@ -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, rather than using visitor ip | `None` (string) |

### Available Backends
* `django_ip_geolocation.backends.IPGeolocationAPI` : (Default) Using https://ipgeolocationapi.com/
Expand Down
6 changes: 4 additions & 2 deletions django_ip_geolocation/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions django_ip_geolocation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions django_ip_geolocation/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 15a5516

Please sign in to comment.