forked from orbit-oss/flask
commit
87222087b3
6 changed files with 47 additions and 2 deletions
1
CHANGES
1
CHANGES
|
|
@ -8,6 +8,7 @@ Version 1.0
|
||||||
|
|
||||||
(release date to be announced, codename to be selected)
|
(release date to be announced, codename to be selected)
|
||||||
|
|
||||||
|
- Added before_render_template signal.
|
||||||
- Added `**kwargs` to :meth:`flask.Test.test_client` to support passing
|
- Added `**kwargs` to :meth:`flask.Test.test_client` to support passing
|
||||||
additional keyword arguments to the constructor of
|
additional keyword arguments to the constructor of
|
||||||
:attr:`flask.Flask.test_client_class`.
|
:attr:`flask.Flask.test_client_class`.
|
||||||
|
|
|
||||||
17
docs/api.rst
17
docs/api.rst
|
|
@ -538,6 +538,23 @@ The following signals exist in Flask:
|
||||||
from flask import template_rendered
|
from flask import template_rendered
|
||||||
template_rendered.connect(log_template_renders, app)
|
template_rendered.connect(log_template_renders, app)
|
||||||
|
|
||||||
|
.. data:: flask.before_render_template
|
||||||
|
:noindex:
|
||||||
|
|
||||||
|
This signal is sent before template rendering process. The
|
||||||
|
signal is invoked with the instance of the template as `template`
|
||||||
|
and the context as dictionary (named `context`).
|
||||||
|
|
||||||
|
Example subscriber::
|
||||||
|
|
||||||
|
def log_template_renders(sender, template, context, **extra):
|
||||||
|
sender.logger.debug('Rendering template "%s" with context %s',
|
||||||
|
template.name or 'string template',
|
||||||
|
context)
|
||||||
|
|
||||||
|
from flask import before_render_template
|
||||||
|
before_render_template.connect(log_template_renders, app)
|
||||||
|
|
||||||
.. data:: request_started
|
.. data:: request_started
|
||||||
|
|
||||||
This signal is sent when the request context is set up, before
|
This signal is sent when the request context is set up, before
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ from .templating import render_template, render_template_string
|
||||||
from .signals import signals_available, template_rendered, request_started, \
|
from .signals import signals_available, template_rendered, request_started, \
|
||||||
request_finished, got_request_exception, request_tearing_down, \
|
request_finished, got_request_exception, request_tearing_down, \
|
||||||
appcontext_tearing_down, appcontext_pushed, \
|
appcontext_tearing_down, appcontext_pushed, \
|
||||||
appcontext_popped, message_flashed
|
appcontext_popped, message_flashed, before_render_template
|
||||||
|
|
||||||
# We're not exposing the actual json module but a convenient wrapper around
|
# We're not exposing the actual json module but a convenient wrapper around
|
||||||
# it.
|
# it.
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,7 @@ _signals = Namespace()
|
||||||
# Core signals. For usage examples grep the source code or consult
|
# Core signals. For usage examples grep the source code or consult
|
||||||
# the API documentation in docs/api.rst as well as docs/signals.rst
|
# the API documentation in docs/api.rst as well as docs/signals.rst
|
||||||
template_rendered = _signals.signal('template-rendered')
|
template_rendered = _signals.signal('template-rendered')
|
||||||
|
before_render_template = _signals.signal('before-render-template')
|
||||||
request_started = _signals.signal('request-started')
|
request_started = _signals.signal('request-started')
|
||||||
request_finished = _signals.signal('request-finished')
|
request_finished = _signals.signal('request-finished')
|
||||||
request_tearing_down = _signals.signal('request-tearing-down')
|
request_tearing_down = _signals.signal('request-tearing-down')
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ from jinja2 import BaseLoader, Environment as BaseEnvironment, \
|
||||||
TemplateNotFound
|
TemplateNotFound
|
||||||
|
|
||||||
from .globals import _request_ctx_stack, _app_ctx_stack
|
from .globals import _request_ctx_stack, _app_ctx_stack
|
||||||
from .signals import template_rendered
|
from .signals import template_rendered, before_render_template
|
||||||
|
|
||||||
|
|
||||||
def _default_template_ctx_processor():
|
def _default_template_ctx_processor():
|
||||||
|
|
@ -102,6 +102,8 @@ class DispatchingJinjaLoader(BaseLoader):
|
||||||
|
|
||||||
def _render(template, context, app):
|
def _render(template, context, app):
|
||||||
"""Renders the template and fires the signal"""
|
"""Renders the template and fires the signal"""
|
||||||
|
|
||||||
|
before_render_template.send(app, template=template, context=context)
|
||||||
rv = template.render(context)
|
rv = template.render(context)
|
||||||
template_rendered.send(app, template=template, context=context)
|
template_rendered.send(app, template=template, context=context)
|
||||||
return rv
|
return rv
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,30 @@ def test_template_rendered():
|
||||||
finally:
|
finally:
|
||||||
flask.template_rendered.disconnect(record, app)
|
flask.template_rendered.disconnect(record, app)
|
||||||
|
|
||||||
|
def test_before_render_template():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return flask.render_template('simple_template.html', whiskey=42)
|
||||||
|
|
||||||
|
recorded = []
|
||||||
|
|
||||||
|
def record(sender, template, context):
|
||||||
|
context['whiskey'] = 43
|
||||||
|
recorded.append((template, context))
|
||||||
|
|
||||||
|
flask.before_render_template.connect(record, app)
|
||||||
|
try:
|
||||||
|
rv = app.test_client().get('/')
|
||||||
|
assert len(recorded) == 1
|
||||||
|
template, context = recorded[0]
|
||||||
|
assert template.name == 'simple_template.html'
|
||||||
|
assert context['whiskey'] == 43
|
||||||
|
assert rv.data == b'<h1>43</h1>'
|
||||||
|
finally:
|
||||||
|
flask.before_render_template.disconnect(record, app)
|
||||||
|
|
||||||
def test_request_signals():
|
def test_request_signals():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
calls = []
|
calls = []
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue