Skip to content

Commit

Permalink
rename geoaware_tags to geoaware, and allow auto include of tags, and…
Browse files Browse the repository at this point in the history
… added a defaults file
  • Loading branch information
un33k committed Nov 22, 2012
1 parent 091ebf6 commit 4572ed6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ To access the GoeIP data via template tags:
The following filters are available for extracting specific GeoIP data in the templates.
Requirement: ``django.core.context_processors.request`` must be present in your `TEMPLATE_CONTEXT_PROCESSORS`.

{% load geoware_tags %}
{% load geoware %} # Or set `GEOIP_INCLUDE_TEMPLATE_TAGS = True` in your settings to auto include tags

{{ request|geo_country_name }}
{{ request|geo_country_code }}
Expand All @@ -128,13 +128,17 @@ To run the tests against the current environment:
python manage.py test geoaware # ToDo

Note: if you are running on localhost in DEBUG mode, then you can add to your settings.py
`GEOAWARE_DEBUG_DOMAIN_OR_IP = 'google.com'` OR `GEOAWARE_DEBUG_DOMAIN_OR_IP = 'some valid public ip'`.
`GEOIP_DEBUG_DOMAIN_OR_IP = 'google.com'` OR `GEOIP_DEBUG_DOMAIN_OR_IP = 'some valid public ip'`.
This is for testing purposes ONLY.


Changelog
=========

0.4.0
-----
* allow auto tag inclusion if flag is set

0.3.0
-----
* force cookie to be saved
Expand Down
13 changes: 12 additions & 1 deletion geoaware/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@

__version__ = '0.3.0'
__version__ = '0.4.0'

import defaults

GEOIP_INCLUDE_TEMPLATE_TAGS = getattr(defaults, 'GEOIP_INCLUDE_TEMPLATE_TAGS')
if GEOIP_INCLUDE_TEMPLATE_TAGS:
from django import template
application_tags = [
'geoaware.templatetags.geoaware',
]
for t in application_tags: template.add_to_builtins(t)

12 changes: 12 additions & 0 deletions geoaware/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.conf import settings

try:
from django.contrib.gis.geoip import GeoIP
except ImportError:
from django.contrib.gis.utils import GeoIP

GEOIP_CACHE_METHOD = getattr(settings, 'GEOIP_CACHE_METHOD', GeoIP.GEOIP_INDEX_CACHE)
GEOIP_INCLUDE_TEMPLATE_TAGS = getattr(settings, 'GEOIP_INCLUDE_TEMPLATE_TAGS', False)

if getattr(settings, 'GEOIP_DEBUG_DOMAIN_OR_IP', ''):
GEOIP_DEBUG_DOMAIN_OR_IP = getattr(settings, 'GEOIP_DEBUG_DOMAIN_OR_IP')
5 changes: 3 additions & 2 deletions geoaware/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.gis.geoip import GeoIP
except ImportError:
from django.contrib.gis.utils import GeoIP
import defaults

# 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}')
Expand Down Expand Up @@ -40,9 +41,9 @@ def get_geo_info(request):
'country_code': '',
'country_name': '',
}
fqdn_or_ip = getattr(settings, 'GEOAWARE_DEBUG_DOMAIN_OR_IP', get_ip_address(request))
fqdn_or_ip = getattr(defaults, 'GEOIP_DEBUG_DOMAIN_OR_IP', get_ip_address(request))
if fqdn_or_ip:
cache_method = getattr(settings, "GEOIP_CACHE_METHOD", GeoIP.GEOIP_INDEX_CACHE)
cache_method = getattr(defaults, 'GEOIP_CACHE_METHOD')
geo = GeoIP(cache=cache_method)
try:
ginfo = geo.city(fqdn_or_ip)
Expand Down
5 changes: 3 additions & 2 deletions geoaware/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf import settings
from geo import get_geo_info
from geo import get_ip_address

import defaults

class GeoAwareSessionMiddleware(object):
""" Saves geo info in session if GeoIP is configured for city or country.
Expand All @@ -24,7 +25,7 @@ class GeoAwareSessionMiddleware(object):

def process_request(self, request):
""" Save or update geo info in session """
fqdn_or_ip = getattr(settings, 'GEOAWARE_DEBUG_DOMAIN_OR_IP', get_ip_address(request))
fqdn_or_ip = getattr(defaults, 'GEOIP_DEBUG_DOMAIN_OR_IP', get_ip_address(request))
try:
if request.session['geo_info']['fqdn_or_ip'] == fqdn_or_ip:
return None
Expand Down
File renamed without changes.

0 comments on commit 4572ed6

Please sign in to comment.