2013-09-28 23:31:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-11-01 04:03:34 +01:00
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2013-09-28 23:31:33 +02:00
|
|
|
from django.conf import settings
|
2013-11-01 04:03:34 +01:00
|
|
|
from django.utils.timezone import now
|
|
|
|
|
|
|
|
MAX_IP_AGE = 180 # seconds
|
2013-09-28 23:31:33 +02:00
|
|
|
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-28 23:31:33 +02:00
|
|
|
def add_settings(request):
|
|
|
|
context = {}
|
2013-10-07 20:02:00 +02:00
|
|
|
context['WWW_HOST'] = settings.WWW_HOST
|
2013-09-28 23:31:33 +02:00
|
|
|
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
|
|
|
|
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
|
|
|
|
return context
|
2013-11-01 04:03:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def remove_stale_ips(request):
|
|
|
|
"""
|
|
|
|
Check the session if there are stale IPs and if so, remove them.
|
|
|
|
"""
|
|
|
|
# XXX is a context processor is the right place for this?
|
|
|
|
s = request.session
|
|
|
|
t_now = now()
|
|
|
|
for key in ['ipv4', 'ipv6', ]:
|
|
|
|
timestamp_key = "%s_timestamp" % key
|
|
|
|
try:
|
|
|
|
timestamp = s[timestamp_key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
stale = timestamp + timedelta(seconds=MAX_IP_AGE) < t_now
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
# invalid timestamp in session
|
|
|
|
del s[timestamp_key]
|
|
|
|
else:
|
|
|
|
if stale:
|
|
|
|
print "ts: %s now: %s - killing %s (was: %s)" % (timestamp, t_now, key, s[key])
|
|
|
|
# kill the IP, it is not up-to-date any more
|
|
|
|
# note: it is used to fill form fields, so set it to empty string
|
|
|
|
s[key] = ''
|
|
|
|
# update the timestamp, so we can retry after a while
|
|
|
|
s[timestamp_key] = t_now
|
|
|
|
return {}
|