Added support for flashing categories.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This fixes #35.
authored by Armin Ronacher · committed by Armin Ronacher · parent 693e444 · Browse files · 3 files·+60 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+60 added−4 removed
- tests:+29 added−0 removed
- other:+31 added−4 removed
Provenance
- Commit
35ed617- Tree
6cfcd62- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
693e444
3 files changed+60 added−4 removed
modified:tests/flask_tests.py+29 added
155 unmodified lines156 flask.flash('Zip')157 assert flask.session.modified158 assert list(flask.get_flashed_messages()) == ['Zap', 'Zip']159 160+ def test_extended_flashing(self):161+ app = flask.Flask(__name__)162+ app.secret_key = 'testkey'163+164+ @app.route('/')165+ def index():166+ flask.flash(u'Hello World')167+ flask.flash(u'Hello World', 'error')168+ flask.flash(flask.Markup(u'<em>Testing</em>'), 'warning')169+ return ''170+171+ @app.route('/test')172+ def test():173+ messages = flask.get_flashed_messages(with_categories=True)174+ assert len(messages) == 3175+ assert messages[0] == ('message', u'Hello World')176+ assert messages[1] == ('error', u'Hello World')177+ assert messages[2] == ('warning', flask.Markup(u'<em>Testing</em>'))178+ return ''179+ messages = flask.get_flashed_messages()180+ assert len(messages) == 3181+ assert messages[0] == u'Hello World'182+ assert messages[1] == u'Hello World'183+ assert messages[2] == flask.Markup(u'<em>Testing</em>')184+185+ c = app.test_client()186+ c.get('/')187+ c.get('/test')188+189 def test_request_processing(self):190 app = flask.Flask(__name__)191 evts = []192 @app.before_request413 unmodified linesmodified:CHANGES+2 added
6 unmodified lines7 -----------8 9 Release date to be announced10 11+- added support for categories for flashed messages.12+13 Version 0.214 -----------15 16 Released on May 12th, codename Jägermeister20 unmodified linesmodified:flask.py+29 added-4 removed
215 unmodified lines216 return getattr(current_app.jinja_env.get_template(template_name).module,217 attribute)218 219 220-def flash(message):220+def flash(message, category='message'):221 """Flashes a message to the next request. In order to remove the222 flashed message from the session and to display it to the user,223 the template has to call :func:`get_flashed_messages`.224 225+ .. versionchanged: 0.5226+ `category` parameter added.227+228 :param message: the message to be flashed.229+ :param category: the category for the message. The following values230+ are recommended: ``'message'`` for any kind of message,231+ ``'error'`` for errors, ``'info'`` for information232+ messages and ``'warning'`` for warnings. However any233+ kind of string and be used as category.234 """227- session.setdefault('_flashes', []).append(message)235+ session.setdefault('_flashes', []).append((category, message))236 237 230-def get_flashed_messages():238+def get_flashed_messages(with_categories=False):239 """Pulls all flashed messages from the session and returns them.240 Further calls in the same request to the function will return233- the same messages.241+ the same messages. By default just the messages are returned,242+ but when `with_categories` is set to `True`, the return value will243+ be a list of tuples in the form ``(category, message)`` instead.244+245+ Example usage:246+247+ .. sourcecode:: html+jinja248+249+ {% for category, msg in get_flashed_messages(with_categories=true) %}250+ <p class=flash-{{ category }}>{{ msg }}251+ {% endfor %}252+253+ .. versionchanged:: 0.5254+ `with_categories` parameter added.255+256+ :param with_categories: set to `True` to also receive categories.257 """258 flashes = _request_ctx_stack.top.flashes259 if flashes is None:260 _request_ctx_stack.top.flashes = flashes = session.pop('_flashes', [])261+ if not with_categories:262+ return [x[1] for x in flashes]263 return flashes264 265 266 def jsonify(*args, **kwargs):896 unmodified lines