I've updated my local tg_flash routines to support pickling - my application 
requires a dictionary with "class", "label", and "message" elements, for 
example.

The change is 100% backwards compatible and adds a few bytes of overhead in 
the general case of a simple string.  Tests could be made to prevent even 
that.

In controllers.py from the turbogears application directory:

>def flash(message):
>    """Set a message to be displayed in the browser on next page display"""
>    import pickle
>    encoded = pickle.dumps(message)
>    cherrypy.response.simpleCookie['tg_flash'] = encoded
>    cherrypy.response.simpleCookie['tg_flash']['path'] = '/'
>
>def _get_flash():
>    """Retrieve the flash message, if one is set. The message is cleared
>    after retrieval."""
>    request_cookie = cherrypy.request.simpleCookie
>    response_cookie = cherrypy.response.simpleCookie
>
>    if request_cookie.has_key("tg_flash"):
>        message = request_cookie["tg_flash"].value
>        if not response_cookie.has_key("tg_flash"):
>            response_cookie["tg_flash"] = ""
>            response_cookie["tg_flash"]['expires'] = 0
>            response_cookie['tg_flash']['path'] = '/'
>    elif response_cookie.has_key("tg_flash"):
>        message = response_cookie["tg_flash"].value
>        response_cookie.pop("tg_flash")
>    else:
>        message = None
>    if message:
>        import pickle
>        message = pickle.loads(message)
>    return message

My KID template is thus:

>       <div py:if="tg_flash" id="flash" py:attrs="class=tg_flash['class']">
>               <label py:content="tg_flash['label']" />
>               <div py:content="tg_flash['message']" />
>       </div>

I attempted to make a patch, but I borked it at the last moment and now have 
no source reference... d'oh!

Reply via email to