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
- StrictAuthentication - Auto log-out inactive users by yeago 3 years, 7 months ago
- Log to syslog by fylb 2 years, 3 months ago
- Facebook shell by stephenemslie 2 years, 8 months ago
- Decorator to logout user based on a test and/or redirect to another url by vemubalu 1 year, 5 months ago
- Profiling Middlware by udfalkso 5 years, 1 month ago
Comments
Thanks for this snippet, an import is missing:
#
Thanks, I've updated the code.
#
Hello,
I get the following error when I try to use your middleware :
----------------------------------------------------------
----------------------------------------------------------
WebGui is my project and in it's settings.py I added 'WebGui.AutoLogout', in the middlware conf like so :
and I also added:
to settings.py like you said. Did I do something wrong?
Thank you, Kenza
#
Three years later, but you'll need to put this class in a file called middleware.py in your application
#
@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
#