Exemple #1
0
    def __call__(self, request):
        action = request.urlvars["action"]
        content_type = request.content_type
        serializer = Serializer()

        try:
            method = getattr(self, action)
            result = method(request, **request.urlvars)

        except Exception as e:
            resp = {"body": e, "content-type": content_type}
            raise webob.exc.HTTPInternalServerError(resp)

        if isinstance(result, dict) or result is None:
            if not result:
                response = webob.exc.HTTPNoContent()

            else:
                status = 200
                content_type = content_type
                body = serializer.serialize(result, content_type)
                response = webob.Response(status=status, body=body, content_type=content_type)
            return response
        else:
            return result
 def __init__(self):
     self.serializer = Serializer()
class Controller(object):

    def __init__(self):
        self.serializer = Serializer()

    def index(self, request, **kwargs):
        return webob.exc.HTTPNoContent()

    def create(self, request, **kwargs):
        return webob.exc.HTTPNoContent()

    def new(self, request, **kwargs):
        return webob.exc.HTTPNoContent()

    def show(self, request, resource, **kwargs):
        return webob.exc.HTTPNotFound()

    def update(self, request, resource, **kwargs):
        return webob.exc.HTTPNotFound()

    def delete(self, request, resource, **kwargs):
        return webob.exc.HTTPNotFound()

    def edit(self, request, resource, **kwargs):
        return webob.exc.HTTPNotFound()

    def serialize(self, data, content_type=CONTENT_TYPE_HTML, **kwargs):
        return self.serializer.serialize(data, content_type, **kwargs)

    def deserialize(self, data, content_type=CONTENT_TYPE_HTML, **kwargs):
        return self.serializer.deserialize(data, content_type, **kwargs)

    def response(self, **kwargs):
        return webob.Response(**kwargs)

    @webob.dec.wsgify
    def __call__(self, request):
        action = request.urlvars['action']

        try:
            method = getattr(self, action)    #pylint: disable=R0921
            result = method(request, **request.urlvars)

        except Exception as exc:
            log.debug(exc)
            raise webob.exc.HTTPInternalServerError()

        if result is None:
            result = webob.exc.HTTPNoContent()

        elif isinstance(result, dict):
            # serialize body based on response content type
            if 'body' in result:
                content_type = result.get('content_type')
                result['body'] = self.serialize(result['body'], content_type)

            result.setdefault('status', HTTP_STATUS_OK)
            result.setdefault('content_type', CONTENT_TYPE_HTML)

            result = self.response(**result)   #pylint: disable=W0142

        elif not isinstance(result, webob.Response) and \
             not isinstance(result, webob.static.FileApp):
            result = webob.exc.HTTPInternalServerError()

        return result