Skip to content

Instantly share code, notes, and snippets.

@snavruzov
Forked from dryan/InactivityLogout.py
Created August 18, 2021 12:44
Show Gist options
  • Save snavruzov/8b3673eb07fe9bf90a3e9215b13e5e1f to your computer and use it in GitHub Desktop.
Save snavruzov/8b3673eb07fe9bf90a3e9215b13e5e1f to your computer and use it in GitHub Desktop.
Django middleware for extending session expiration on access
class InactivityLogout(object):
def process_request( self, request ):
from datetime import datetime, timedelta
from django.conf import settings
COOKIE_AGE = getattr(settings, 'SESSION_COOKIE_AGE', 7200)
if datetime.now() – request.session.get_expiry_date() < timedelta(seconds = COOKIE_AGE):
request.session.set_expiry(datetime.now() + timedelta(seconds = COOKIE_AGE))
return None # pass through
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment