Ticket #2548: session_middleware.2.diff

File session_middleware.2.diff, 2.5 KB (added by upadhyay@…, 17 years ago)

previous patch broke things when SESSION_EXPIRE_AT_BROWSER_CLOSE is set to True

  • contrib/sessions/middleware.py

     
    1212        self.session_key = session_key
    1313        self.accessed = False
    1414        self.modified = False
     15        self.expires_time = False
    1516
    1617    def __contains__(self, key):
    1718        return key in self._session
     
    4849    def delete_test_cookie(self):
    4950        del self[TEST_COOKIE_NAME]
    5051
     52    def set_expires(self, val):
     53        self.expires_time = val
     54        self.modified = True
     55
     56    def get_expires(self):
     57        return self.expires_time or settings.SESSION_COOKIE_AGE
     58
     59
    5160    def _get_session(self):
    5261        # Lazily loads session from storage.
    5362        self.accessed = True
     
    6978            return self._session_cache
    7079
    7180    _session = property(_get_session)
     81    expires = property(get_expires, set_expires)
    7282
    7383class SessionMiddleware(object):
    7484    def process_request(self, request):
     
    92102                    obj = Session.objects.get_new_session_object()
    93103                    session_key = obj.session_key
    94104
    95                 if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE:
     105                if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE and not request.session.expires_time:
    96106                    max_age = None
     107                    delta_yield = datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE)
    97108                    expires = None
    98109                else:
    99                     max_age = settings.SESSION_COOKIE_AGE
    100                     expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT")
     110                    max_age = request.session.expires
     111                    delta_yield = datetime.datetime.now() + datetime.timedelta(seconds=max_age)
     112                    expires = datetime.datetime.strftime(delta_yield, "%a, %d-%b-%Y %H:%M:%S GMT")
    101113                new_session = Session.objects.save(session_key, request.session._session,
    102                     datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
     114                    delta_yield)
    103115                response.set_cookie(settings.SESSION_COOKIE_NAME, session_key,
    104116                    max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
    105117                    secure=settings.SESSION_COOKIE_SECURE or None)
Back to Top