Ticket #7770: cookie-datetime.diff

File cookie-datetime.diff, 1.2 KB (added by Ben Firshman, 16 years ago)
  • django/http/__init__.py

     
    11import os
    22from Cookie import SimpleCookie, CookieError
     3from datetime import datetime, timedelta
    34from pprint import pformat
    45from urllib import urlencode
    56from urlparse import urljoin
     
    322323
    323324    def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
    324325                   domain=None, secure=False):
     326        """Sets a cookie. expires can be a datetime object, a timedelta
     327        object from the current time, or a string in the correct format."""
    325328        self.cookies[key] = value
    326329        if max_age is not None:
    327330            self.cookies[key]['max-age'] = max_age
    328331        if expires is not None:
     332            if isinstance(expires, datetime):
     333                expires = expires.strftime("%a, %d-%b-%Y %H:%M:%S UTC")
     334            elif isinstance(expires, timedelta):
     335                expires = datetime.strftime(datetime.utcnow() + expires, "%a, %d-%b-%Y %H:%M:%S UTC")
    329336            self.cookies[key]['expires'] = expires
    330337        if path is not None:
    331338            self.cookies[key]['path'] = path
Back to Top