예제 #1
0
 def process_response(self, request, response):
     """
     If request.session was modified, or if the configuration is to save the
     session every time, save the changes and set a session cookie.
     """
     try:
         accessed = request.session.accessed
         modified = request.session.modified
     except AttributeError:
         pass
     else:
         if accessed:
             patch_vary_headers(response, ('Cookie',))
         if modified or settings.SESSION_SAVE_EVERY_REQUEST:
             if request.session.get_expire_at_browser_close():
                 max_age = None
                 expires = None
             else:
                 max_age = request.session.get_expiry_age()
                 expires_time = time.time() + max_age
                 expires = cookie_date(expires_time)
             # Save the session data and refresh the client cookie.
             # Skip session save for 500 responses, refs #3881.
             if response.status_code != 500:
                 request.session.save()
                 response.set_cookie(settings.SESSION_COOKIE_NAME,
                         request.session.session_key, max_age=max_age,
                         expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
                         path=settings.SESSION_COOKIE_PATH,
                         secure=settings.SESSION_COOKIE_SECURE or None,
                         httponly=settings.SESSION_COOKIE_HTTPONLY or None)
     return response
예제 #2
0
 def test_max_age_expiration(self):
     "Cookie will expire if max_age is provided"
     response = HttpResponse()
     response.set_cookie("max_age", max_age=10)
     max_age_cookie = response.cookies["max_age"]
     self.assertEqual(max_age_cookie["max-age"], 10)
     self.assertEqual(max_age_cookie["expires"], cookie_date(time.time() + 10))
예제 #3
0
    def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
                   domain=None, secure=False, httponly=False):
        """
        Sets a cookie.

        ``expires`` can be:
        - a string in the correct format,
        - a naive ``datetime.datetime`` object in UTC,
        - an aware ``datetime.datetime`` object in any time zone.
        If it is a ``datetime.datetime`` object then ``max_age`` will be calculated.

        """
        self.cookies[key] = value
        if expires is not None:
            if isinstance(expires, datetime.datetime):
                if timezone.is_aware(expires):
                    expires = timezone.make_naive(expires, timezone.utc)
                delta = expires - expires.utcnow()
                # Add one second so the date matches exactly (a fraction of
                # time gets lost between converting to a timedelta and
                # then the date string).
                delta = delta + datetime.timedelta(seconds=1)
                # Just set max_age - the max_age logic will set expires.
                expires = None
                max_age = max(0, delta.days * 86400 + delta.seconds)
            else:
                self.cookies[key]['expires'] = expires
        if max_age is not None:
            self.cookies[key]['max-age'] = max_age
            # IE requires expires, so set it if hasn't been already.
            if not expires:
                self.cookies[key]['expires'] = cookie_date(time.time() +
                                                           max_age)
        if path is not None:
            self.cookies[key]['path'] = path
        if domain is not None:
            self.cookies[key]['domain'] = domain
        if secure:
            self.cookies[key]['secure'] = True
        if httponly:
            self.cookies[key]['httponly'] = True
예제 #4
0
 def test_cookie_date(self):
     t = 1167616461.0
     self.assertEqual(cookie_date(t), 'Mon, 01-Jan-2007 01:54:21 GMT')