CurrentSessionIDMiddleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from uuid import uuid4

from django.conf import settings


if hasattr(settings, 'FLASH_SESSION_COOKIE_NAME'):
    FLASH_SESSION_COOKIE_NAME = settings.FLASH_SESSION_COOKIE_NAME
else:
    FLASH_SESSION_COOKIE_NAME = 'XFlashSessionID'


class LazySessionId(object):
    def __get__(self, request, obj_type=None):
        if not hasattr(request, '_cached_current_session_id'):
            if FLASH_SESSION_COOKIE_NAME in request.COOKIES:
                request._cached_current_session_id = request.COOKIES[FLASH_SESSION_COOKIE_NAME]
            else:
                request._cached_current_session_id = str(uuid4()) 
        return request._cached_current_session_id


class CurrentSessionIDMiddleware(object):
    def process_request(self, request):
        request.__class__.current_session_id = LazySessionId()
        
    
    def process_response(self, request, response):
        if hasattr(request, '_cached_current_session_id'):
            response.set_cookie(FLASH_SESSION_COOKIE_NAME, request.current_session_id)
        return response

More like this

  1. Sessions and authentication without cookies by danfairs 6 years, 5 months ago
  2. Upload, Progressbar with sessions by revolunet 5 years, 7 months ago
  3. Middleware for using HttpOnly session cookie (including monkey patching for support for Python <2.6) by chrj 3 years, 11 months ago
  4. Add httponly to session cookie by rodolfo.3 4 years ago
  5. RefreshSessionMiddleware by jcassee 5 years, 8 months ago

Comments

(Forgotten your password?)