Ticket #7770: 7770.diff
File 7770.diff, 3.3 KB (added by , 16 years ago) |
---|
-
tests/regressiontests/requests/tests.py
1 1 """ 2 >>> from django.http import HttpRequest 2 >>> from django.http import HttpRequest, HttpResponse 3 3 >>> print repr(HttpRequest()) 4 4 <HttpRequest 5 5 GET:{}, … … 44 44 >>> request.path = '' 45 45 >>> print request.build_absolute_uri(location="/path/with:colons") 46 46 http://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'] 58 10 59 >>> datetime_cookie['max-age'] == timedelta_cookie['max-age'] 60 True 61 >>> datetime_cookie['expires'] == timedelta_cookie['expires'] 62 True 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' 47 66 """ -
django/http/__init__.py
1 1 import os 2 2 import re 3 import time 4 import datetime 3 5 from Cookie import SimpleCookie, CookieError 4 6 from pprint import pformat 5 7 from urllib import urlencode … … 12 14 13 15 from django.utils.datastructures import MultiValueDict, ImmutableList 14 16 from django.utils.encoding import smart_str, iri_to_uri, force_unicode 17 from django.utils.http import cookie_date 15 18 from django.http.multipartparser import MultiPartParser 16 19 from django.conf import settings 17 20 from django.core.files import uploadhandler … … 337 340 338 341 def set_cookie(self, key, value='', max_age=None, expires=None, path='/', 339 342 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 """ 340 350 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 341 362 if max_age is not None: 342 363 self.cookies[key]['max-age'] = max_age 343 if expires is not None:344 self.cookies[key]['expires'] = expires345 364 if path is not None: 346 365 self.cookies[key]['path'] = path 347 366 if domain is not None: