[Kimchi-devel] [PATCH V10 02/10] Update root.py to make Cheetah render the JSON template.
shaohef at linux.vnet.ibm.com
shaohef at linux.vnet.ibm.com
Mon Jun 16 10:28:19 UTC 2014
From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
when the request are json files, set the Content-Type as
application/json.
Signed-off-by: Adam King <rak at linux.vnet.ibm.com>
Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
src/kimchi/root.py | 5 +++++
src/kimchi/template.py | 34 +++++++++++++++++++---------------
2 files changed, 24 insertions(+), 15 deletions(-)
diff --git a/src/kimchi/root.py b/src/kimchi/root.py
index 9186486..48a1f87 100644
--- a/src/kimchi/root.py
+++ b/src/kimchi/root.py
@@ -82,6 +82,11 @@ def get(self):
def default(self, page, **kwargs):
if page.endswith('.html'):
return template.render(page, kwargs)
+ if page.endswith('.json'):
+ cherrypy.response.headers['Content-Type'] = \
+ 'application/json;charset=utf-8'
+ context = template.render_cheetah_file(page, None)
+ return context.encode("utf-8")
raise cherrypy.HTTPError(404)
@cherrypy.expose
diff --git a/src/kimchi/template.py b/src/kimchi/template.py
index 60cd818..146e735 100644
--- a/src/kimchi/template.py
+++ b/src/kimchi/template.py
@@ -82,26 +82,30 @@ def can_accept_html():
can_accept('*/*')
+def render_cheetah_file(resource, data):
+ paths = cherrypy.request.app.root.paths
+ filename = paths.get_template_path(resource)
+ try:
+ params = {'data': data}
+ lang = validate_language(get_lang())
+ gettext_conf = {'domain': 'kimchi',
+ 'localedir': paths.mo_dir,
+ 'lang': [lang]}
+ params['lang'] = gettext_conf
+ return Template(file=filename, searchList=params).respond()
+ except OSError, e:
+ if e.errno == errno.ENOENT:
+ raise cherrypy.HTTPError(404)
+ else:
+ raise
+
+
def render(resource, data):
if can_accept('application/json'):
cherrypy.response.headers['Content-Type'] = \
'application/json;charset=utf-8'
return json.dumps(data, indent=2, separators=(',', ':'))
elif can_accept_html():
- paths = cherrypy.request.app.root.paths
- filename = paths.get_template_path(resource)
- try:
- params = {'data': data}
- lang = validate_language(get_lang())
- gettext_conf = {'domain': 'kimchi',
- 'localedir': paths.mo_dir,
- 'lang': [lang]}
- params['lang'] = gettext_conf
- return Template(file=filename, searchList=params).respond()
- except OSError, e:
- if e.errno == errno.ENOENT:
- raise cherrypy.HTTPError(404)
- else:
- raise
+ return render_cheetah_file(resource, data)
else:
raise cherrypy.HTTPError(406)
--
1.9.3
More information about the Kimchi-devel
mailing list