Beispiel #1
0
class CacheController(WSGIController):
    def test_bad_json(self):
        return ["this is neat"]

    test_bad_json = jsonify(test_bad_json)

    def test_good_json(self):
        return dict(fred=42)

    test_good_json = jsonify(test_good_json)
Beispiel #2
0
def _expose_wrapper(f, template):
    """Returns a function that will render the passed in function according
    to the passed in template"""
    f.exposed = True
    f.template = template

    if template == "json":
        return jsonify(f)
    elif template == "string":
        return f

    def wrapped_f(*args, **kwargs):
        result = f(*args, **kwargs)
        extra_vars = {
            # Steal a page from TurboGears' book:
            # include the genshi XML helper for convenience in templates.
            "XML": XML
        }
        # If the provided template path isn't absolute (ie, doesn't start with
        # a '/'), then prepend the default search path. By providing the
        # template path to genshi as an absolute path, we invoke different
        # rules for the resolution of 'xi:include' paths in the template.
        # See http://genshi.edgewall.org/browser/trunk/genshi/template/loader.py#L178
        if not template.startswith("/"):
            tmpl = os.path.join(config["genshi_search_path"], template)
        else:
            tmpl = template
        extra_vars.update(result)
        return render(tmpl, extra_vars=extra_vars)

    return wrapped_f
Beispiel #3
0
def _expose_wrapper(f, template):
    """Returns a function that will render the passed in function according
    to the passed in template"""
    f.exposed = True
    f.template = template

    if template == "json":
        return jsonify(f)
    elif template == "string":
        return f

    def wrapped_f(*args, **kwargs):
        result = f(*args, **kwargs)

        extra_vars = {
            # Steal a page from TurboGears' book:
            # include the genshi XML helper for convenience in templates.
            'XML': XML
        }
        extra_vars.update(result)

        # If the provided template path isn't absolute (ie, doesn't start with
        # a '/'), then prepend the default search path. By providing the
        # template path to genshi as an absolute path, we invoke different
        # rules for the resolution of 'xi:include' paths in the template.
        # See http://genshi.edgewall.org/browser/trunk/genshi/template/loader.py#L178
        if not template.startswith('/'):
            tmpl = os.path.join(config['genshi_search_path'], template)
        else:
            tmpl = template

        if request.environ.get('paste.testing', False):
            # Make the vars passed from action to template accessible to tests
            request.environ['paste.testing_variables']['tmpl_vars'] = result

            # Serve application/xhtml+xml instead of text/html during testing.
            # This allows us to query the response xhtml as ElementTree XML
            # instead of BeautifulSoup HTML.
            # NOTE: We do not serve true xhtml to all clients that support it
            #       because of a bug in Mootools Swiff as of v1.2.4:
            #       https://mootools.lighthouseapp.com/projects/2706/tickets/758
            if response.content_type == 'text/html':
                response.content_type = 'application/xhtml+xml'

        return render(tmpl, extra_vars=extra_vars)
    return wrapped_f