mirror of https://github.com/django/django.git
Fixed #17931 -- Accepted aware datetimes to set cookies expiry dates. Thanks jaddison for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17766 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
c7cc4cfb9e
commit
c8e2f7591d
|
@ -121,6 +121,7 @@ from django.http.utils import *
|
||||||
from django.utils.datastructures import MultiValueDict, ImmutableList
|
from django.utils.datastructures import MultiValueDict, ImmutableList
|
||||||
from django.utils.encoding import smart_str, iri_to_uri, force_unicode
|
from django.utils.encoding import smart_str, iri_to_uri, force_unicode
|
||||||
from django.utils.http import cookie_date
|
from django.utils.http import cookie_date
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
|
RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
|
||||||
|
|
||||||
|
@ -641,13 +642,18 @@ class HttpResponse(object):
|
||||||
"""
|
"""
|
||||||
Sets a cookie.
|
Sets a cookie.
|
||||||
|
|
||||||
``expires`` can be a string in the correct format or a
|
``expires`` can be:
|
||||||
``datetime.datetime`` object in UTC. If ``expires`` is a datetime
|
- a string in the correct format,
|
||||||
object then ``max_age`` will be calculated.
|
- 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
|
self.cookies[key] = value
|
||||||
if expires is not None:
|
if expires is not None:
|
||||||
if isinstance(expires, datetime.datetime):
|
if isinstance(expires, datetime.datetime):
|
||||||
|
if timezone.is_aware(expires):
|
||||||
|
expires = timezone.make_naive(expires, timezone.utc)
|
||||||
delta = expires - expires.utcnow()
|
delta = expires - expires.utcnow()
|
||||||
# Add one second so the date matches exactly (a fraction of
|
# Add one second so the date matches exactly (a fraction of
|
||||||
# time gets lost between converting to a timedelta and
|
# time gets lost between converting to a timedelta and
|
||||||
|
|
|
@ -12,6 +12,7 @@ from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_r
|
||||||
from django.test.utils import get_warnings_state, restore_warnings_state
|
from django.test.utils import get_warnings_state, restore_warnings_state
|
||||||
from django.utils import unittest
|
from django.utils import unittest
|
||||||
from django.utils.http import cookie_date
|
from django.utils.http import cookie_date
|
||||||
|
from django.utils.timezone import utc
|
||||||
|
|
||||||
|
|
||||||
class RequestsTests(unittest.TestCase):
|
class RequestsTests(unittest.TestCase):
|
||||||
|
@ -207,6 +208,15 @@ class RequestsTests(unittest.TestCase):
|
||||||
datetime_cookie = response.cookies['datetime']
|
datetime_cookie = response.cookies['datetime']
|
||||||
self.assertEqual(datetime_cookie['max-age'], 10)
|
self.assertEqual(datetime_cookie['max-age'], 10)
|
||||||
|
|
||||||
|
def test_aware_expiration(self):
|
||||||
|
"Cookie accepts an aware datetime as expiration time"
|
||||||
|
response = HttpResponse()
|
||||||
|
expires = (datetime.utcnow() + timedelta(seconds=10)).replace(tzinfo=utc)
|
||||||
|
time.sleep(0.001)
|
||||||
|
response.set_cookie('datetime', expires=expires)
|
||||||
|
datetime_cookie = response.cookies['datetime']
|
||||||
|
self.assertEqual(datetime_cookie['max-age'], 10)
|
||||||
|
|
||||||
def test_far_expiration(self):
|
def test_far_expiration(self):
|
||||||
"Cookie will expire when an distant expiration time is provided"
|
"Cookie will expire when an distant expiration time is provided"
|
||||||
response = HttpResponse()
|
response = HttpResponse()
|
||||||
|
|
Loading…
Reference in New Issue