Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merging branches

  • Loading branch information...
commit 834d1b5aa9ddc3770874842533177ff253b16f59 2 parents 3022f0c + 2d91f35
Josh VanderLinden codekoala authored
27 tracking/__init__.py
... ... @@ -1,33 +1,8 @@
1 1 import listeners
2 2
3   -VERSION = (0, 3, 7)
  3 +VERSION = (0, 4, 0)
4 4
5 5 def get_version():
6 6 "Returns the version as a human-format string."
7 7 return '.'.join([str(i) for i in VERSION])
8 8
9   -# initialize the URL prefixes that we shouldn't track
10   -try:
11   - from django.conf import settings
12   - prefixes = getattr(settings, 'NO_TRACKING_PREFIXES', [])
13   -except ImportError:
14   - pass
15   -else:
16   - if '!!initialized!!' not in prefixes:
17   - from django.core.urlresolvers import reverse, NoReverseMatch
18   - if settings.MEDIA_URL and settings.MEDIA_URL != '/':
19   - prefixes.append(settings.MEDIA_URL)
20   -
21   - if settings.ADMIN_MEDIA_PREFIX:
22   - prefixes.append(settings.ADMIN_MEDIA_PREFIX)
23   -
24   - try:
25   - # finally, don't track requests to the tracker update pages
26   - prefixes.append(reverse('tracking-refresh-active-users'))
27   - except NoReverseMatch:
28   - # django-tracking hasn't been included in the URLconf if we get here
29   - pass
30   -
31   - prefixes.append('!!initialized!!')
32   -
33   - settings.NO_TRACKING_PREFIXES = prefixes
37 tracking/middleware.py
... ... @@ -1,23 +1,22 @@
1 1 from datetime import datetime, timedelta
2 2 import logging
3   -import random
4 3 import re
5   -import time
6 4 import traceback
7   -import urllib, urllib2
8 5
9 6 from django.conf import settings
10 7 from django.contrib.auth.models import AnonymousUser
11 8 from django.core.cache import cache
  9 +from django.core.urlresolvers import reverse, NoReverseMatch
12 10 from django.db.utils import DatabaseError
13 11 from django.http import Http404
  12 +
14 13 from tracking import utils
15 14 from tracking.models import Visitor, UntrackedUserAgent, BannedIP
16 15
17 16 title_re = re.compile('<title>(.*?)</title>')
18 17 log = logging.getLogger('tracking.middleware')
19 18
20   -class VisitorTrackingMiddleware:
  19 +class VisitorTrackingMiddleware(object):
21 20 """
22 21 Keeps track of your active users. Anytime a visitor accesses a valid URL,
23 22 their unique record will be updated with the page they're on and the last
@@ -29,6 +28,32 @@ class VisitorTrackingMiddleware:
29 28 same IP and user agent in the last 5 minutes
30 29 """
31 30
  31 + @property
  32 + def prefixes(self):
  33 + """Returns a list of URL prefixes that we should not track"""
  34 +
  35 + if not hasattr(self, '_prefixes'):
  36 + self._prefixes = getattr(settings, 'NO_TRACKING_PREFIXES', [])
  37 +
  38 + if not getattr(settings, '_FREEZE_TRACKING_PREFIXES', False):
  39 + for name in ('MEDIA_URL', 'ADMIN_MEDIA_PREFIX'):
  40 + url = getattr(settings, name)
  41 + if url and url != '/':
  42 + self._prefixes.append(url)
  43 +
  44 + try:
  45 + # finally, don't track requests to the tracker update pages
  46 + self._prefixes.append(reverse('tracking-refresh-active-users'))
  47 + except NoReverseMatch:
  48 + # django-tracking hasn't been included in the URLconf if we
  49 + # get here, which is not a bad thing
  50 + pass
  51 +
  52 + settings.NO_TRACKING_PREFIXES = self._prefixes
  53 + settings._FREEZE_TRACKING_PREFIXES = True
  54 +
  55 + return self._prefixes
  56 +
32 57 def process_request(self, request):
33 58 # don't process AJAX requests
34 59 if request.is_ajax(): return
@@ -59,10 +84,8 @@ def process_request(self, request):
59 84 # otherwise just fake a session key
60 85 session_key = '%s:%s' % (ip_address, user_agent)
61 86
62   - prefixes = getattr(settings, 'NO_TRACKING_PREFIXES', [])
63   -
64 87 # ensure that the request.path does not begin with any of the prefixes
65   - for prefix in prefixes:
  88 + for prefix in self.prefixes:
66 89 if request.path.startswith(prefix):
67 90 log.debug('Not tracking request to: %s' % request.path)
68 91 return
22 tracking/templates/tracking/_active_users.js
@@ -33,6 +33,28 @@ $(document).ready(function () {
33 33 });
34 34 $(document).unload(GUnload);
35 35
  36 +$('html').ajaxSend(function(event, xhr, settings) {
  37 + function getCookie(name) {
  38 + var cookieValue = null;
  39 + if (document.cookie && document.cookie != '') {
  40 + var cookies = document.cookie.split(';');
  41 + for (var i = 0; i < cookies.length; i++) {
  42 + var cookie = jQuery.trim(cookies[i]);
  43 + // Does this cookie string begin with the name we want?
  44 + if (cookie.substring(0, name.length + 1) == (name + '=')) {
  45 + cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  46 + break;
  47 + }
  48 + }
  49 + }
  50 + return cookieValue;
  51 + }
  52 + if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
  53 + // Only send the token to relative URLs i.e. locally.
  54 + xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
  55 + }
  56 +});
  57 +
36 58 function createMarkers() {
37 59 // Pull back all active users and create markers for them
38 60 $.ajax({

0 comments on commit 834d1b5

Please sign in to comment.
Something went wrong with that request. Please try again.