Ticket #7770: 7770.diff

File 7770.diff, 3.3 KB (added by Chris Beaven, 16 years ago)
  • tests/regressiontests/requests/tests.py

     
    11"""
    2 >>> from django.http import HttpRequest
     2>>> from django.http import HttpRequest, HttpResponse
    33>>> print repr(HttpRequest())
    44<HttpRequest
    55GET:{},
     
    4444>>> request.path = ''
    4545>>> print request.build_absolute_uri(location="/path/with:colons")
    4646http://www.example.com/path/with:colons
     47
     48
     49# Test cookie datetime expiration logic
     50>>> from datetime import datetime, timedelta
     51>>> delta = timedelta(seconds=10)
     52>>> response = HttpResponse()
     53>>> response.set_cookie('datetime', expires=datetime.now()+delta)
     54>>> response.set_cookie('delta', expires=delta)
     55>>> datetime_cookie = response.cookies['datetime']
     56>>> timedelta_cookie = response.cookies['datetime']
     57>>> datetime_cookie['max-age']
     5810
     59>>> datetime_cookie['max-age'] == timedelta_cookie['max-age']
     60True
     61>>> datetime_cookie['expires'] == timedelta_cookie['expires']
     62True
     63>>> response.set_cookie('datetime', expires=datetime(2028,1,1,4,5,6))
     64>>> response.cookies['datetime']['expires']
     65'Sat, 01-Jan-2028 04:05:06 GMT'
    4766"""
  • django/http/__init__.py

     
    11import os
    22import re
     3import time
     4import datetime
    35from Cookie import SimpleCookie, CookieError
    46from pprint import pformat
    57from urllib import urlencode
     
    1214
    1315from django.utils.datastructures import MultiValueDict, ImmutableList
    1416from django.utils.encoding import smart_str, iri_to_uri, force_unicode
     17from django.utils.http import cookie_date
    1518from django.http.multipartparser import MultiPartParser
    1619from django.conf import settings
    1720from django.core.files import uploadhandler
     
    337340
    338341    def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
    339342                   domain=None, secure=False):
     343        """
     344        Sets a cookie.
     345
     346        ``expires`` can be a string in the correct format or a datetime object
     347        (either datetime.datetime or datetime.timedelta). If ``expires`` is a
     348        datetime object then ``max_age`` will be calculated.
     349        """
    340350        self.cookies[key] = value
     351        if expires is not None:
     352            if isinstance(expires, (datetime.datetime, datetime.timedelta)):
     353                if isinstance(expires, datetime.datetime):
     354                    expires = expires - expires.now()
     355                    # Add one second so the date matches exactly (a fraction of
     356                    # time gets lost between converting to a timedelta and
     357                    # then the date string).
     358                    expires = expires + datetime.timedelta(seconds=1)
     359                max_age = max(0, expires.days * 86400 + expires.seconds)
     360                expires = cookie_date(time.time() + max_age)
     361            self.cookies[key]['expires'] = expires
    341362        if max_age is not None:
    342363            self.cookies[key]['max-age'] = max_age
    343         if expires is not None:
    344             self.cookies[key]['expires'] = expires
    345364        if path is not None:
    346365            self.cookies[key]['path'] = path
    347366        if domain is not None:
Back to Top