Mercurial > public > sg101
comparison gpp/core/middleware.py @ 423:3fe60148f75c
Fixing #203; use Redis for who's online function.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 23 Apr 2011 19:19:38 +0000 |
parents | e9a066db3f54 |
children |
comparison
equal
deleted
inserted
replaced
422:6309814cd6f7 | 423:3fe60148f75c |
---|---|
4 | 4 |
5 from django.db import IntegrityError | 5 from django.db import IntegrityError |
6 from django.contrib.auth import logout | 6 from django.contrib.auth import logout |
7 from django.conf import settings | 7 from django.conf import settings |
8 | 8 |
9 from core.models import UserLastVisit | |
10 from core.models import AnonLastVisit | |
11 from core.functions import get_ip | 9 from core.functions import get_ip |
10 from core.whos_online import report_user, report_visitor | |
12 | 11 |
13 | 12 |
14 class InactiveUserMiddleware(object): | 13 class InactiveUserMiddleware(object): |
15 """ | 14 """ |
16 This middleware ensures users with is_active set to False get their | 15 This middleware ensures users with is_active set to False get their |
24 if request.user.is_authenticated() and not request.user.is_active: | 23 if request.user.is_authenticated() and not request.user.is_active: |
25 logout(request) | 24 logout(request) |
26 | 25 |
27 | 26 |
28 ONLINE_COOKIE = 'sg101_online' # online cookie name | 27 ONLINE_COOKIE = 'sg101_online' # online cookie name |
29 ONLINE_TIMEOUT = 10 * 60 # online cookie lifetime in seconds | 28 ONLINE_TIMEOUT = 5 * 60 # online cookie lifetime in seconds |
29 | |
30 | 30 |
31 class WhosOnline(object): | 31 class WhosOnline(object): |
32 """ | 32 """ |
33 This middleware class keeps track of which registered users have | 33 This middleware class keeps track of which registered users have |
34 been seen recently, and the number of unique unregistered users. | 34 been seen recently, and the number of unique unregistered users. |
48 if not hasattr(request, 'user') or request.is_ajax(): | 48 if not hasattr(request, 'user') or request.is_ajax(): |
49 return response | 49 return response |
50 | 50 |
51 if request.user.is_authenticated(): | 51 if request.user.is_authenticated(): |
52 if request.COOKIES.get(ONLINE_COOKIE) is None: | 52 if request.COOKIES.get(ONLINE_COOKIE) is None: |
53 # update the last seen timestamp | 53 # report that we've seen the user |
54 try: | 54 report_user(request.user.username) |
55 ulv = UserLastVisit.objects.get(user=request.user) | |
56 except UserLastVisit.DoesNotExist: | |
57 ulv = UserLastVisit(user=request.user) | |
58 | 55 |
59 ulv.last_visit = datetime.datetime.now() | 56 # set a cookie to expire |
60 ulv.save() | |
61 | |
62 # set a cookie to expire in 10 minutes or so | |
63 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) | 57 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) |
64 else: | 58 else: |
65 if request.COOKIES.get(settings.CSRF_COOKIE_NAME) is not None: | 59 if request.COOKIES.get(settings.CSRF_COOKIE_NAME) is not None: |
66 # We have a non-authenticated user that has cookies enabled. This | 60 # We have a non-authenticated user that has cookies enabled. This |
67 # means we can track them. | 61 # means we can track them. |
68 if request.COOKIES.get(ONLINE_COOKIE) is None: | 62 if request.COOKIES.get(ONLINE_COOKIE) is None: |
69 # update the timestamp for this anonymous visitor | 63 # see if we can get the IP address |
70 ip = get_ip(request) | 64 ip = get_ip(request) |
71 if ip: | 65 if ip: |
72 try: | 66 # report that we've seen this visitor |
73 alv = AnonLastVisit.objects.get(ip=ip) | 67 report_visitor(ip) |
74 except AnonLastVisit.DoesNotExist: | |
75 alv = AnonLastVisit(ip=ip) | |
76 | 68 |
77 alv.last_visit = datetime.datetime.now() | 69 # set a cookie to expire |
78 | |
79 # There is a race condition and sometimes another thread | |
80 # saves a record before we do; just log this if it happens. | |
81 try: | |
82 alv.save() | |
83 except IntegrityError: | |
84 logging.exception('WhosOnline.process_response') | |
85 | |
86 # set a cookie to expire in 10 minutes or so | |
87 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) | 70 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) |
88 | 71 |
89 return response | 72 return response |