Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?

2011-03-12 Thread Jeff Dairiki
On Thu, Mar 10, 2011 at 01:07:32PM -0800, Seth wrote:

 Are those of us not returning real Response objects stuck with building 
 our cookie strings manually, or am I missing something here?

I think you should be able to use a sacrificial WebOb Response object
to construct the cookies for you.

Here's an untested example of what I mean:

import webob   

res = webob.Response()
res.set_cookie(cookie_name, cookie_value,
   path='/', max_age=max_age, httponly=True)
request.response_headerlist =  [
('Set-Cookie', _)
for _ in res.headers.getall('Set-Cookie') ]

Jeff

-- 
You received this message because you are subscribed to the Google Groups 
pylons-devel group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.



Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?

2011-03-12 Thread Michael Merickel
Going through the trouble of creating a webob response is much more
complicated than simply adding a response_callback.

def _set_cookie(request, response):
response.set_cookie()
request.add_response_callback(_set_cookie)

Michael

-- 
You received this message because you are subscribed to the Google Groups 
pylons-devel group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.



Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?

2011-03-12 Thread Jeff Dairiki
On Sat, Mar 12, 2011 at 01:55:21PM -0600, Michael Merickel wrote:
 Going through the trouble of creating a webob response is much more
 complicated than simply adding a response_callback.
 
 def _set_cookie(request, response):
 response.set_cookie()
 request.add_response_callback(_set_cookie)

That does look like generally a better solution than mine.  Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
pylons-devel group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.



Setting cookies in Pyramid for @action()s or HTTPFound redirects?

2011-03-10 Thread Seth
I'm having the hardest time figuring out the best way to set cookies for my 
methods that don't return a true Response object (and therefore, don't 
have a set_cookie() method). Is there no such helper in the Pyramid stack? 
The closest thing I've found so far is the Varying Attributes of Rendered 
Responses section ( http://goo.gl/LwoPc ) but that doesn't really outline a 
helper method, and requires you to build the cookies manually and add them 
to the request.response_headerlist attribute.

Are those of us not returning real Response objects stuck with building 
our cookie strings manually, or am I missing something here?

Thanks,
Seth

-- 
You received this message because you are subscribed to the Google Groups 
pylons-devel group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.



Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?

2011-03-10 Thread Stephen Lacy
To me, the answer to this question really lies in what session
implementation are you using?

For me, I've opted to use a session in a database on the server, which
allows me to set arbitrarily large items into the session without any real
penalty.

Then, for whatever you'd set a custom cookie for, I just set an attribute on
request.session.  Usually, this is something that would trigger some action
on the next page, so in the next page when I take the action, I delete the
session value just to keep things clean.

If you're not using server-side sessions, then you could do the same, but
remember to keep your values small.

Another alternative is to register a response callback on the request
(request.add_response_callback(...)) and thath method will be passed a
response object that you can then call set_cookie on no matter what the
actual response code is.

Steve

On Thu, Mar 10, 2011 at 1:07 PM, Seth seedifferen...@gmail.com wrote:

 I'm having the hardest time figuring out the best way to set cookies for my
 methods that don't return a true Response object (and therefore, don't
 have a set_cookie() method). Is there no such helper in the Pyramid stack?
 The closest thing I've found so far is the Varying Attributes of Rendered
 Responses section ( http://goo.gl/LwoPc ) but that doesn't really outline
 a helper method, and requires you to build the cookies manually and add them
 to the request.response_headerlist attribute.

 Are those of us not returning real Response objects stuck with building
 our cookie strings manually, or am I missing something here?

 Thanks,
 Seth

 --
 You received this message because you are subscribed to the Google Groups
 pylons-devel group.
 To post to this group, send email to pylons-devel@googlegroups.com.
 To unsubscribe from this group, send email to
 pylons-devel+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/pylons-devel?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
pylons-devel group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.



Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?

2011-03-10 Thread Seth
Currently I'm using the default cookie/session factories, but I'm looking to 
actually set cookies that last longer than the session so the 
request.session solution doesn't apply (unless I'm missing something 
there--I don't think it can be given a max_age). The callback method you 
suggested could work, but to me that's not really any simpler of a solution 
than creating the cookie manually and adding it to 
request.response_headerlist.


-- 
You received this message because you are subscribed to the Google Groups 
pylons-devel group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.