Merged in changes from master by hand
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 81148db · Browse files · 3 files·+37 added−12 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+37 added−12 removed
- tests:+2 added−3 removed
- other:+35 added−9 removed
Provenance
- Commit
8798b4b- Tree
6c3f40b- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
81148db
3 files changed+37 added−12 removed
modified:flask/__init__.py+1 added-1 removed
16 unmodified lines17 18 from flask.app import Flask19 from flask.helpers import url_for, jsonify, json_available, flash, send_file, \20 get_flashed_messages, render_template, render_template, render_template_string, \21- get_template_attribute21+ get_template_attribute, json22 from flask.globals import current_app, g, request, session, _request_ctx_stack23 from flask.module import Modulemodified:flask/app.py+34 added-8 removed
18 unmodified lines19 # a lock used for logger initialization20 _logger_lock = Lock()21 22 23+def _select_autoescape(filename):24+ """Returns `True` if autoescaping should be active for the given25+ template name.26+ """27+ if filename is None:28+ return False29+ return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))30+31+32 class Flask(_PackageBoundObject):33 """The flask object implements a WSGI application and acts as the central34 object. It is passed the name of the module or package of the35 application. Once it is created it will act as a central registry for124 unmodified lines160 )161 162 #: Options that are passed directly to the Jinja2 environment.163 jinja_options = ImmutableDict(155- autoescape=True,164+ autoescape=_select_autoescape,165 extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_']166 )167 168 #: Default configuration parameters.89 unmodified lines258 259 #: The Jinja2 environment. It is created from the260 #: :attr:`jinja_options` and the loader that is returned261 #: by the :meth:`create_jinja_loader` function.253- self.jinja_env = Environment(loader=self.create_jinja_loader(),254- **self.jinja_options)255- self.jinja_env.globals.update(256- url_for=url_for,257- get_flashed_messages=get_flashed_messages258- )259- self.jinja_env.filters['tojson'] = _tojson_filter262+ self.jinja_env = self.create_jinja_environment()263+ self.init_jinja_globals()264 265 @property266 def logger(self):267 """A :class:`logging.Logger` object for this application. The28 unmodified lines296 logger.addHandler(handler)297 self._logger = logger298 return logger299 300+ def create_jinja_environment(self):301+ """Creates the Jinja2 environment based on :attr:`jinja_options`302+ and :meth:`create_jinja_loader`.303+304+ .. versionadded:: 0.5305+ """306+ return Environment(loader=self.create_jinja_loader(),307+ **self.jinja_options)308+309 def create_jinja_loader(self):310 """Creates the Jinja loader. By default just a package loader for311 the configured package is returned that looks up templates in the312 `templates` folder. To add other loaders it's possible to2 unmodified lines315 if get_pkg_resources() is None:316 return FileSystemLoader(os.path.join(self.root_path, 'templates'))317 return PackageLoader(self.import_name)318 319+ def init_jinja_globals(self):320+ """Callde directly after the environment was created to inject321+ some defaults (like `url_for`, `get_flashed_messages` and the322+ `tojson` filter.323+324+ .. versionadded:: 0.5325+ """326+ self.jinja_env.globals.update(327+ url_for=url_for,328+ get_flashed_messages=get_flashed_messages329+ )330+ self.jinja_env.filters['tojson'] = _tojson_filter331+332 def update_template_context(self, context):333 """Update the template context with some commonly used variables.334 This injects request, session and g into the template context.335 489 unmodified linesmodified:tests/flask_tests.py+2 added-3 removed
19 unmodified lines20 from contextlib import contextmanager21 from datetime import datetime22 from werkzeug import parse_date, parse_options_header23 from cStringIO import StringIO24-from flask.helpers import json24 25 example_path = os.path.join(os.path.dirname(__file__), '..', 'examples')26 sys.path.append(os.path.join(example_path, 'flaskr'))27 sys.path.append(os.path.join(example_path, 'minitwit'))379 unmodified lines407 c = app.test_client()408 for url in '/kw', '/dict':409 rv = c.get(url)410 assert rv.mimetype == 'application/json'412- assert json.loads(rv.data) == d411+ assert flask.json.loads(rv.data) == d412 413 def test_json_attr(self):414 app = flask.Flask(__name__)415 @app.route('/add', methods=['POST'])416 def add():417 return unicode(flask.request.json['a'] + flask.request.json['b'])418 c = app.test_client()420- rv = c.post('/add', data=json.dumps({'a': 1, 'b': 2}),419+ rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}),420 content_type='application/json')421 assert rv.data == '3'422 423 def test_template_escaping(self):474 unmodified lines