My flash() solution

6 views
Skip to first unread message

AZMel

unread,
Jun 7, 2007, 3:27:25 PM6/7/07
to TurboGears
I have not been able to find a solution that was simple to be able to
change the color of the flash box so I came up with this one.

It is a very simple but very effective solution for me and can be used
without having to recode any flash messages that are considered as
"green". You know, the ones that have the check mark and the message
that the operation worked fine.

Start off with modify style.css and changing the #status_block to:

#status_block {
margin: 0 auto 0.5em auto;
padding: 15px 10px 15px 55px;
background: #cec URL('../images/ok.png') left center no-repeat;
border: 1px solid #9c9;
width: 450px;
font-size: 120%;
font-weight: bolder;
}


.error {
margin: 0.5em auto 0.5em auto;
padding: 15px 10px 15px 55px;
width: 450px;
background: #C00 URL('../images/error.png') left center no-repeat;
border: 1px solid #600;
font-size: 120%;
font-weight: bolder;
color: #fff;
}


.warning {
margin: 0.5em auto 0.5em auto;
padding: 15px 10px 15px 55px;
width: 450px;
background: #fe4 URL('../images/warning.png') left center no-repeat;
border: 1px solid #cb3;
font-size: 120%;
font-weight: bolder;
}

.notice {
margin: 0.5em auto 0.5em auto;
padding: 15px 10px 15px 55px;
width: 450px;
background: #eef URL('../images/info.png') left center no-repeat;
border: 1px solid #cce;
}

Be sure you have png files for the graphics you want to use.


You can add any others you want or completely rewrite what I have.


Then you change the code where the flash is being display. I am still
using the master.kid that was created by CRUD.

<div id="main_content">
<span py:if="not tg_flash.startswith('%%')">
<div id="status_block" py:if="tg_flash" class="flash"
py:content="tg_flash"></div>
</span>
<span py:if="tg_flash.startswith('%%')">
<span py:if="tg_flash.startswith('%%warning%%')">
<div id="warning" py:if="tg_flash" class="warning"
py:content="tg_flash.split('%%')[2]"></div>
</span>
<span py:if="tg_flash.startswith('%%error%%')">
<div id="error" py:if="tg_flash" class="error"
py:content="tg_flash.split('%%')[2]"></div>
</span>
<span py:if="tg_flash.startswith('%%ok%%')">
<div id="flash" py:if="tg_flash" class="flash"
py:content="tg_flash.split('%%')[2]"></div>
</span>
</span>
<div py:replace="[item.text]+item[:]"/>
</div>


Last you recode the flash statements in your controller by adding %
%warning%%, %%error%%, or %%ok%%. If you don't use any of these then
the default green box will be used.

Some examples of what I use are:

flash("%%error%%Unable to destroy. Child records still
exist.")
flash("Division was successfully destroyed.")
flash("%%ok%%Division was successfully created.")
flash("%%warning%%Serial Number already exists.")

You can extend this anyway you want.

Hope this helps anyone out there who is as frustrated with flash as I
was.

Mel

AZMel

unread,
Jun 7, 2007, 3:33:22 PM6/7/07
to TurboGears
Sorry, I posted a bugged version. Replace:

> <div id="main_content">
> <span py:if="not tg_flash.startswith('%%')">
> <div id="status_block" py:if="tg_flash" class="flash"
> py:content="tg_flash"></div>
> </span>
> <span py:if="tg_flash.startswith('%%')">
> <span py:if="tg_flash.startswith('%%warning%%')">
> <div id="warning" py:if="tg_flash" class="warning"
> py:content="tg_flash.split('%%')[2]"></div>
> </span>
> <span py:if="tg_flash.startswith('%%error%%')">
> <div id="error" py:if="tg_flash" class="error"
> py:content="tg_flash.split('%%')[2]"></div>
> </span>
> <span py:if="tg_flash.startswith('%%ok%%')">
> <div id="flash" py:if="tg_flash" class="flash"
> py:content="tg_flash.split('%%')[2]"></div>
> </span>
> </span>
> <div py:replace="[item.text]+item[:]"/>
> </div>
>

with

<div id="main_content">
<span py:if="tg_flash">


<span py:if="not tg_flash.startswith('%%')">
<div id="status_block" py:if="tg_flash" class="flash"
py:content="tg_flash"></div>
</span>
<span py:if="tg_flash.startswith('%%')">
<span py:if="tg_flash.startswith('%%warning%%')">
<div id="warning" py:if="tg_flash" class="warning"
py:content="tg_flash.split('%%')[2]"></div>
</span>
<span py:if="tg_flash.startswith('%%error%%')">
<div id="error" py:if="tg_flash" class="error"
py:content="tg_flash.split('%%')[2]"></div>
</span>
<span py:if="tg_flash.startswith('%%ok%%')">
<div id="flash" py:if="tg_flash" class="flash"
py:content="tg_flash.split('%%')[2]"></div>
</span>
</span>
</span>
<div py:replace="[item.text]+item[:]"/>
</div>

Thanks,

Mel

Christopher Arndt

unread,
Jun 7, 2007, 3:42:53 PM6/7/07
to turbo...@googlegroups.com
AZMel schrieb:

> I have not been able to find a solution that was simple to be able to
> change the color of the flash box so I came up with this one.

In case you didn't see it, there was a thread about how to enhance
turbogears.flash in different ways, about three weeks ago on this list.
Search the archive for "Flash notes with HTML tags".

Chris

Marco Mariani

unread,
Jun 8, 2007, 5:15:14 AM6/8/07
to turbo...@googlegroups.com
AZMel ha scritto:

> Hope this helps anyone out there who is as frustrated with flash as I was.
>

Yes, it's a common need. This recipe has been independently cooked
several times by different chefs. I did almost the same. Somebody
suggested storing the rendered HTML flash message on the cookie.. I
would rather store it in a server-side session, since cookies can be a
scarce resource, but I develop for intranets, and www needs are
different. I would like to queue _several_ flash messages, for instance.
If I did the same program for the web, I would probably keep the UI
simpler than it is now.

andre...@gmail.com

unread,
Jun 17, 2007, 8:18:29 PM6/17/07
to TurboGears
I'd like to show my suggestion to the problem. It is based on
ans...@gmail.com's solution (http://www.nabble.com/My-
turbogears.flash()-alternative-t3896614.html), 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 -*-

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é


Reply all
Reply to author
Forward
0 new messages