comparison gpp/core/middleware.py @ 227:423c39ee44e0

Rework the who's online middleware and template tag for #87.
author Brian Neal <bgneal@gmail.com>
date Tue, 06 Jul 2010 03:02:20 +0000
parents 8c1832b9d815
children 08b30ac04580
comparison
equal deleted inserted replaced
226:405468b8e3b9 227:423c39ee44e0
1 """Common middleware for the entire project.""" 1 """Common middleware for the entire project."""
2 import datetime
3
2 from django.contrib.auth import logout 4 from django.contrib.auth import logout
5 from django.conf import settings
6
7 from core.models import UserLastVisit
8 from core.models import AnonLastVisit
9 from core.functions import get_ip
10
3 11
4 class InactiveUserMiddleware(object): 12 class InactiveUserMiddleware(object):
5 """ 13 """
6 This middleware ensures users with is_active set to False get their 14 This middleware ensures users with is_active set to False get their
7 session destroyed and are treated as logged out. 15 session destroyed and are treated as logged out.
8 This middleware should come after the 'django.contrib.auth.middleware. 16 This middleware should come after the 'django.contrib.auth.middleware.
9 AuthenticationMiddleware' in settings.py. 17 AuthenticationMiddleware' in settings.py.
10 Idea taken from: http://djangosnippets.org/snippets/1105/ 18 Idea taken from: http://djangosnippets.org/snippets/1105/
11 """ 19 """
12 20
13 def process_request(self, request): 21 def process_view(self, request, view_func, view_args, view_kwargs):
14 if request.user.is_authenticated() and not request.user.is_active: 22 if request.user.is_authenticated() and not request.user.is_active:
15 logout(request) 23 logout(request)
16 24
25
26 ONLINE_COOKIE = 'sg101_online' # online cookie name
27 ONLINE_TIMEOUT = 10 * 60 # online cookie lifetime in seconds
28
29 class WhosOnline(object):
30 """
31 This middleware class keeps track of which registered users have
32 been seen recently, and the number of unique unregistered users.
33 This middleware should come after the authentication middleware,
34 as we count on the user attribute being attached to the request.
35 """
36
37 def process_response(self, request, response):
38 """
39 Keep track of who is online.
40 """
41 if request.is_ajax():
42 return
43
44 if request.user.is_authenticated():
45 if request.COOKIES.get(ONLINE_COOKIE) is None:
46 # update the last seen timestamp
47 try:
48 ulv = UserLastVisit.objects.get(user=request.user)
49 except UserLastVisit.DoesNotExist:
50 ulv = UserLastVisit(user=request.user)
51
52 ulv.last_visit = datetime.datetime.now()
53 ulv.save()
54
55 # set a cookie to expire in 10 minutes or so
56 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT)
57 else:
58 if request.COOKIES.get(settings.CSRF_COOKIE_NAME) is not None:
59 # We have a non-authenticated user that has cookies enabled. This
60 # means we can track them.
61 if request.COOKIES.get(ONLINE_COOKIE) is None:
62 # update the timestamp for this anonymous visitor
63 ip = get_ip(request)
64 if ip:
65 try:
66 alv = AnonLastVisit.objects.get(ip=ip)
67 except AnonLastVisit.DoesNotExist:
68 alv = AnonLastVisit(ip=ip)
69
70 alv.last_visit = datetime.datetime.now()
71 alv.save()
72
73 # set a cookie to expire in 10 minutes or so
74 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT)
75
76 return response