From 390666ac2bf8223bede4f78a97836051bc9f9526 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 1 Nov 2005 01:02:07 +0000 Subject: [PATCH] Fixed #508 -- Added support for 'expires' in cookies and changed session middleware to set 'expires' in addition to 'max_age'. Thanks, mark@junklight.com git-svn-id: http://code.djangoproject.com/svn/django/trunk@1035 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/middleware/sessions.py | 3 ++- django/utils/httpwrappers.py | 4 ++-- docs/request_response.txt | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/django/middleware/sessions.py b/django/middleware/sessions.py index 42b2118410..8b9f21f78d 100644 --- a/django/middleware/sessions.py +++ b/django/middleware/sessions.py @@ -71,6 +71,7 @@ class SessionMiddleware: session_key = request.session.session_key or sessions.get_new_session_key() new_session = sessions.save(session_key, request.session._session, datetime.datetime.now() + datetime.timedelta(seconds=SESSION_COOKIE_AGE)) + expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT") response.set_cookie(SESSION_COOKIE_NAME, session_key, - max_age=SESSION_COOKIE_AGE, domain=SESSION_COOKIE_DOMAIN) + max_age=SESSION_COOKIE_AGE, expires=expires, domain=SESSION_COOKIE_DOMAIN) return response diff --git a/django/utils/httpwrappers.py b/django/utils/httpwrappers.py index 5f9362bd24..c1aa9d6ee1 100644 --- a/django/utils/httpwrappers.py +++ b/django/utils/httpwrappers.py @@ -172,9 +172,9 @@ class HttpResponse: return True return False - def set_cookie(self, key, value='', max_age=None, path='/', domain=None, secure=None): + def set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=None): self.cookies[key] = value - for var in ('max_age', 'path', 'domain', 'secure'): + for var in ('max_age', 'path', 'domain', 'secure', 'expires'): val = locals()[var] if val is not None: self.cookies[key][var.replace('_', '-')] = val diff --git a/docs/request_response.txt b/docs/request_response.txt index 150a5bc92c..85a5e091db 100644 --- a/docs/request_response.txt +++ b/docs/request_response.txt @@ -284,12 +284,14 @@ Methods Returns ``True`` or ``False`` based on a case-insensitive check for a header with the given name. -``set_cookie(key, value='', max_age=None, path='/', domain=None, secure=None)`` +``set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None)`` Sets a cookie. The parameters are the same as in the `cookie Morsel`_ object in the Python standard library. * ``max_age`` should be a number of seconds, or ``None`` (default) if the cookie should last only as long as the client's browser session. + * ``expires`` should be a string in the format + ``"Wdy, DD-Mon-YY HH:MM:SS GMT"``. * Use ``domain`` if you want to set a cross-domain cookie. For example, ``domain=".lawrence.com"`` will set a cookie that is readable by the domains www.lawrence.com, blogs.lawrence.com and