summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBert JW Regeer <bertjw@regeer.org>2014-11-08 21:59:42 -0700
committerBert JW Regeer <bertjw@regeer.org>2015-03-22 21:36:43 -0600
commit8e1db14a6ad2c1e2188b2265845bc3a3818ab836 (patch)
tree4c6e01fa7a04989cb2a1506ce33ae56f0044b5ee
parent7cc8094a80fb5700c268b3bf01ebc3075b59e363 (diff)
downloadwebob-8e1db14a6ad2c1e2188b2265845bc3a3818ab836.tar.gz
Refactor the respnse.set_cookie
Start using the make_cookie call that is available in webob.cookies, this way we don't duplicate the same behavior. There are a couple of backwards compatible fixes: - If expires is set to a timedelta, and max_age is not set, we set max_age to expires a timedelta - expires can also be a datetime, however this was not documented. So if it is a datetime, we want to get a timedelta, by taking the existing expires value, and removing datetime.utcnow() from the value.
-rw-r--r--webob/response.py40
1 files changed, 17 insertions, 23 deletions
diff --git a/webob/response.py b/webob/response.py
index 05e392f..d720138 100644
--- a/webob/response.py
+++ b/webob/response.py
@@ -31,6 +31,7 @@ from webob.compat import (
from webob.cookies import (
Cookie,
Morsel,
+ make_cookie,
)
from webob.datetime_utils import (
@@ -690,7 +691,7 @@ class Response(object):
# set_cookie, unset_cookie, delete_cookie, merge_cookies
#
- def set_cookie(self, key, value='', max_age=None,
+ def set_cookie(self, name, value='', max_age=None,
path='/', domain=None, secure=False, httponly=False,
comment=None, expires=None, overwrite=False):
"""
@@ -698,7 +699,7 @@ class Response(object):
Arguments are:
- ``key``
+ ``name``
The cookie name.
@@ -766,28 +767,21 @@ class Response(object):
"""
if overwrite:
- self.unset_cookie(key, strict=False)
- if value is None: # delete the cookie from the client
- value = ''
- max_age = 0
- expires = timedelta(days=-5)
- elif expires is None and max_age is not None:
- if isinstance(max_age, int):
- max_age = timedelta(seconds=max_age)
- expires = datetime.utcnow() + max_age
- elif max_age is None and expires is not None:
+ self.unset_cookie(name, strict=False)
+
+ # If expires is set, but not max_age we set max_age to expires
+ if not max_age and isinstance(expires, timedelta):
+ max_age = expires
+
+ # expires can also be a datetime
+ if not max_age and isinstance(expires, datetime):
max_age = expires - datetime.utcnow()
- value = bytes_(value, 'utf8')
- key = bytes_(key, 'utf8')
- m = Morsel(key, value)
- m.path = bytes_(path, 'utf8')
- m.domain = bytes_(domain, 'utf8')
- m.comment = bytes_(comment, 'utf8')
- m.expires = expires
- m.max_age = max_age
- m.secure = secure
- m.httponly = httponly
- self.headerlist.append(('Set-Cookie', m.serialize()))
+
+ cookie = make_cookie(name, value, max_age=max_age, path=path,
+ domain=domain, secure=secure, httponly=httponly,
+ comment=comment)
+
+ self.headerlist.append(('Set-Cookie', cookie))
def delete_cookie(self, name, path='/', domain=None):
"""