Login

Auto Logout Middleware

Author:
LuckiDog
Posted:
October 19, 2007
Language:
Python
Version:
.96
Score:
5 (after 5 ratings)

This Middleware is to log users out after a certain amount of time has passed. You'll want to add AUTO_LOGOUT_DELAY to your settings.py, set to a number of minutes after which a user should be logged out.

It adds the key 'last_touch' to the session, you'll want to change that if you happen to be using that already.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.conf import settings
from django.contrib import auth
from datetime import datetime, timedelta

class AutoLogout:
  def process_request(self, request):
    if not request.user.is_authenticated() :
      #Can't log out if not logged in
      return

    try:
      if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0):
        auth.logout(request)
        del request.session['last_touch']
        return
    except KeyError:
      pass

    request.session['last_touch'] = datetime.now()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

david_bgk (on October 22, 2007):

Thanks for this snippet, an import is missing:

from django.contrib import auth

#

LuckiDog (on October 24, 2007):

Thanks, I've updated the code.

#

kmajbar (on November 25, 2007):

Hello,

I get the following error when I try to use your middleware :

----------------------------------------------------------

Traceback (most recent call last):

  File "/var/lib/python-support/python2.5/django/core/servers/basehttp.py", line 272, in run
    self.result = application(self.environ, self.start_response)

  File "/var/lib/python-support/python2.5/django/core/servers/basehttp.py", line 614, in __call__
    return self.application(environ, start_response)

  File "/var/lib/python-support/python2.5/django/core/handlers/wsgi.py", line 184, in __call__
    self.load_middleware()

  File "/var/lib/python-support/python2.5/django/core/handlers/base.py", line 35, in load_middleware
    raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname)

ImproperlyConfigured: Middleware module "WebGui" does not define a "AutoLogout" class

----------------------------------------------------------

WebGui is my project and in it's settings.py I added 'WebGui.AutoLogout', in the middlware conf like so :

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'WebGui.AutoLogout',
)

and I also added:

AUTO_LOGOUT_DELAY=1

to settings.py like you said. Did I do something wrong?

Thank you, Kenza

#

LuckiDog (on February 22, 2010):

Three years later, but you'll need to put this class in a file called middleware.py in your application

#

LuckiDog (on February 22, 2010):

@kmajbar To use this as a project level middleware, you would save this code as WebGui/middleware.py

Then add 'WebGui.middleware.AutoLogout' to your MIDDLEWARE_CLASSES

#

Please login first before commenting.