Browse Source

Added support for flashing categories.

This fixes #35.
pull/1638/head
Armin Ronacher 15 years ago
parent
commit
35ed617fe4
  1. 2
      CHANGES
  2. 33
      flask.py
  3. 29
      tests/flask_tests.py

2
CHANGES

@ -8,6 +8,8 @@ Version 0.5
Release date to be announced
- added support for categories for flashed messages.
Version 0.2
-----------

33
flask.py

@ -217,24 +217,49 @@ def get_template_attribute(template_name, attribute):
attribute)
def flash(message):
def flash(message, category='message'):
"""Flashes a message to the next request. In order to remove the
flashed message from the session and to display it to the user,
the template has to call :func:`get_flashed_messages`.
.. versionchanged: 0.5
`category` parameter added.
:param message: the message to be flashed.
:param category: the category for the message. The following values
are recommended: ``'message'`` for any kind of message,
``'error'`` for errors, ``'info'`` for information
messages and ``'warning'`` for warnings. However any
kind of string and be used as category.
"""
session.setdefault('_flashes', []).append(message)
session.setdefault('_flashes', []).append((category, message))
def get_flashed_messages():
def get_flashed_messages(with_categories=False):
"""Pulls all flashed messages from the session and returns them.
Further calls in the same request to the function will return
the same messages.
the same messages. By default just the messages are returned,
but when `with_categories` is set to `True`, the return value will
be a list of tuples in the form ``(category, message)`` instead.
Example usage:
.. sourcecode:: html+jinja
{% for category, msg in get_flashed_messages(with_categories=true) %}
<p class=flash-{{ category }}>{{ msg }}
{% endfor %}
.. versionchanged:: 0.5
`with_categories` parameter added.
:param with_categories: set to `True` to also receive categories.
"""
flashes = _request_ctx_stack.top.flashes
if flashes is None:
_request_ctx_stack.top.flashes = flashes = session.pop('_flashes', [])
if not with_categories:
return [x[1] for x in flashes]
return flashes

29
tests/flask_tests.py

@ -157,6 +157,35 @@ class BasicFunctionalityTestCase(unittest.TestCase):
assert flask.session.modified
assert list(flask.get_flashed_messages()) == ['Zap', 'Zip']
def test_extended_flashing(self):
app = flask.Flask(__name__)
app.secret_key = 'testkey'
@app.route('/')
def index():
flask.flash(u'Hello World')
flask.flash(u'Hello World', 'error')
flask.flash(flask.Markup(u'<em>Testing</em>'), 'warning')
return ''
@app.route('/test')
def test():
messages = flask.get_flashed_messages(with_categories=True)
assert len(messages) == 3
assert messages[0] == ('message', u'Hello World')
assert messages[1] == ('error', u'Hello World')
assert messages[2] == ('warning', flask.Markup(u'<em>Testing</em>'))
return ''
messages = flask.get_flashed_messages()
assert len(messages) == 3
assert messages[0] == u'Hello World'
assert messages[1] == u'Hello World'
assert messages[2] == flask.Markup(u'<em>Testing</em>')
c = app.test_client()
c.get('/')
c.get('/test')
def test_request_processing(self):
app = flask.Flask(__name__)
evts = []

Loading…
Cancel
Save