-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from rednaks/master
1.6.0 release
- Loading branch information
Showing
20 changed files
with
275 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[run] | ||
branch = true | ||
|
||
[report] | ||
omit = | ||
*site-packages* | ||
*tests* | ||
*.tox* | ||
show_missing = True | ||
exclude_lines = | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals, absolute_import | ||
|
||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings") | ||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,10 @@ def readme(): | |
|
||
setup( | ||
name='django-ip-geolocation', | ||
version='1.5.3', | ||
version='1.6.0', | ||
author='Skander Ben Mahmoud', | ||
author_email='[email protected]', | ||
packages=find_packages(exclude=("tests", "docs")), | ||
packages=find_packages(exclude=("*tests*", "docs")), | ||
url='https://github.com/rednaks/django-ip-geolocation', | ||
license='MIT', | ||
description="Django request/response hook (Middleware and Decorator) to geolocate visitors using their IP address", # noqa: E501 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Django test module.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Mocked backend module.""" | ||
from django_ip_geolocation.backends.base import GeolocationBackend | ||
|
||
|
||
class BackendMock(GeolocationBackend): | ||
"""BackendMock backend implementation.""" | ||
|
||
def geolocate(self): | ||
"""Mock api call.""" | ||
self._raw_data = { | ||
"continent": "Europe", | ||
"country_code": "49", | ||
"name": "Germany", | ||
"geo": { | ||
"latitude": 51.165691, | ||
"longitude": 10.451526 | ||
}, | ||
"currency_code": "EUR" | ||
} | ||
|
||
def _parse(self): | ||
"""Parse raw data.""" | ||
self._continent = self._raw_data.get('continent') | ||
self._country = { | ||
'code': self._raw_data.get('alpha2'), | ||
'name': self._raw_data.get('name') | ||
} | ||
self._geo_data = self._raw_data.get('geo') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
coverage | ||
mock | ||
flake8 | ||
flake8-docstrings | ||
bandit | ||
twine | ||
tox | ||
codecov | ||
tox-travis | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Django settings.""" | ||
# -*- coding: utf-8 | ||
from __future__ import unicode_literals, absolute_import | ||
|
||
# import django | ||
|
||
DEBUG = True | ||
USE_TZ = True | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = '-%n*2l!dqp6wkjn44kbv5y=2m6en@l495gb9@&$#o89%8oy75g' | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': ':memory:', | ||
} | ||
} | ||
|
||
ROOT_URLCONF = 'tests.urls' | ||
|
||
INSTALLED_APPS = [ | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django_ip_geolocation.middleware.IpGeolocationMiddleware', | ||
] | ||
|
||
|
||
IP_GEOLOCATION_SETTINGS = { | ||
'BACKEND': 'tests.backend_mock.BackendMock', | ||
'BACKEND_API_KEY': '', | ||
'BACKEND_EXTRA_PARAMS': {}, | ||
'BACKEND_USERNAME': '', | ||
'RESPONSE_HEADER': 'X-IP-Geolocation', | ||
'ENABLE_REQUEST_HOOK': True, | ||
'ENABLE_RESPONSE_HOOK': True, | ||
'ENABLE_COOKIE': False, | ||
'FORCE_IP_ADDR': None, | ||
'USER_CONSENT_VALIDATOR': None | ||
} | ||
|
||
SITE_ID = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
|
||
from django.http import HttpResponse | ||
|
||
from django.test import TestCase | ||
import logging | ||
from django_ip_geolocation.settings import IP_GEOLOCATION_SETTINGS | ||
from tests.utils import change_settings | ||
|
||
|
||
class GeolocationTests(TestCase): | ||
|
||
def setUp(self): | ||
self._default_settings = IP_GEOLOCATION_SETTINGS.copy() | ||
|
||
|
||
def test_geolocation_in_response(self): | ||
response = self.client.get('/test_geolocation/') | ||
self.assertTrue(response.has_header('X-IP-Geolocation')) | ||
if response.has_header('X-IP-Geolocation'): | ||
print(response['X-IP-Geolocation']) | ||
|
||
@change_settings(settings={'ENABLE_REQUEST_HOOK': False}) | ||
def test_geolocation_in_response_when_request_disabled(self): | ||
response = self.client.get('/test_geolocation/') | ||
self.assertTrue(response.has_header('X-IP-Geolocation')) | ||
if response.has_header('X-IP-Geolocation'): | ||
print(response['X-IP-Geolocation']) | ||
|
||
@change_settings(settings={'ENABLE_RESPONSE_HOOK': False}) | ||
def test_geolocation_not_in_response_when_response_disabled(self): | ||
response = self.client.get('/test_geolocation/') | ||
self.assertFalse(response.has_header('X-IP-Geolocation')) | ||
|
||
def test_cookie_not_in_response(self): | ||
response = self.client.get('/test_geolocation/') | ||
self.assertIsNone(response.cookies.get('geolocation')) | ||
|
||
@change_settings(settings={'ENABLE_COOKIE': True}) | ||
def test_cookie_in_response(self): | ||
response = self.client.get('/test_geolocation/') | ||
self.assertIsNotNone(response.cookies.get('geolocation')) | ||
|
||
@change_settings(settings={'USER_CONSENT_VALIDATOR': 'tests.user_consent_mock.check_user_consent'}) | ||
def test_user_not_consented(self): | ||
response = self.client.get('/test_geolocation/') | ||
self.assertFalse(response.has_header('X-IP-Geolocation')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#import django | ||
from django.conf.urls import url | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
url('^test_geolocation/$', views.geolocation_test) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
def check_user_consent(request): | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django_ip_geolocation.settings import IP_GEOLOCATION_SETTINGS | ||
|
||
|
||
def change_settings(settings): | ||
def decorator(func): | ||
def wrapper(*args, **kwargs): | ||
default_settings = args[0]._default_settings | ||
IP_GEOLOCATION_SETTINGS.update(settings) | ||
func(*args, **kwargs) | ||
IP_GEOLOCATION_SETTINGS.update(default_settings) | ||
return wrapper | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.http import HttpResponse | ||
from django.views.decorators.csrf import ensure_csrf_cookie | ||
|
||
|
||
@ensure_csrf_cookie | ||
def geolocation_test(request): | ||
""" | ||
Initialize request. | ||
""" | ||
response = HttpResponse("hello skander") | ||
return response |
Oops, something went wrong.