1
from django.conf import settings
5
# this is not intended to be an all-knowing IP address regex
6
IP_RE = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
10
"""Retrieves the remote IP address from the request data.
13
behind a proxy, they may have a comma-separated list of IP addresses, so
14
we need to account for that. In such a case, only the first IP in the
15
list will be retrieved. Also, some hosts that use a proxy will put the
16
REMOTE_ADDR into HTTP_X_FORWARDED_FOR. This will handle pulling back the
17
IP from the proper place.
21
# if neither header contain a value, just use local loopback
22
ip_address = request.META.get('HTTP_X_FORWARDED_FOR',
23
request.META.get('REMOTE_ADDR', '127.0.0.1'))
25
# make sure we have one and only one IP
27
ip_address = IP_RE.match(ip_address)
29
ip_address = ip_address.group(0)
31
# no IP, probably from some dirty proxy or other device
32
# throw in some bogus IP
33
ip_address = '10.0.0.1'
41
"""Gets any specified timeout from the settings file, or use 10 minutes by
43
return getattr(settings, 'TRACKING_TIMEOUT', 10)
46
def get_cleanup_timeout():
48
Gets any specified visitor clean-up timeout from the settings file, or
49
use 24 hours by default
51
return getattr(settings, 'TRACKING_CLEANUP_TIMEOUT', 24)
55
"""A strange attempt at cleaning up unicode."""
60
uni = str(s).decode('iso-8859-1')
61
except UnicodeDecodeError:
64
uni = str(s).decode('utf-8')
65
except UnicodeDecodeError:
66
# last resort method... one character at a time (ugh)
67
if s and type(s) in (str, unicode):
70
uni += unicodedata.normalize('NFKC', unicode(c))
71
except UnicodeDecodeError:
74
return uni.encode('ascii', 'xmlcharrefreplace')