before_render_template signal
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alexander Pantyukhin · committed by Markus Unterwaditzer · parent cb9f645 · Browse files · 4 files·+29 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+29 added−3 removed
- tests:+24 added−0 removed
- other:+5 added−3 removed
Provenance
- Commit
d53d5c7- Tree
ceeb2ad- Author
Alexander Pantyukhin <alexander.pantyukhin@fastdev.se>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
cb9f645
4 files changed+29 added−3 removed
modified:flask/__init__.py+1 added-1 removed
32 unmodified lines33 # the signals34 from .signals import signals_available, template_rendered, request_started, \35 request_finished, got_request_exception, request_tearing_down, \36 appcontext_tearing_down, appcontext_pushed, \37- appcontext_popped, message_flashed37+ appcontext_popped, message_flashed, before_render_template38 39 # We're not exposing the actual json module but a convenient wrapper around40 # it.41 from . import json8 unmodified linesmodified:flask/signals.py+2 added-1 removed
43 unmodified lines44 45 # Core signals. For usage examples grep the source code or consult46 # the API documentation in docs/api.rst as well as docs/signals.rst47 template_rendered = _signals.signal('template-rendered')48+before_render_template = _signals.signal('before-render-template')49 request_started = _signals.signal('request-started')50 request_finished = _signals.signal('request-finished')51 request_tearing_down = _signals.signal('request-tearing-down')52 got_request_exception = _signals.signal('got-request-exception')53 appcontext_tearing_down = _signals.signal('appcontext-tearing-down')54 appcontext_pushed = _signals.signal('appcontext-pushed')55 appcontext_popped = _signals.signal('appcontext-popped')55-message_flashed = _signals.signal('message-flashed')56+message_flashed = _signals.signal('message-flashed')modified:flask/templating.py+2 added-1 removed
10 unmodified lines11 from jinja2 import BaseLoader, Environment as BaseEnvironment, \12 TemplateNotFound13 14 from .globals import _request_ctx_stack, _app_ctx_stack15-from .signals import template_rendered15+from .signals import template_rendered, before_render_template16 17 18 def _default_template_ctx_processor():19 """Default template context processor. Injects `request`,81 unmodified lines101 102 103 def _render(template, context, app):104 """Renders the template and fires the signal"""105+ before_render_template.send(app, template=template, context=context)106 rv = template.render(context)107 template_rendered.send(app, template=template, context=context)108 return rv109 30 unmodified linesmodified:tests/test_signals.py+24 added
44 unmodified lines45 assert context['whiskey'] == 4246 finally:47 flask.template_rendered.disconnect(record, app)48 49+def test_before_render_template():50+ app = flask.Flask(__name__)51+52+ @app.route('/')53+ def index():54+ return flask.render_template('simple_template.html', whiskey=42)55+56+ recorded = []57+58+ def record(sender, template, context):59+ context['whiskey'] = 4360+ recorded.append((template, context))61+62+ flask.before_render_template.connect(record, app)63+ try:64+ rv = app.test_client().get('/')65+ assert len(recorded) == 166+ template, context = recorded[0]67+ assert template.name == 'simple_template.html'68+ assert context['whiskey'] == 4369+ assert rv.data == b'<h1>43</h1>'70+ finally:71+ flask.before_render_template.disconnect(record, app)72+73 def test_request_signals():74 app = flask.Flask(__name__)75 calls = []76 126 unmodified lines