I'd like to show my suggestion to the problem. It is based on
[EMAIL PROTECTED] 's solution (some posts above), but it is a bit
simpler:

 * It also handles multiple messages
 * also based on session.
 * accept html
 * the error type automatically  produces the correspondent CSS class.

It's supposed to be used with genshi. There is a snippet of the
master.html template:

        <div py:if="'flash' in cherrypy.session">
                <p py:for="message in cherrypy.session['flash']"
py:content="message.to_genshi()" py:attrs="message.attrs">Flash
Message</p>
        </div>

The flash function could be used as:

flash('Item <strong>removed</strong>') or
flash('Invalid date', message_type='error')


Finally, the code:
-----------------------------

# -*- coding: utf-8 -*-

# inspired on 
http://www.nabble.com/My-turbogears.flash()-alternative-t3896614.html

import cherrypy
import genshi

class FlashMessage(object):
        def __init__(self, message, message_type):
                self.message      = message
                self.message_type = message_type

        def __repr__(self):
                return self.message

        @property
        def attrs(self):
                return {'class': '%s' % self.message_type}

        def to_genshi(self):
                return genshi.Markup(self.message)


class FlashMessagesIterator(object):
        def __init__(self):
                self.messages = list()

        def append(self, message):
                self.messages.append(message)

        def __iter__(self):
                return self

        def next(self):
                if len(self.messages):
                        return self.messages.pop(0)
                else:
                        raise StopIteration


def flash(message, message_type = 'notice'):
        if 'flash' not in cherrypy.session:
                cherrypy.session['flash'] = FlashMessagesIterator()

        flash_message = FlashMessage(message, message_type)
        cherrypy.session['flash'].append(flash_message)


def flash_errors(tg_errors):
        for field, error in tg_errors.items():
                message = '%s: %s' % (field, error)
                flash(message = message, message_type = 'error')


--------------

Regards,

André



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to turbogears@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to