Merged in changes from master by hand
This commit is contained in:
parent
81148db5b6
commit
8798b4b711
3 changed files with 37 additions and 12 deletions
|
|
@ -18,6 +18,6 @@ from jinja2 import Markup, escape
|
|||
from flask.app import Flask
|
||||
from flask.helpers import url_for, jsonify, json_available, flash, send_file, \
|
||||
get_flashed_messages, render_template, render_template, render_template_string, \
|
||||
get_template_attribute
|
||||
get_template_attribute, json
|
||||
from flask.globals import current_app, g, request, session, _request_ctx_stack
|
||||
from flask.module import Module
|
||||
|
|
|
|||
42
flask/app.py
42
flask/app.py
|
|
@ -20,6 +20,15 @@ from flask.module import _ModuleSetupState
|
|||
_logger_lock = Lock()
|
||||
|
||||
|
||||
def _select_autoescape(filename):
|
||||
"""Returns `True` if autoescaping should be active for the given
|
||||
template name.
|
||||
"""
|
||||
if filename is None:
|
||||
return False
|
||||
return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))
|
||||
|
||||
|
||||
class Flask(_PackageBoundObject):
|
||||
"""The flask object implements a WSGI application and acts as the central
|
||||
object. It is passed the name of the module or package of the
|
||||
|
|
@ -152,7 +161,7 @@ class Flask(_PackageBoundObject):
|
|||
|
||||
#: Options that are passed directly to the Jinja2 environment.
|
||||
jinja_options = ImmutableDict(
|
||||
autoescape=True,
|
||||
autoescape=_select_autoescape,
|
||||
extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_']
|
||||
)
|
||||
|
||||
|
|
@ -250,13 +259,8 @@ class Flask(_PackageBoundObject):
|
|||
#: The Jinja2 environment. It is created from the
|
||||
#: :attr:`jinja_options` and the loader that is returned
|
||||
#: by the :meth:`create_jinja_loader` function.
|
||||
self.jinja_env = Environment(loader=self.create_jinja_loader(),
|
||||
**self.jinja_options)
|
||||
self.jinja_env.globals.update(
|
||||
url_for=url_for,
|
||||
get_flashed_messages=get_flashed_messages
|
||||
)
|
||||
self.jinja_env.filters['tojson'] = _tojson_filter
|
||||
self.jinja_env = self.create_jinja_environment()
|
||||
self.init_jinja_globals()
|
||||
|
||||
@property
|
||||
def logger(self):
|
||||
|
|
@ -293,6 +297,15 @@ class Flask(_PackageBoundObject):
|
|||
self._logger = logger
|
||||
return logger
|
||||
|
||||
def create_jinja_environment(self):
|
||||
"""Creates the Jinja2 environment based on :attr:`jinja_options`
|
||||
and :meth:`create_jinja_loader`.
|
||||
|
||||
.. versionadded:: 0.5
|
||||
"""
|
||||
return Environment(loader=self.create_jinja_loader(),
|
||||
**self.jinja_options)
|
||||
|
||||
def create_jinja_loader(self):
|
||||
"""Creates the Jinja loader. By default just a package loader for
|
||||
the configured package is returned that looks up templates in the
|
||||
|
|
@ -303,6 +316,19 @@ class Flask(_PackageBoundObject):
|
|||
return FileSystemLoader(os.path.join(self.root_path, 'templates'))
|
||||
return PackageLoader(self.import_name)
|
||||
|
||||
def init_jinja_globals(self):
|
||||
"""Callde directly after the environment was created to inject
|
||||
some defaults (like `url_for`, `get_flashed_messages` and the
|
||||
`tojson` filter.
|
||||
|
||||
.. versionadded:: 0.5
|
||||
"""
|
||||
self.jinja_env.globals.update(
|
||||
url_for=url_for,
|
||||
get_flashed_messages=get_flashed_messages
|
||||
)
|
||||
self.jinja_env.filters['tojson'] = _tojson_filter
|
||||
|
||||
def update_template_context(self, context):
|
||||
"""Update the template context with some commonly used variables.
|
||||
This injects request, session and g into the template context.
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ from contextlib import contextmanager
|
|||
from datetime import datetime
|
||||
from werkzeug import parse_date, parse_options_header
|
||||
from cStringIO import StringIO
|
||||
from flask.helpers import json
|
||||
|
||||
example_path = os.path.join(os.path.dirname(__file__), '..', 'examples')
|
||||
sys.path.append(os.path.join(example_path, 'flaskr'))
|
||||
|
|
@ -409,7 +408,7 @@ class JSONTestCase(unittest.TestCase):
|
|||
for url in '/kw', '/dict':
|
||||
rv = c.get(url)
|
||||
assert rv.mimetype == 'application/json'
|
||||
assert json.loads(rv.data) == d
|
||||
assert flask.json.loads(rv.data) == d
|
||||
|
||||
def test_json_attr(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -417,7 +416,7 @@ class JSONTestCase(unittest.TestCase):
|
|||
def add():
|
||||
return unicode(flask.request.json['a'] + flask.request.json['b'])
|
||||
c = app.test_client()
|
||||
rv = c.post('/add', data=json.dumps({'a': 1, 'b': 2}),
|
||||
rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}),
|
||||
content_type='application/json')
|
||||
assert rv.data == '3'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue