def set_cookie(self, key, value='', max_age=None, path='/', domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False): """ Set (add) a cookie for the response """ 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: 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()))
def set_cookie(self, key, value='', max_age=None, path='/', domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False): """ Set (add) a cookie for the response """ 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: max_age = expires - datetime.utcnow() if isinstance(value, unicode): value = value.encode('utf8') m = Morsel(key, value) m.path = path m.domain = domain m.comment = comment m.expires = expires m.max_age = max_age m.secure = secure m.httponly = httponly self.headerlist.append(('Set-Cookie', str(m)))
def set_cookie( self, key, value="", max_age=None, path="/", domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False, ): """ Set (add) a cookie for the response """ 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: max_age = expires - datetime.utcnow() if isinstance(value, unicode): value = value.encode("utf8") m = Morsel(key, value) m.path = path m.domain = domain m.comment = comment m.expires = expires m.max_age = max_age m.secure = secure m.httponly = httponly self.headerlist.append(("Set-Cookie", str(m)))
def set_cookie(self, key, value='', max_age=None, path='/', domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False): """ Set (add) a cookie for the response. Arguments are: ``key`` The cookie name. ``value`` The cookie value, which should be a string or ``None``. If ``value`` is ``None``, it's equivalent to calling the :meth:`webob.response.Response.unset_cookie` method for this cookie key (it effectively deletes the cookie on the client). ``max_age`` An integer representing a number of seconds or ``None``. If this value is an integer, it is used as the ``Max-Age`` of the generated cookie. If ``expires`` is not passed and this value is an integer, the ``max_age`` value will also influence the ``Expires`` value of the cookie (``Expires`` will be set to now + max_age). If this value is ``None``, the cookie will not have a ``Max-Age`` value (unless ``expires`` is also sent). ``path`` A string representing the cookie ``Path`` value. It defaults to ``/``. ``domain`` A string representing the cookie ``Domain``, or ``None``. If domain is ``None``, no ``Domain`` value will be sent in the cookie. ``secure`` A boolean. If it's ``True``, the ``secure`` flag will be sent in the cookie, if it's ``False``, the ``secure`` flag will not be sent in the cookie. ``httponly`` A boolean. If it's ``True``, the ``HttpOnly`` flag will be sent in the cookie, if it's ``False``, the ``HttpOnly`` flag will not be sent in the cookie. ``comment`` A string representing the cookie ``Comment`` value, or ``None``. If ``comment`` is ``None``, no ``Comment`` value will be sent in the cookie. ``expires`` A ``datetime.timedelta`` object representing an amount of time or the value ``None``. A non-``None`` value is used to generate the ``Expires`` value of the generated cookie. If ``max_age`` is not passed, but this value is not ``None``, it will influence the ``Max-Age`` header (``Max-Age`` will be 'expires_value - datetime.utcnow()'). If this value is ``None``, the ``Expires`` cookie value will be unset (unless ``max_age`` is also passed). ``overwrite`` If this key is ``True``, before setting the cookie, unset any existing cookie. """ 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: 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()))