Ticket #7770: 7770.2.diff

File 7770.2.diff, 4.5 KB (added by SmileyChris, 5 years ago)
  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index e585a71..7c096fc 100644
    a b  
     1import datetime
    12import os
    23import re
     4import time
    35from Cookie import BaseCookie, SimpleCookie, CookieError
    46from pprint import pformat
    57from urllib import urlencode
    except ImportError: 
    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
    class HttpResponse(object): 
    373376
    374377    def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
    375378                   domain=None, secure=False):
     379        """
     380        Sets a cookie.
     381
     382        ``expires`` can be a string in the correct format or a datetime object
     383        (either datetime.datetime or datetime.timedelta) in UTC. If ``expires``
     384        is a datetime object then ``max_age`` will be calculated.
     385        """
    376386        self.cookies[key] = value
    377         if max_age is not None:
    378             self.cookies[key]['max-age'] = max_age
    379387        if expires is not None:
     388            if isinstance(expires, (datetime.datetime, datetime.timedelta)):
     389                if isinstance(expires, datetime.datetime):
     390                    expires = expires - expires.utcnow()
     391                    # Add one second so the date matches exactly (a fraction of
     392                    # time gets lost between converting to a timedelta and
     393                    # then the date string).
     394                    expires = expires + datetime.timedelta(seconds=1)
     395                max_age = max(0, expires.days * 86400 + expires.seconds)
     396                expires = cookie_date(time.time() + max_age)
    380397            self.cookies[key]['expires'] = expires
     398        if max_age is not None:
     399            self.cookies[key]['max-age'] = max_age
    381400        if path is not None:
    382401            self.cookies[key]['path'] = path
    383402        if domain is not None:
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index bf984f4..400bf0b 100644
    a b Methods 
    512512    Sets a cookie. The parameters are the same as in the `cookie Morsel`_
    513513    object in the Python standard library.
    514514
     515        * ``expires`` should either be a string in the format
     516          ``"Wdy, DD-Mon-YY HH:MM:SS GMT"`` or a ``datetime`` object
     517          (either ``datetime.datetime`` in UTC or ``datetime.timedelta``).
     518          If ``expires`` is either type of ``datetime`` object, the ``max_age``
     519          will be calculated.
    515520        * ``max_age`` should be a number of seconds, or ``None`` (default) if
    516521          the cookie should last only as long as the client's browser session.
    517         * ``expires`` should be a string in the format
    518           ``"Wdy, DD-Mon-YY HH:MM:SS GMT"``.
    519522        * Use ``domain`` if you want to set a cross-domain cookie. For example,
    520523          ``domain=".lawrence.com"`` will set a cookie that is readable by
    521524          the domains www.lawrence.com, blogs.lawrence.com and
  • tests/regressiontests/requests/tests.py

    diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
    index 1615a73..f6c2c6d 100644
    a b  
    11"""
    2 >>> from django.http import HttpRequest
     2>>> from django.http import HttpRequest, HttpResponse
    33>>> print repr(HttpRequest())
    44<HttpRequest
    55GET:{},
    https://www.example.com/asdf 
    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.utcnow()+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"""
Back to Top