-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
388 additions
and
37 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 |
---|---|---|
@@ -1,35 +1,5 @@ | ||
*.py[cod] | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Packages | ||
*.egg | ||
*.egg-info | ||
dist | ||
build | ||
eggs | ||
parts | ||
bin | ||
var | ||
sdist | ||
develop-eggs | ||
.installed.cfg | ||
lib | ||
lib64 | ||
|
||
# Installer logs | ||
pip-log.txt | ||
|
||
# Unit test / coverage reports | ||
.coverage | ||
.tox | ||
nosetests.xml | ||
|
||
# Translations | ||
*.mo | ||
|
||
# Mr Developer | ||
.mr.developer.cfg | ||
.project | ||
.pydevproject | ||
*.pyc | ||
MANIFEST | ||
dist/ | ||
build/ | ||
*.egg-info/ |
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 @@ | ||
language: python | ||
python: | ||
- "2.6" | ||
- "2.7" | ||
env: | ||
- DJANGO_VERSION=1.3 | ||
- DJANGO_VERSION=1.4 | ||
install: | ||
- pip install -q Django==$DJANGO_VERSION --use-mirrors | ||
- pip install -q -r requirements.txt --use-mirrors | ||
script: python manage.py test geoaware |
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 |
---|---|---|
@@ -1,2 +1,135 @@ | ||
django-geoaware | ||
=============== | ||
Django GeoAware Application | ||
==================== | ||
|
||
A Django application that can include GeoIP info in the session and/or the context. | ||
|
||
**Author:** Val Neekman, [ [email protected], @vneekman] | ||
|
||
Overview | ||
======== | ||
|
||
Django GeoAware provides a middleware as well as a context processor for including | ||
GeoInfo into the session and/or the context. | ||
|
||
How to install | ||
================== | ||
|
||
Please ensure that you have properly configured your system as per: | ||
https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoip/ | ||
|
||
1. easy_install django-geoaware | ||
2. pip install django-geoaware | ||
3. git clone https://github.com/un33k/django-geoaware | ||
a. cd django-geoaware | ||
b. run python setup.py | ||
4. wget https://github.com/un33k/django-geoaware/zipball/master | ||
a. unzip the downloaded file | ||
b. cd into django-geoaware-* directory | ||
c. run python setup.py | ||
|
||
How to use | ||
================= | ||
|
||
Include 'geoware' in your INSTALLED_APPS somewhere after 'django.contrib.gis'. | ||
|
||
INSTALLED_APPS = [ | ||
'some apps go here', | ||
'django.contrib.gis', | ||
'some other apps can go here', | ||
'geoaware', | ||
'some other apps can go here', | ||
] | ||
|
||
To include Geo Info in session: | ||
|
||
Include geoaware middleware in your MIDDLEWARE_CLASSES after the session middleware | ||
|
||
MIDDLEWARE_CLASSES = [ | ||
'django.middleware.common.CommonMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'some middleware can go here', | ||
'geoaware.middleware.GeoSessionMiddleware', | ||
'some more middleware can go here', | ||
] | ||
|
||
The GeoInfo would be available in request.session['geo_info'] | ||
To include Geo Info in context: | ||
|
||
Include geoware context processor in your TEMPLATE_CONTEXT_PROCESSORS after 'django.core.context_processors.request'. | ||
|
||
TEMPLATE_CONTEXT_PROCESSORS = [ | ||
'django.contrib.auth.context_processors.auth', | ||
'some context processor can go here', | ||
'django.core.context_processors.request', | ||
'some context processor can go here', | ||
'geoaware.context_processors.geoaware', | ||
'some context processor can go here', | ||
] | ||
|
||
The data will be available in geo_info. | ||
|
||
To access the geo data: | ||
|
||
geo_info is a dictionary that includes the following: | ||
geo_info = { | ||
'fqdn_or_ip': '', | ||
'city': '', | ||
'continent_code': '', | ||
'region': '', | ||
'charset': 0, | ||
'area_code': 0, | ||
'longitude': 0.0, | ||
'country_code3': '', | ||
'latitude': 0.0, | ||
'postal_code': None, | ||
'dma_code': 0, | ||
'country_code': '', | ||
'country_name': '', | ||
} | ||
if some fields are not available they are left unpopulated. | ||
|
||
|
||
Running the tests | ||
================= | ||
|
||
To run the tests against the current environment: | ||
|
||
python manage.py test geoaware # ToDo | ||
|
||
|
||
Changelog | ||
========= | ||
|
||
0.0.1 | ||
----- | ||
* initial release | ||
|
||
|
||
License | ||
======= | ||
|
||
Copyright © Val Neekman | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
Redistributions in binary form must reproduce the above copyright notice, this | ||
list of conditions and the following disclaimer in the documentation and/or | ||
other materials provided with the distribution. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
|
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,2 @@ | ||
|
||
__version__ = '0.1.0' |
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,27 @@ | ||
from django.conf import settings | ||
from geo import get_geo_info | ||
from geo import get_ip_address | ||
|
||
def geoaware(request): | ||
""" Bring GeoInfo into the context if GeoIP is configured for city or country. | ||
geo_info = { | ||
'fqdn_or_ip': '', | ||
'city': '', | ||
'continent_code': '', | ||
'region': '', | ||
'charset': 0, | ||
'area_code': 0, | ||
'longitude': 0.0, | ||
'country_code3': '', | ||
'latitude': 0.0, | ||
'postal_code': None, | ||
'dma_code': 0, | ||
'country_code': '', | ||
'country_name': '', | ||
} | ||
""" | ||
cxt = {} | ||
fqdn_or_ip = getattr(settings, 'DEBUG_DOMAIN_OR_IP', get_ip_address(request)) | ||
geo_info = get_geo_info(fqdn_or_ip) | ||
cxt = {'geo_info': geo_info} | ||
return cxt |
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,59 @@ | ||
import re | ||
from django.conf import settings | ||
|
||
try: | ||
from django.contrib.gis.geoip import GeoIP | ||
except ImportError: | ||
from django.contrib.gis.utils import GeoIP | ||
|
||
# by no means this is a perfect IP regex | ||
IP_RE = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') | ||
|
||
def get_ip_address(request): | ||
""" Make best attempt to get client's real IP or return the loopback IP """ | ||
|
||
lo = '127.0.0.1' | ||
get_ip = lambda x,y: request.META.get(x,y) | ||
ip_addr = get_ip('HTTP_X_FORWARDED_FOR', get_ip('HTTP_X_REAL_IP', get_ip('REMOTE_ADDR', lo))) | ||
try: | ||
ip_addr = IP_RE.match(ip_addr).group(0) | ||
except: | ||
ip_addr = lo | ||
return ip_addr | ||
|
||
|
||
def get_geo_info(fqdn_or_ip): | ||
""" Get GeoInfo - Relies on Django to raise exception on improper configuration """ | ||
|
||
geo_info = { | ||
'fqdn_or_ip': '', | ||
'city': '', | ||
'continent_code': '', | ||
'region': '', | ||
'charset': 0, | ||
'area_code': 0, | ||
'longitude': 0.0, | ||
'country_code3': '', | ||
'latitude': 0.0, | ||
'postal_code': None, | ||
'dma_code': 0, | ||
'country_code': '', | ||
'country_name': '', | ||
} | ||
if fqdn_or_ip: | ||
geo = GeoIP() | ||
try: | ||
ginfo = geo.city(fqdn_or_ip) | ||
geo_info.update(ginfo) | ||
except: | ||
try: | ||
ginfo = geo.country(fqdn_or_ip) | ||
geo_info.update(ginfo) | ||
except: | ||
pass | ||
geo_info['fqdn_or_ip'] = fqdn_or_ip | ||
|
||
return geo_info | ||
|
||
|
||
|
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,35 @@ | ||
from django.conf import settings | ||
from geo import get_geo_info | ||
from geo import get_ip_address | ||
|
||
class GeoAwareSessionMiddleware(object): | ||
""" Saves geo info in session if GeoIP is configured for city or country. | ||
geo_info = { | ||
'fqdn_or_ip': '', | ||
'city': '', | ||
'continent_code': '', | ||
'region': '', | ||
'charset': 0, | ||
'area_code': 0, | ||
'longitude': 0.0, | ||
'country_code3': '', | ||
'latitude': 0.0, | ||
'postal_code': None, | ||
'dma_code': 0, | ||
'country_code': '', | ||
'country_name': '', | ||
} | ||
""" | ||
|
||
def process_request(self, request): | ||
""" Save or update geo info in session """ | ||
fqdn_or_ip = getattr(settings, 'DEBUG_DOMAIN_OR_IP', get_ip_address(request)) | ||
try: | ||
if request.session['geo_info']['fqdn_or_ip'] == fqdn_or_ip: | ||
return None | ||
except: | ||
pass | ||
geo_info = get_geo_info(fqdn_or_ip) | ||
request.session['geo_info'] = geo_info | ||
return None |
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,2 @@ | ||
# Look Django, I am an app | ||
|
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 @@ | ||
# ToDo |
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 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsettings") | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
echo -e "\nRunning: (pep8 --show-source --show-pep8 --select=errors --testsuite=.)\n\n" | ||
pep8 --show-source --show-pep8 --select=errors --testsuite=./ | ||
echo -e "\n\n" |
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,2 @@ | ||
Django>=1.3 | ||
|
Oops, something went wrong.