Login

RefreshSessionMiddleware

Author:
jcassee
Posted:
August 6, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 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.