Skip to content

Commit 7c0dd0e

Browse files
grishagrisha
authored andcommitted
Renamed setCookie to addCookie. Also changed addCookie so that you can
just give it a couple of strings as args and it will automatically construct a cookie for you. PR: Obtained from: Submitted by: Reviewed by:
1 parent d9d74ae commit 7c0dd0e

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

Doc/modpython4.tex

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,16 +1616,28 @@ \subsection{Classes\label{pyapi-cookie-classes}}
16161616

16171617
\subsection{Functions\label{pyapi-cookie-func}}
16181618

1619-
\begin{funcdesc}{setCookie}{req, cookie}
1619+
\begin{funcdesc}{addCookie}{req, cookie\optional{, value, attributes}}
16201620
This is a convenience function for setting a cookie in request
1621-
headers. \var{req} is a mod_python \class{Request} object,
1622-
\var{cookie} is an object whose string representation is a valid
1623-
cookie. (Most often it is an instance of mod_python \class{Cookie}
1624-
or any of its subclasses).
1621+
headers. \var{req} is a mod_python \class{Request} object. If
1622+
\var{cookie} is an instance of \class{Cookie} (or subclass thereof),
1623+
then the cookie is set, otherwise, \var{cookie} must be a string, in
1624+
which case a \class{Cookie} is constructed using \var{cookie} as
1625+
name, \var{value} as the value, along with any valid \class{Cookie}
1626+
attributes specified as keyword arguments.
16251627

16261628
This function will also set \samp{Cache-Control:
16271629
no-cache="set-cookie"} header to inform caches that the cookie value
16281630
should not be cached.
1631+
1632+
Here is one way to use this function:
1633+
\begin{verbatim}
1634+
c = Cookie.Cookie('spam', 'eggs', expires=time.time()+300)
1635+
Cookie.addCookie(req, c)
1636+
\end{verbatim}
1637+
Here is another:
1638+
\begin{verbatim}
1639+
Cookie.addCookie(req, 'spam', 'eggs', expires=time.time()+300)
1640+
\end{verbatim}
16291641
\end{funcdesc}
16301642

16311643
\begin{funcdesc}{getCookies}{req \optional{, Class, data}}
@@ -1651,7 +1663,7 @@ \subsection{Examples\label{pyapi-cookie-example}}
16511663
16521664
cookie = Cookie.Cookie('eggs', 'spam')
16531665
cookie.expires = time.time() + 300
1654-
Cookie.setCookie(req, cookie)
1666+
Cookie.addCookie(req, cookie)
16551667
16561668
req.write('This response contains a cookie!\n')
16571669
return apache.OK
@@ -1685,7 +1697,7 @@ \subsection{Examples\label{pyapi-cookie-example}}
16851697
16861698
# MarshaCookie allows value to be any marshallable object
16871699
value = {'egg_count': 32, 'color': 'white'}
1688-
Cookie.setCookie(req, Cookie.MarshalCookie('spam', value, \
1700+
Cookie.addCookie(req, Cookie.MarshalCookie('spam', value, \
16891701
'secret007'))
16901702
req.write('Spam cookie not found, but we just set one!\n')
16911703

lib/python/mod_python/Cookie.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#
5555
# Originally developed by Gregory Trubetskoy.
5656
#
57-
# $Id: Cookie.py,v 1.7 2003/07/24 20:51:08 grisha Exp $
57+
# $Id: Cookie.py,v 1.8 2003/07/26 19:25:18 grisha Exp $
5858

5959
"""
6060
@@ -374,11 +374,17 @@ def _parseCookie(str, Class):
374374

375375
return result
376376

377-
def setCookie(req, cookie):
377+
def addCookie(req, cookie, value="", **kw):
378378
"""
379379
Sets a cookie in outgoing headers and adds a cache
380380
directive so that caches don't cache the cookie.
381381
"""
382+
383+
# is this a cookie?
384+
if not isinstance(cookie, Cookie):
385+
386+
# make a cookie
387+
cookie = Cookie(cookie, value, **kw)
382388

383389
if not req.headers_out.has_key("Set-Cookie"):
384390
req.headers_out.add("Cache-Control", 'no-cache="set-cookie"')

0 commit comments

Comments
 (0)