librelist archives

« back to archive

Categorized flash messages

Categorized flash messages

From:
Dan Jacob
Date:
2010-05-07 @ 17:22
Would it be a good idea to allow categorized flash messages ?

Something like:

flash("Well done", "success")
flash("Oops", "error")

And in your template:

{% for message, category in get_flashed_messages() %}
<li class="{{ category }}">{{ message }}</li>
{% endfor %}

Re: [flask] Categorized flash messages

From:
Armin Ronacher
Date:
2010-05-07 @ 17:43
Hi,

On 5/7/10 7:22 PM, Dan Jacob wrote:
> Would it be a good idea to allow categorized flash messages ?
It would be a good idea but it would break a lot of existing code. 
That's something other people have to demand as well for me to make the 
change :)


Regards,
Armin

Re: [flask] Categorized flash messages

From:
Dan Jacob
Date:
2010-05-07 @ 17:46
Should be easy enough to override - subject for another snippet :-)

On 7 May 2010 18:43, Armin Ronacher <armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 5/7/10 7:22 PM, Dan Jacob wrote:
>> Would it be a good idea to allow categorized flash messages ?
> It would be a good idea but it would break a lot of existing code.
> That's something other people have to demand as well for me to make the
> change :)
>
>
> Regards,
> Armin
>

Re: [flask] Categorized flash messages

From:
Thadeus Burgess
Date:
2010-05-07 @ 17:52
I like this idea as well. You should be able to keep compatibility,
ie: if they don't specify a category use something like "__default__"
?

--
Thadeus





On Fri, May 7, 2010 at 12:46 PM, Dan Jacob <danjac354@gmail.com> wrote:
> Should be easy enough to override - subject for another snippet :-)
>
> On 7 May 2010 18:43, Armin Ronacher <armin.ronacher@active-4.com> wrote:
>> Hi,
>>
>> On 5/7/10 7:22 PM, Dan Jacob wrote:
>>> Would it be a good idea to allow categorized flash messages ?
>> It would be a good idea but it would break a lot of existing code.
>> That's something other people have to demand as well for me to make the
>> change :)
>>
>>
>> Regards,
>> Armin
>>
>

Re: [flask] Categorized flash messages

From:
Armin Ronacher
Date:
2010-05-07 @ 17:54
Hi,

On 5/7/10 7:52 PM, Thadeus Burgess wrote:
> I like this idea as well. You should be able to keep compatibility,
> ie: if they don't specify a category use something like "__default__"
That is not the problem, the problem is the template:

    {% for msg in get_flashed_messages() %}
      <p class=message>{{ msg }}
    {% endfor %}

If msg would suddenly be a tuple, everybody would have to change the 
layout template.  Flask is young so it's probably okay to change that, 
but it's still annoying for everybody involved.


Regards,
Armin

Re: [flask] Categorized flash messages

From:
Thadeus Burgess
Date:
2010-05-07 @ 18:26
What if you must pass an argument to
get_flashed_messages(category="__default__")

That way everything continues to work the way it is.

--
Thadeus





On Fri, May 7, 2010 at 12:54 PM, Armin Ronacher
<armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 5/7/10 7:52 PM, Thadeus Burgess wrote:
>> I like this idea as well. You should be able to keep compatibility,
>> ie: if they don't specify a category use something like "__default__"
> That is not the problem, the problem is the template:
>
>    {% for msg in get_flashed_messages() %}
>      <p class=message>{{ msg }}
>    {% endfor %}
>
> If msg would suddenly be a tuple, everybody would have to change the
> layout template.  Flask is young so it's probably okay to change that,
> but it's still annoying for everybody involved.
>
>
> Regards,
> Armin
>

Re: [flask] Categorized flash messages

From:
Steve Losh
Date:
2010-05-07 @ 18:45
On May 7, 2010, at 2:26 PM, Thadeus Burgess wrote:

> What if you must pass an argument to
> get_flashed_messages(category="__default__")
> 

Solution: make a Message class that implements __unicode__() to avoid 
breaking existing code.

You can use {{ msg }} as usual in templates, get at the category with {{ 
msg.category }}, and can easily add it with flash(message,'warning') or 
flash(message, category='warning') to be explicit.

For example:


diff --git a/flask.py b/flask.py
--- a/flask.py
+++ b/flask.py
@@ -145,4 +145,12 @@
 
 
+class Message(object):
+    def __init__(self, content, category=None):
+        self.content = content
+        self.category = None
+
+    def __unicode__(self):
+        return unicode(self.content)
+
 def url_for(endpoint, **values):
     """Generates a URL to the given endpoint with the method provided.
@@ -184,5 +192,5 @@
 
 
-def flash(message):
+def flash(message, category=None):
     """Flashes a message to the next request.  In order to remove the
     flashed message from the session and to display it to the user,
@@ -191,5 +199,6 @@
     :param message: the message to be flashed.
     """
-    session.setdefault('_flashes', []).append(message)
+    msg = Message(message, category)
+    session.setdefault('_flashes', []).append(msg)


> That way everything continues to work the way it is.
> 
> --
> Thadeus
> 
> 
> 
> 
> 
> On Fri, May 7, 2010 at 12:54 PM, Armin Ronacher
> <armin.ronacher@active-4.com> wrote:
>> Hi,
>> 
>> On 5/7/10 7:52 PM, Thadeus Burgess wrote:
>>> I like this idea as well. You should be able to keep compatibility,
>>> ie: if they don't specify a category use something like "__default__"
>> That is not the problem, the problem is the template:
>> 
>>    {% for msg in get_flashed_messages() %}
>>      <p class=message>{{ msg }}
>>    {% endfor %}
>> 
>> If msg would suddenly be a tuple, everybody would have to change the
>> layout template.  Flask is young so it's probably okay to change that,
>> but it's still annoying for everybody involved.
>> 
>> 
>> Regards,
>> Armin
>> 

Re: [flask] Categorized flash messages

From:
Dan Jacob
Date:
2010-05-07 @ 21:04
On 7 May 2010 19:45, Steve Losh <steve@stevelosh.com> wrote:
> On May 7, 2010, at 2:26 PM, Thadeus Burgess wrote:
>
>> What if you must pass an argument to
>> get_flashed_messages(category="__default__")
>>
>
> Solution: make a Message class that implements __unicode__() to avoid 
breaking existing code.

+1 to this solution, as it doesn't break backward compatibility.

>
> You can use {{ msg }} as usual in templates, get at the category with {{
msg.category }}, and can easily add it with flash(message,'warning') or 
flash(message, category='warning') to be explicit.
>
> For example:
>
>
> diff --git a/flask.py b/flask.py
> --- a/flask.py
> +++ b/flask.py
> @@ -145,4 +145,12 @@
>
>
> +class Message(object):
> +    def __init__(self, content, category=None):
> +        self.content = content
> +        self.category = None
> +
> +    def __unicode__(self):
> +        return unicode(self.content)
> +
>  def url_for(endpoint, **values):
>     """Generates a URL to the given endpoint with the method provided.
> @@ -184,5 +192,5 @@
>
>
> -def flash(message):
> +def flash(message, category=None):
>     """Flashes a message to the next request.  In order to remove the
>     flashed message from the session and to display it to the user,
> @@ -191,5 +199,6 @@
>     :param message: the message to be flashed.
>     """
> -    session.setdefault('_flashes', []).append(message)
> +    msg = Message(message, category)
> +    session.setdefault('_flashes', []).append(msg)
>
>
>> That way everything continues to work the way it is.
>>
>> --
>> Thadeus
>>
>>
>>
>>
>>
>> On Fri, May 7, 2010 at 12:54 PM, Armin Ronacher
>> <armin.ronacher@active-4.com> wrote:
>>> Hi,
>>>
>>> On 5/7/10 7:52 PM, Thadeus Burgess wrote:
>>>> I like this idea as well. You should be able to keep compatibility,
>>>> ie: if they don't specify a category use something like "__default__"
>>> That is not the problem, the problem is the template:
>>>
>>>    {% for msg in get_flashed_messages() %}
>>>      <p class=message>{{ msg }}
>>>    {% endfor %}
>>>
>>> If msg would suddenly be a tuple, everybody would have to change the
>>> layout template.  Flask is young so it's probably okay to change that,
>>> but it's still annoying for everybody involved.
>>>
>>>
>>> Regards,
>>> Armin
>>>
>
>