Merge branch 'alexpantyukhin-master'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #1390
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents cb9f645, a9066a3 · Browse files · 6 files·+47 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+47 added−2 removed
- tests:+24 added−0 removed
- other:+23 added−2 removed
Provenance
- Commit
8722208- Tree
1cdaf84- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
cb9f645- Parent 2
a9066a3
6 files changed+47 added−2 removed
modified:docs/api.rst+17 added
536 unmodified lines537 538 from flask import template_rendered539 template_rendered.connect(log_template_renders, app)540 541+.. data:: flask.before_render_template542+ :noindex:543+544+ This signal is sent before template rendering process. The545+ signal is invoked with the instance of the template as `template`546+ and the context as dictionary (named `context`).547+548+ Example subscriber::549+550+ def log_template_renders(sender, template, context, **extra):551+ sender.logger.debug('Rendering template "%s" with context %s',552+ template.name or 'string template',553+ context)554+555+ from flask import before_render_template556+ before_render_template.connect(log_template_renders, app)557+558 .. data:: request_started559 560 This signal is sent when the request context is set up, before561 any request processing happens. Because the request context is already338 unmodified linesmodified: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+1 added
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')4 unmodified linesmodified:flask/templating.py+3 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+106+ before_render_template.send(app, template=template, context=context)107 rv = template.render(context)108 template_rendered.send(app, template=template, context=context)109 return rv110 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 linesmodified:CHANGES+1 added
6 unmodified lines7 -----------8 9 (release date to be announced, codename to be selected)10 11+- Added before_render_template signal.12 - Added `**kwargs` to :meth:`flask.Test.test_client` to support passing13 additional keyword arguments to the constructor of14 :attr:`flask.Flask.test_client_class`.15 - Added ``SESSION_REFRESH_EACH_REQUEST`` config key that controls the536 unmodified lines