Mercurial > public > sg101
diff 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 |
line wrap: on
line diff
--- a/gpp/core/middleware.py Thu Jun 10 03:31:57 2010 +0000 +++ b/gpp/core/middleware.py Tue Jul 06 03:02:20 2010 +0000 @@ -1,5 +1,13 @@ """Common middleware for the entire project.""" +import datetime + from django.contrib.auth import logout +from django.conf import settings + +from core.models import UserLastVisit +from core.models import AnonLastVisit +from core.functions import get_ip + class InactiveUserMiddleware(object): """ @@ -10,7 +18,59 @@ Idea taken from: http://djangosnippets.org/snippets/1105/ """ - def process_request(self, request): + def process_view(self, request, view_func, view_args, view_kwargs): if request.user.is_authenticated() and not request.user.is_active: logout(request) + +ONLINE_COOKIE = 'sg101_online' # online cookie name +ONLINE_TIMEOUT = 10 * 60 # online cookie lifetime in seconds + +class WhosOnline(object): + """ + This middleware class keeps track of which registered users have + been seen recently, and the number of unique unregistered users. + This middleware should come after the authentication middleware, + as we count on the user attribute being attached to the request. + """ + + def process_response(self, request, response): + """ + Keep track of who is online. + """ + if request.is_ajax(): + return + + if request.user.is_authenticated(): + if request.COOKIES.get(ONLINE_COOKIE) is None: + # update the last seen timestamp + try: + ulv = UserLastVisit.objects.get(user=request.user) + except UserLastVisit.DoesNotExist: + ulv = UserLastVisit(user=request.user) + + ulv.last_visit = datetime.datetime.now() + ulv.save() + + # set a cookie to expire in 10 minutes or so + response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) + else: + if request.COOKIES.get(settings.CSRF_COOKIE_NAME) is not None: + # We have a non-authenticated user that has cookies enabled. This + # means we can track them. + if request.COOKIES.get(ONLINE_COOKIE) is None: + # update the timestamp for this anonymous visitor + ip = get_ip(request) + if ip: + try: + alv = AnonLastVisit.objects.get(ip=ip) + except AnonLastVisit.DoesNotExist: + alv = AnonLastVisit(ip=ip) + + alv.last_visit = datetime.datetime.now() + alv.save() + + # set a cookie to expire in 10 minutes or so + response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) + + return response