Skip to content

Instantly share code, notes, and snippets.

@dannyroa
Created June 24, 2010 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyroa/452008 to your computer and use it in GitHub Desktop.
Save dannyroa/452008 to your computer and use it in GitHub Desktop.
import re
import logging
from django.conf import settings
from django.core.cache import cache
from userprofile.models import LastOnline
import datetime
from django.core.urlresolvers import reverse
class LastOnlineMiddleware(object):
"""Log the last time a user was online"""
def process_request(self, request):
if request.user.is_authenticated() and not request.path.startswith(reverse('admin:index')) and not request.path.startswith(settings.MEDIA_URL):
cache_created = cache.add('last_online_%s' % request.user.id, datetime.datetime.now(), settings.LAST_ONLINE_CACHE_EXPIRE_IN_SECONDS)
if cache_created:
last_online, created = LastOnline.objects.get_or_create(user=request.user)
if not created:
last_online.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment