def test_decorate_view(self):
        def myfunction():
            pass

        meth = "POST"
        decorated = decorate_view(myfunction, {}, meth)
        self.assertEqual(decorated.__name__, "{0}__{1}".format(func_name(myfunction), meth))
    def test_decorate_resource_view(self):
        class MyResource(object):
            def __init__(self, **kwargs):
                pass

            def myview(self):
                pass

        meth = "POST"
        decorated = decorate_view(_UnboundView(MyResource, "myview"), {}, meth)
        self.assertEqual(decorated.__name__, "{0}__{1}".format(func_name(MyResource.myview), meth))
 def test_func_name_class_method(self):
     self.assertEqual("TestServiceWithWrapper.test_wrapped", func_name(TestServiceWithWrapper.test_wrapped))
 def test_func_name_decorated_function(self):
     self.assertEqual("return_foo", func_name(return_foo))
 def test_func_name_string(self):
     self.assertEqual("some_string", func_name("some_string"))
Beispiel #6
0
 def __init__(self, klass, view):
     self.unbound_view = getattr(klass, view.lower())
     functools.update_wrapper(self, self.unbound_view)
     self.__name__ = func_name(self.unbound_view)
 def test_func_name_undecorated_function(self):
     self.assertEqual("my_acl", func_name(my_acl))
Beispiel #8
0
 def test_func_name_decorated_function(self):
     self.assertEqual("return_foo", func_name(return_foo))
Beispiel #9
0
def decorate_view(view, args, method):
    """Decorate a given view with cornice niceties.

    This function returns a function with the same signature than the one
    you give as :param view:

    :param view: the view to decorate
    :param args: the args to use for the decoration
    :param method: the HTTP method
    """
    def wrapper(request):
        # if the args contain a klass argument then use it to resolve the view
        # location (if the view argument isn't a callable)
        ob = None
        view_ = view
        if 'klass' in args and not callable(view):
            params = dict(request=request)
            if 'factory' in args and 'acl' not in args:
                params['context'] = request.context
            ob = args['klass'](**params)
            if is_string(view):
                view_ = getattr(ob, view.lower())
            elif isinstance(view, _UnboundView):
                view_ = view.make_bound_view(ob)

        # set data deserializer
        if 'deserializer' in args:
            request.deserializer = args['deserializer']

        # do schema validation
        if 'schema' in args:
            validate_colander_schema(args['schema'], request)
        elif hasattr(ob, 'schema'):
            validate_colander_schema(ob.schema, request)

        # the validators can either be a list of callables or contain some
        # non-callable values. In which case we want to resolve them using the
        # object if any
        validators = args.get('validators', ())
        for validator in validators:
            if is_string(validator) and ob is not None:
                validator = getattr(ob, validator)
            validator(request)

        # only call the view if we don't have validation errors
        if len(request.errors) == 0:
            try:
                # If we have an object, it already has the request.
                if ob:
                    response = view_()
                else:
                    response = view_(request)
            except:
                # cors headers need to be set if an exception was raised
                request.info['cors_checked'] = False
                raise

        # check for errors and return them if any
        if len(request.errors) > 0:
            # We already checked for CORS, but since the response is created
            # again, we want to do that again before returning the response.
            request.info['cors_checked'] = False
            return args['error_handler'](request.errors)

        # if the view returns its own response, cors headers need to be set
        if isinstance(response, Response):
            request.info['cors_checked'] = False

        # We can't apply filters at this level, since "response" may not have
        # been rendered into a proper Response object yet.  Instead, give the
        # request a reference to its api_kwargs so that a tween can apply them.
        # We also pass the object we created (if any) so we can use it to find
        # the filters that are in fact methods.
        request.cornice_args = (args, ob)
        return response

    # return the wrapper, not the function, keep the same signature
    if not is_string(view):
        functools.update_wrapper(wrapper, view)

    # Set the wrapper name to something useful
    wrapper.__name__ = "{0}__{1}".format(func_name(view), method)
    return wrapper
Beispiel #10
0
 def test_func_name_undecorated_function(self):
     self.assertEqual("my_acl", func_name(my_acl))
Beispiel #11
0
 def __init__(self, klass, view):
     self.unbound_view = getattr(klass, view.lower())
     functools.update_wrapper(self, self.unbound_view)
     self.__name__ = func_name(self.unbound_view)
Beispiel #12
0
def decorate_view(view, args, method):
    """Decorate a given view with cornice niceties.

    This function returns a function with the same signature than the one
    you give as :param view:

    :param view: the view to decorate
    :param args: the args to use for the decoration
    :param method: the HTTP method
    """
    def wrapper(request):
        # if the args contain a klass argument then use it to resolve the view
        # location (if the view argument isn't a callable)
        ob = None
        view_ = view
        if 'klass' in args and not callable(view):
            params = dict(request=request)
            if 'factory' in args and 'acl' not in args:
                params['context'] = request.context
            ob = args['klass'](**params)
            if is_string(view):
                view_ = getattr(ob, view.lower())
            elif isinstance(view, _UnboundView):
                view_ = view.make_bound_view(ob)

        # set data deserializer
        if 'deserializer' in args:
            request.deserializer = args['deserializer']

        # do schema validation
        if 'schema' in args:
            validate_colander_schema(args['schema'], request)
        elif hasattr(ob, 'schema'):
            validate_colander_schema(ob.schema, request)

        # the validators can either be a list of callables or contain some
        # non-callable values. In which case we want to resolve them using the
        # object if any
        validators = args.get('validators', ())
        for validator in validators:
            if is_string(validator) and ob is not None:
                validator = getattr(ob, validator)
            validator(request)

        # only call the view if we don't have validation errors
        if len(request.errors) == 0:
            try:
                # If we have an object, it already has the request.
                if ob:
                    response = view_()
                else:
                    response = view_(request)
            except:
                # cors headers need to be set if an exception was raised
                request.info['cors_checked'] = False
                raise

        # check for errors and return them if any
        if len(request.errors) > 0:
            # We already checked for CORS, but since the response is created
            # again, we want to do that again before returning the response.
            request.info['cors_checked'] = False
            return args['error_handler'](request.errors)

        # if the view returns its own response, cors headers need to be set
        if isinstance(response, Response):
            request.info['cors_checked'] = False

        # We can't apply filters at this level, since "response" may not have
        # been rendered into a proper Response object yet.  Instead, give the
        # request a reference to its api_kwargs so that a tween can apply them.
        # We also pass the object we created (if any) so we can use it to find
        # the filters that are in fact methods.
        request.cornice_args = (args, ob)
        return response

    # return the wrapper, not the function, keep the same signature
    if not is_string(view):
        functools.update_wrapper(wrapper, view)

    # Set the wrapper name to something useful
    wrapper.__name__ = "{0}__{1}".format(func_name(view), method)
    return wrapper
Beispiel #13
0
 def test_func_name_class_method(self):
     self.assertEqual("TestServiceWithWrapper.test_wrapped", func_name(TestServiceWithWrapper.test_wrapped))
Beispiel #14
0
 def test_func_name_string(self):
     self.assertEqual("some_string", func_name("some_string"))