On 7/11/09 1:21 PM, Tres Seaver wrote:
> Hash: SHA1
> Iain Duncan wrote:
>> On Wed, 2009-07-08 at 03:28 -0400, Chris McDonough wrote:
>>> On 7/8/09 2:54 AM, Iain Duncan wrote:
>>>> On Wed, 2009-07-08 at 02:36 -0400, Chris McDonough wrote:
>>>>> We don't have a convention for "flash".  I've been remiss in seeing how 
>>>>> the
>>>>> other frameworks implement this, but I *think* something like this would 
>>>>> be closest:
>>>>> return render_template_to_response('some/template.pt', message='Updated')
>>>>> And in the template:
>>>>> <div class="someclass">${message}</div>
>>>> Thanks Chris. The convention in the other frameworks that I've used is
>>>> that the message is 'flashed' when handling the update from a post, and
>>>> displayed after the redirect, to avoid the problem when a user hits
>>>> resend after a post that should not occur twice. So usually the flash
>>>> helper is a simple utility to set, read, and wipe a session variable or
>>>> cookie. I guess bfg is not making any assumptions about session vars or
>>>> cookies though?
>>> Since BFG depends on no particular persistence mechanism, BFG has no 
>>> built-in
>>> session support, although I often use this pair of packages to get it:
>>> http://pypi.python.org/pypi/repoze.browserid/0.1
>>> http://pypi.python.org/pypi/repoze.session/0.2
>>> Of course any sessioning system would work just as well (ala Beaker).
>>>> If you are thinking of adding such a thing to bfg, I'll hold off on my
>>>> own ( or use a really crappy one ).
>>> No, I'm not.
>>>> Can we set a simple cookie with
>>>> webob maybe as an ultralight version that doesn't depend on a particular
>>>> method of storing session vars?
>> At the moment I'm going for adding the message to the redirect url as a
>> get var, ugly but easy to scale. If anyone has other suggestions for how
>> to pass it on through the HTTPFound object, I'd love to hear them. I'm
>> pretty happy that the get var method requires no sessions or cookies
>> though, and it really doesn't look all that odd:
>> /pet/1/edit?_msg="Your+changes+have+been+saved"
>> I guess if I wanted to publish this app down the road as a repoze
>> contribution it would be best not to add any session/cookie dependencies
>> and just make it dead easy for others to change how flash messages are
>> set and consumed eh?

I think some package which set and consumed a cookie value cookie would be 
fine... it would be possible to supply a package that offers a "flash" function 
with a notional API like this:

def view(context, request):
     ... do work ...
     response = HTTPFound(location="/some/other/place)
     flash(response, 'Your message has been saved')

The view that renders as a result of the redirect at /some/other/place will 
to know how to pull out the flash message from the request (if it's in a 
or from a session object, and it will need to pass it along to the template it 
renders.  For example:

def someotherplace(context, request)
     flashmessage = get_flash_message(request)
     return render_template_to_response('the/template.pt'
                                        flashmessage = flashmessage)

.. and the template would need to know that it should try to render something 
into a div based on the 'flashmessage' value.

BFG templates just have no access to any globals at all.  All the names that 
templates know about must be fed to them by the view.  So this feature is 
largely a matter of cooperation between what the "setting" view does to set the 
flash message and what the "consuming" view (or template) does to consume flash 

This is not really a BFG-framework-level feature in a framework that doesn't 
have globals like Pylons; instead it's an application-level (or 
higher-level-framework) feature that could be satisfied via an add-on package 
folks found the API useful.

- C

Repoze-dev mailing list

Reply via email to