コード例 #1
0
ファイル: decorators.py プロジェクト: dephraser/piss
 def wrapper(*args, **kwargs):
     # If the request is XML or JSON, return the Eve endpoint
     if request_is_xml() or request_is_json():
         return eve_view_func[request_type](*args, **kwargs)
     
     # If the request is for the `item` or `resource` endpoints, get
     # the resource name and method and feed them to the HTML renderer
     if request_type == 'item' or request_type == 'resource':
         resource = _resource()
         method = request_method()
         return wrapper_func(resource, method, **kwargs)
     
     # For all other endpoints, return the HTML renderer
     return wrapper_func(*args, **kwargs)
コード例 #2
0
ファイル: utils.py プロジェクト: dephraser/piss
def render_response(obj, template_name, **kwargs):
    '''
    Converts a Python object into a suitable response object (JSON, XML, or
    HTML) depending on the `Accept` header of the request.

    :param obj: the object to be converted.
    :param template_name: the name of the HTML template to use.
    :param **kwargs: additional arguments for the template rendering.
    '''
    if request_is_json():
        if not isinstance(obj, dict):
            obj = {'_items': obj}
        return jsonify(obj)
    elif request_is_xml():
        response = make_response(render_xml(obj))
        response.mimetype = 'application/xml'
        response.charset = 'utf-8'
        return response
    else:
        return render_template(template_name, obj=obj, **kwargs)