Ticket #8122: 8122-r8161.diff

File 8122-r8161.diff, 4.1 KB (added by jcassee, 7 years ago)
  • django/contrib/sessions/middleware.py

     
    2222            if accessed:
    2323                patch_vary_headers(response, ('Cookie',))
    2424            if modified or settings.SESSION_SAVE_EVERY_REQUEST:
    25                 if request.session.get_expire_at_browser_close():
    26                     max_age = None
    27                     expires = None
    28                 else:
    29                     max_age = request.session.get_expiry_age()
    30                     expires_time = time.time() + max_age
    31                     expires = cookie_date(expires_time)
    3225                # Save the session data and refresh the client cookie.
    3326                request.session.save()
    34                 response.set_cookie(settings.SESSION_COOKIE_NAME,
    35                         request.session.session_key, max_age=max_age,
    36                         expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
    37                         path=settings.SESSION_COOKIE_PATH,
    38                         secure=settings.SESSION_COOKIE_SECURE or None)
     27                self.set_session_cookie(request, response,
     28                        request.session.session_key)
     29            elif settings.SESSION_COOKIE_NAME not in request.COOKIES:
     30                # Set a temporary cookie
     31                self.set_session_cookie(request, response, '')
    3932        return response
     33
     34    def set_session_cookie(self, request, response, key):
     35        if request.session.get_expire_at_browser_close():
     36            max_age = None
     37            expires = None
     38        else:
     39            max_age = request.session.get_expiry_age()
     40            expires_time = time.time() + max_age
     41            expires = cookie_date(expires_time)
     42        response.set_cookie(settings.SESSION_COOKIE_NAME, key,
     43                max_age=max_age, expires=expires,
     44                domain=settings.SESSION_COOKIE_DOMAIN,
     45                path=settings.SESSION_COOKIE_PATH,
     46                secure=settings.SESSION_COOKIE_SECURE or None)
  • django/contrib/sessions/backends/base.py

     
    1818    """
    1919    Base class for all Session classes.
    2020    """
    21     TEST_COOKIE_NAME = 'testcookie'
    22     TEST_COOKIE_VALUE = 'worked'
    2321
    2422    def __init__(self, session_key=None):
    2523        self._session_key = session_key
    2624        self.accessed = False
    2725        self.modified = False
     26        self._cookie_received = (session_key is not None)
    2827
    2928    def __contains__(self, key):
    3029        return key in self._session
     
    6261            return value
    6362
    6463    def set_test_cookie(self):
    65         self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE
     64        from warnings import warn
     65        warn('set_test_cookie is deprecated. It is no longer required.')
    6666
    6767    def test_cookie_worked(self):
    68         return self.get(self.TEST_COOKIE_NAME) == self.TEST_COOKIE_VALUE
     68        from warnings import warn
     69        warn('test_cookie_worked is deprecated. Use request.accepts_cookies()')
     70        return self.accepts_cookies()
    6971
    7072    def delete_test_cookie(self):
    71         del self[self.TEST_COOKIE_NAME]
     73        from warnings import warn
     74        warn('delete_test_cookie is deprecated. It is no longer required.')
    7275
     76    def accepts_cookies(self):
     77        return self._cookie_received
     78
    7379    def encode(self, session_dict):
    7480        "Returns the given session dictionary pickled and encoded as a string."
    7581        pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
     
    141147        try:
    142148            return self._session_cache
    143149        except AttributeError:
    144             if self._session_key is None:
     150            if self._session_key:
     151                self._session_cache = self.load()
     152            else:
    145153                self._session_cache = {}
    146             else:
    147                 self._session_cache = self.load()
    148154        return self._session_cache
    149155
    150156    _session = property(_get_session)
Back to Top