Commits

Josh VanderLinden committed 43918ab

Began using Django's built-in GIS utilities instead of relying on the C library (GC #10). Updated the README to reflect this change. Reorganized the code a little bit.

  • Participants
  • Parent commits 0e0223a

Comments (0)

Files changed (5)

 
 If you wish to use a Google Map to display where your visitors are probably at,
 you must have a `Google Maps API key
-<http://code.google.com/intl/ro/apis/maps/signup.html>`_, which is free.  Along
-with that, you must have the `GeoIP C API
-<http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz>`_ installed and
-the `GeoIP Python API
-<http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.4.tar.gz>`_.
-Finally, you might want to grab the GeoLite City binary unless you are a paying
-MaxMind customer.  Configuring this feature is discussed later.
+<http://code.google.com/intl/ro/apis/maps/signup.html>`_, which is free.  In
+the past, you were required to have a couple of GeoIP API libraries.  Since
+version 0.2.11, these dependencies have been replaced with Django's built-in
+GIS utilities.  You might want to grab the `GeoLite City binary
+<http://www.maxmind.com/app/geolitecity>`_ unless you are a paying MaxMind
+customer.  This is the data file that ``django-tracking`` uses to translate an
+IP into a location on the planet.  Configuring this feature is discussed later.
 
 Installation
 ============
   like ``/usr/local/share/GeoIP.dat`` or ``/usr/share/GeoIP/GeoIP.dat``.  You
   can try leaving this blank if you want; the code will look in the default
   location if possible.
+* ``DEFAULT_TRACKING_TEMPLATE``: The template to use when generating the
+  visitor map.  Defaults to ``tracking/visitor_map.html``.
 
 When that's done, you should be able to go to ``/tracking/map/`` on your site
 (replacing ``tracking`` with whatever prefix you chose to use in your URLconf,

tracking/__init__.py

-VERSION = (0, 2, 10)
+VERSION = (0, 2, 11)
 
 def get_version():
     "Returns the version as a human-format string."

tracking/models.py

+from datetime import datetime, timedelta
+from django.conf import settings
+from django.contrib.auth.models import User
+from django.contrib.gis.utils import GeoIP
 from django.db import models
-from django.contrib.auth.models import User
-from django.conf import settings
 from django.utils.translation import ugettext, ugettext_lazy as _
 from tracking import utils
-from datetime import datetime, timedelta
 import os
-import unicodedata
-
-try:
-    import GeoIP
-except ImportError:
-    GeoIP = None
-
-def u_clean(s):
-    uni = ''
-    try:
-        # try this first
-        uni = str(s).decode('iso-8859-1')
-    except UnicodeDecodeError:
-        try:
-            # try utf-8 next
-            uni = str(s).decode('utf-8')
-        except UnicodeDecodeError:
-            # last resort method... one character at a time
-            if s and type(s) in (str, unicode):
-                for c in s:
-                    try:
-                        uni += unicodedata.normalize('NFKC', unicode(c))
-                    except UnicodeDecodeError:
-                        uni += '-'
-
-    return uni.encode('ascii', 'xmlcharrefreplace')
 
 class VisitorManager(models.Manager):
     def active(self, timeout=None):
         if getattr(settings, 'TRACKING_USE_GEOIP', False) and GeoIP:
             geoip_data_file = getattr(settings, 'GEOIP_DATA_FILE', None)
 
-            if geoip_data_file:
-                assert os.access(geoip_data_file, os.R_OK)
+            if geoip_data_file and os.access(geoip_data_file, os.R_OK):
                 gip = GeoIP.open(geoip_data_file, GeoIP.GEOIP_MEMORY_CACHE)
-            else:
-                gip = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
 
             try:
                 return gip.record_by_addr(self.ip_address)
                 # if we get here, chances are that we didn't get a result for
                 # the IP
                 pass
+
         return None
+
     geoip_data = property(_get_geoip_data)
 
     def _get_geoip_data_json(self):
         if not self.geoip_data: return {}
 
         for key,value in self.geoip_data.items():
-            #print 'Cleaning: %s :: %s' % (key, value)
-            clean[key] = u_clean(value)
+            clean[key] = utils.u_clean(value)
         return clean
+
     geoip_data_json = property(_get_geoip_data_json)
 
     class Meta:

tracking/utils.py

 from django.conf import settings
 import re
+import unicodedata
 
 # this is not intended to be an all-knowing IP address regex
 IP_RE = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
     gets a list of prefixes that shouldn't be tracked
     """
     return getattr(settings, 'NO_TRACKING_PREFIXES', [])
+
+def u_clean(s):
+    """A strange attempt at cleaning up unicode"""
+
+    uni = ''
+    try:
+        # try this first
+        uni = str(s).decode('iso-8859-1')
+    except UnicodeDecodeError:
+        try:
+            # try utf-8 next
+            uni = str(s).decode('utf-8')
+        except UnicodeDecodeError:
+            # last resort method... one character at a time (ugh)
+            if s and type(s) in (str, unicode):
+                for c in s:
+                    try:
+                        uni += unicodedata.normalize('NFKC', unicode(c))
+                    except UnicodeDecodeError:
+                        uni += '-'
+
+    return uni.encode('ascii', 'xmlcharrefreplace')
+

tracking/views.py

 from django.shortcuts import render_to_response
 from django.conf import settings
 from django.utils.translation import ungettext
-from tracking.models import Visitor, u_clean as uc
+from tracking.models import Visitor
+from tracking.utils import u_clean as uc
 from datetime import datetime
 
 DEFAULT_TRACKING_TEMPLATE = getattr(settings, 'DEFAULT_TRACKING_TEMPLATE',
 
     return friendly_time
 
-def display_map(request, template_name=DEFAULT_TRACKING_TEMPLATE, 
+def display_map(request, template_name=DEFAULT_TRACKING_TEMPLATE,
         extends_template='base.html'):
     """
     Displays a map of recently active users.  Requires a Google Maps API key