From b7c39113e7dafe0ada503bca1f4cf5be026776ca Mon Sep 17 00:00:00 2001 From: Matt Good Date: Fri, 8 Mar 2013 09:00:10 -0800 Subject: [PATCH] Template editor should use Jinja loader's encoding The template loader wouldn't handle non-ASCII template encodings. Updated it to use the encoding parameter of the Jinja loader when reading and writing the template files. Fixes #46 --- CHANGES.rst | 8 ++++++++ flask_debugtoolbar/panels/template.py | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e08ac6d..37be727 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,14 @@ Changes ======= +0.8.1 () +-------- + +Fixes: + +- Fix template editor with non-ASCII templates (#46) + + 0.8 (2013-02-21) ---------------- diff --git a/flask_debugtoolbar/panels/template.py b/flask_debugtoolbar/panels/template.py index ffbeca2..4075954 100644 --- a/flask_debugtoolbar/panels/template.py +++ b/flask_debugtoolbar/panels/template.py @@ -77,6 +77,16 @@ def require_enabled(): abort(403) +def _get_source(template): + with open(template.filename, 'rb') as fp: + source = fp.read() + return source.decode(_template_encoding()) + + +def _template_encoding(): + return getattr(current_app.jinja_loader, 'encoding', 'utf-8') + + @module.route('/template/') def template_editor(key): require_enabled() @@ -87,8 +97,7 @@ def template_editor(key): 'static_path': url_for('_debug_toolbar.static', filename=''), 'request': request, 'templates': [ - {'name': t.name, - 'source': open(t.filename).read()} + {'name': t.name, 'source': _get_source(t)} for t in templates ] }) @@ -98,8 +107,8 @@ def template_editor(key): def save_template(key): require_enabled() template = TemplateDebugPanel.get_cache_for_key(key)[0]['template'] - content = request.form['content'] - with open(template.filename, 'w') as fp: + content = request.form['content'].encode(_template_encoding()) + with open(template.filename, 'wb') as fp: fp.write(content) return 'ok'