Login

RefreshSessionMiddleware

Author:
jcassee
Posted:
August 6, 2008
Language:
Python
Version:
.96
Tags:
sessions refresh
Score:
1 (after 1 ratings)

This middleware refreshes the session before it expires to avoid dropping the session of an active (but read-only) user. By default it refreshes the session after half the expiry time has elapsed.

(This middleware does nothing for browser-length sessions.)

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

REFRESH_AGE = getattr(settings, 'SESSION_COOKIE_REFRESH_AGE',
        int(settings.SESSION_COOKIE_AGE / 2))

class RefreshSessionMiddleware(object):
    """
    This middleware automatically refreshes the session after some time
    (default: settings.SESSION_COOKIE_AGE / 2).
    """
    def process_request(self, request):
        assert hasattr(request, 'session'), "RefreshSessionMiddleware " \
                "requires session middleware to be installed. Edit your " \
                "MIDDLEWARE_CLASSES setting to insert " \
                "'django.contrib.sessions.middleware.SessionMiddleware'."
        if request.session.get_expiry_age() < REFRESH_AGE:
            request.session.modified = True
        return None

More like this

  1. Middleware for using HttpOnly session cookie (including monkey patching for support for Python <2.6) by chrj 5 years ago
  2. CurrentSessionIDMiddleware by troolee 4 years, 10 months ago
  3. HammeringMiddleware by pandoodl 4 years, 2 months ago
  4. Keep Me Logged In for Django by krvss 4 years, 3 months ago
  5. StrictAuthentication - Auto log-out inactive users by yeago 6 years, 7 months ago

Comments

robharvey (on October 25, 2008):

I think you'd want to ensure get_expiry_age() is >= 0 as well.

#

Please login first before commenting.