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
This commit is contained in:
Matt Good
2013-03-08 09:00:10 -08:00
parent 05288daf17
commit b7c39113e7
2 changed files with 21 additions and 4 deletions

View File

@@ -1,6 +1,14 @@
Changes
=======
0.8.1 ()
--------
Fixes:
- Fix template editor with non-ASCII templates (#46)
0.8 (2013-02-21)
----------------

View File

@@ -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/<key>')
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'