Skip to content

Commit

Permalink
add template tags to app
Browse files Browse the repository at this point in the history
  • Loading branch information
un33k committed Nov 21, 2012
1 parent e993142 commit f9919bb
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,36 @@ To access the GeoIP data:
# If some fields are not available they are left unpopulated.
```

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`.

```html
{% load geoware_tags %}
{{ request|geo_country_name }}
{{ request|geo_country_code }}
{{ request|geo_country_code3 }}
{{ request|geo_city }}
{{ request|geo_latitude }}
{{ request|geo_longitude }}
{{ request|geo_postal_code }}
{{ request|geo_region }}
{{ request|geo_dma_code }}
{{ request|geo_area_code }}
{{ request|geo_fqdn_or_ip }}
{{ request|geo_charset }}
```

Running the tests
=================

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 you can add to your settings.py `GEOAWARE_DEBUG_DOMAIN_OR_IP = 'google.com'` OR `GEOAWARE_DEBUG_DOMAIN_OR_IP = 'some valid public ip'`. This is for testing purposes ONLY.


Changelog
=========
Expand Down
3 changes: 1 addition & 2 deletions geoaware/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def geoaware(request):
}
"""
cxt = {}
fqdn_or_ip = getattr(settings, 'DEBUG_DOMAIN_OR_IP', get_ip_address(request))
geo_info = get_geo_info(fqdn_or_ip)
geo_info = get_geo_info(request)
cxt = {'geo_info': geo_info}
return cxt
6 changes: 4 additions & 2 deletions geoaware/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_ip_address(request):
return ip_addr


def get_geo_info(fqdn_or_ip):
def get_geo_info(request):
""" Get GeoInfo - Relies on Django to raise exception on improper configuration """

geo_info = {
Expand All @@ -40,8 +40,10 @@ def get_geo_info(fqdn_or_ip):
'country_code': '',
'country_name': '',
}
fqdn_or_ip = getattr(settings, 'GEOAWARE_DEBUG_DOMAIN_OR_IP', get_ip_address(request))
if fqdn_or_ip:
geo = GeoIP()
cache_method = getattr(settings, "GEOIP_CACHE_METHOD", GeoIP.GEOIP_STANDARD)
geo = GeoIP(cache=cache_method)
try:
ginfo = geo.city(fqdn_or_ip)
geo_info.update(ginfo)
Expand Down
4 changes: 2 additions & 2 deletions geoaware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class GeoAwareSessionMiddleware(object):

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))
fqdn_or_ip = getattr(settings, 'GEOAWARE_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)
geo_info = get_geo_info(request)
request.session['geo_info'] = geo_info
return None
Empty file.
76 changes: 76 additions & 0 deletions geoaware/templatetags/geoaware_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from django import template
from django.conf import settings
from ..geo import get_geo_info
from ..geo import get_ip_address

register = template.Library()

def _get_geoip_info(request):
if 'geo_info' in request.session:
geo_info = request.session['geo_info']
else:
geo_info = get_geo_info(request)
return geo_info


@register.filter
def geo_country_name(request):
geo_info = _get_geoip_info(request)
return(geo_info['country_name'])

@register.filter
def geo_country_code(request):
geo_info = _get_geoip_info(request)
return(geo_info['country_code'])

@register.filter
def geo_country_code3(request):
geo_info = _get_geoip_info(request)
return(geo_info['country_code3'])

@register.filter
def geo_city(request):
geo_info = _get_geoip_info(request)
return(geo_info['city'])

@register.filter
def geo_latitude(request):
geo_info = _get_geoip_info(request)
return(geo_info['latitude'])

@register.filter
def geo_longitude(request):
geo_info = _get_geoip_info(request)
return(geo_info['longitude'])

@register.filter
def geo_postal_code(request):
geo_info = _get_geoip_info(request)
return(geo_info['postal_code'])

@register.filter
def geo_region(request):
geo_info = _get_geoip_info(request)
return(geo_info['region'])

@register.filter
def geo_dma_code(request):
geo_info = _get_geoip_info(request)
return(geo_info['dma_code'])

@register.filter
def geo_area_code(request):
geo_info = _get_geoip_info(request)
return(geo_info['area_code'])

@register.filter
def geo_fqdn_or_ip(request):
geo_info = _get_geoip_info(request)
return(geo_info['fqdn_or_ip'])

@register.filter
def geo_charset(request):
geo_info = _get_geoip_info(request)
return(geo_info['charset'])


0 comments on commit f9919bb

Please sign in to comment.