Ian, oh lord of the middleware, I'd love to get your opinion on this:

---------- Forwarded message ----------
From: Shannon -jj Behrens <[EMAIL PROTECTED]>
Date: Nov 2, 2006 3:27 PM
Subject: Re: setting a cookie whether a redirect happens or not
To: [email protected]
Cc: Todd Agulnick <[EMAIL PROTECTED]>


On 11/2/06, Shannon -jj Behrens <[EMAIL PROTECTED]> wrote:
> I like to have a facility called "action_results".  It's a place where
> I can put a message that will show up on the next page.  It should
> work whether a redirect happens or not.  In my old days, I use to put
> it in the session so that it would survive if a redirect happened
> between the time that the controller set it and the time that the
> template showed it.  A redirect is common after a successful post, of
> course.
>
> These days I'm trying to shy away from using the session for this
> purpose.  I plan on having many Web servers for my service, and so
> sessions are going to cost more.  If I can get away from using a
> session for this very common need, that's better.  The obvious thing
> to do is to use a cookie.
>
> In my old system, I could set a cookie regardless of whether I was
> going to show a template or do a redirect.  However, that's not the
> case in Pylons.  It's actually quite a pain to try to set a cookie if
> some other code is right about to do a "h.redirect_to".  That's
> because you need a response in order to set a cookie, and when you use
> "h.redirect_to", you never even see the response object.  In my old
> system, "ctx.response.cookie" was an instance of "Cookie.SimpleCookie"
> from the Python standard library.  It was available regardless of
> whether a redirect was going to happen.
>
> Now, I guess I could avoid all use of "h.redirect_to" and always
> generate a response specifically.  I guess I could also pull off some
> middleware magic.  I could probably also catch the exception that
> "h.redirect_to" raises and do something intelligent.  None of these
> options sounds especially appealing, so I'm wondering if the list has
> any easier solutions.

Another option would be to create a piece of middleware that looks for
"environ['response.cookie']".  This would be an instance of
"Cookie.SimpleCookie" from the Python standard library.  This piece of
middleware would need to be "lower" in middleware.py than
"httpexceptions.make_middleware" in order to set cookies even when an
exception occurs.

Just a Thought,
-jj

--
http://jjinux.blogspot.com/

_______________________________________________
Paste-users mailing list
[email protected]
http://webwareforpython.org/cgi-bin/mailman/listinfo/paste-users

Reply via email to