custom_request_handler.py 5.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Copyright (c) 2014 thkang2
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Jinja2 templating for Tornado, taken from https://github.com/thkang2/jinja_tornado"""

import json
import datetime

25
from jinja2 import Environment, FileSystemLoader, Markup, TemplateSyntaxError
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

from tornado.escape import squeeze, linkify, url_escape, xhtml_escape
import tornado.web

import zoe_lib.version

import zoe_api.web.utils


class JinjaApp(object):
    """A Jinja2-capable Tornado application."""
    def __init__(self, application, jinja_options=None):
        self.application = application
        self.jinja_options = jinja_options
        self.init_app(application, jinja_options)

    @classmethod
    def init_app(cls, application, jinja_options=None):
        """Init the application."""
        app_settings = application.settings

        _loader = FileSystemLoader(
            app_settings.get('template_path', 'templates')
        )

        _jinja_config = {
            'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'],
            'auto_reload': app_settings.get('autoreload', False),
            'loader': _loader,
            'cache_size': 50 if app_settings.get('compiled_template_cache', True) else 0,
            'autoescape': True if app_settings.get('autoescape', 'xhtml_escape') == "xhtml_escape" else False,
        }

        _jinja_config.update(**(jinja_options or {}))
        environment = Environment(**_jinja_config)

        application.jinja_environment = environment
        app_settings['jinja_environment'] = environment
        environment.filters.update(tojson=tojson_filter, xhtml_escape=xhtml_escape, url_escape=url_escape, squeeze=squeeze, linkify=linkify)

        return environment


def dumps(obj, **kwargs):
    """Escape characters."""
    # https://github.com/mitsuhiko/flask/blob/master/flask/json.py
    return json.dumps(obj, **kwargs) \
        .replace(u'<', u'\\u003c') \
        .replace(u'>', u'\\u003e') \
        .replace(u'&', u'\\u0026') \
        .replace(u"'", u'\\u0027')


def tojson_filter(obj, **kwargs):
    """Filter for JSON output in templates."""
    # https://github.com/mitsuhiko/flask/blob/master/flask/json.py
    return Markup(dumps(obj, **kwargs))


class ZoeRequestHandler(tornado.web.RequestHandler):
    """usage:
        class JinjaPoweredHandler(JinjaTemplateMixin, tornado.web.RequestHandler):
            pass
    """

    def initialize(self, *args_, **kwargs_):
        """Initialize the Jinja template system."""
        # jinja environment is shared among an application
        if 'jinja_environment' not in self.application.settings:
            raise RuntimeError("Needs jinja2 Environment. Initialize with JinjaApp.init_app first")
        else:
            self._jinja_env = self.application.settings['jinja_environment']

    def _render(self, template, **kwargs):
        """ todo: support multiple template preprocessors """

        ctx = {
            'request': self.request,
            'path_args': self.path_args,
            'path_kwargs': self.path_kwargs,
            'settings': self.application.settings,
            'reverse_url': self.application.reverse_url,
            'static_url': self.static_url,
            'xsrf_form_html': self.xsrf_form_html,
            'datetime': datetime,
            'locale': self.locale,
            'handler': self,
            'zoe_version': zoe_lib.version.ZOE_VERSION
        }

        ctx.update(kwargs)
        return template.render(ctx)

    def render(self, template_name, **kwargs):
        """ renders a template file. """
        template = self._jinja_env.get_template(template_name)
        try:
            html = self._render(template, **kwargs)
124 125 126 127 128
        except TemplateSyntaxError as e:
            zoe_api.web.utils.error_page(self, 'Template syntax error at {}:{}:<br> {}'.format(e.name, e.lineno, e.message), 500)
            return
        except Exception as e:
            zoe_api.web.utils.error_page(self, 'Jinja2 template exception: {}'.format(e), 500)
129 130 131 132 133 134 135 136 137 138 139
            return
        self.finish(html)

    def render_string(self, source, **kwargs):
        """ renders a template source string. """
        template = self._jinja_env.from_string(source)
        return self._render(template, **kwargs)

    def data_received(self, chunk):
        """Not implemented as we do not use stream uploads"""
        pass