def manually_handle_validation(self):
        # This is done to check that we don't break compatibility
        # with external modules that perform custom validation like tgext.socketio

        controller = self.__class__.validate_function
        args = (2, 'NaN')
        try:
            output = ''
            validate_params = get_params_with_argspec(controller, {}, args)
            params = DecoratedController._perform_validate(
                controller, validate_params)
        except validation_errors as inv:
            handler, output = DecoratedController._handle_validation_errors(
                controller, args, {}, inv, None)

        return output
Esempio n. 2
0
    def manually_handle_validation(self):
        # This is done to check that we don't break compatibility
        # with external modules that perform custom validation like tgext.socketio

        controller = self.__class__.validate_function
        args = (2, 'NaN')
        try:
            output = ''
            validate_params = get_params_with_argspec(controller, {}, args)
            params = DecoratedController._perform_validate(controller,
                                                           validate_params)
        except validation_errors as inv:
            handler, output = DecoratedController._handle_validation_errors(controller,
                                                                            args, {},
                                                                            inv, None)

        return output
Esempio n. 3
0
    def _call(self, controller, params, remainder=None, tgl=None):
        """Run the controller with the given parameters.

        _call is called by _perform_call in CoreDispatcher.

        Any of the before_validate hook, the validation, the before_call hook,
        and the controller method can return a FormEncode Invalid exception,
        which will give the validation error handler the opportunity to provide
        a replacement decorated controller method and output that will
        subsequently be rendered.

        This allows for validation to display the original page or an
        abbreviated form with validation errors shown on validation failure.

        The before_render hook provides a place for functions that are called
        before the template is rendered. For example, you could use it to
        add and remove from the dictionary returned by the controller method,
        before it is passed to rendering.

        The after_render hook can act upon and modify the response out of
        rendering.

        """
        if tgl is None: #pragma: no cover
            #compatibility with old code that didn't pass request locals explicitly
            tgl = tg.request.environ['tg.locals']

        self._initialize_validation_context(tgl)

        #This is necessary to prevent spurious Content Type header which would
        #cause problems to paste.response.replace_header calls and cause
        #responses wihout content type to get out with a wrong content type
        resp_headers = tgl.response.headers
        if not resp_headers.get('Content-Type'):
            resp_headers.pop('Content-Type', None)

        if remainder:
            remainder = tuple(map(url2pathname, remainder or []))
        else:
            remainder = tuple()

        tg_decoration = controller.decoration
        try:
            tg_decoration.run_hooks(tgl, 'before_validate', remainder, params)

            validate_params = get_params_with_argspec(controller, params, remainder)

            # Validate user input
            params = self._perform_validate(controller, validate_params)

            tgl.tmpl_context.form_values = params

            tg_decoration.run_hooks(tgl, 'before_call', remainder, params)

            params, remainder = remove_argspec_params_from_params(controller, params, remainder)

            #apply controller wrappers
            try:
                controller_caller = tgl.config['controller_caller']
            except KeyError:
                controller_caller = call_controller

            # call controller method
            output = controller_caller(controller, remainder, params)

        except validation_errors as inv:
            controller, output = self._handle_validation_errors(controller, remainder, params, inv)

        #Be sure that we run hooks if the controller changed due to validation errors
        tg_decoration = controller.decoration

        # Render template
        tg_decoration.run_hooks(tgl, 'before_render', remainder, params, output)

        response = self._render_response(tgl, controller, output)
        
        tg_decoration.run_hooks(tgl, 'after_render', response)
        
        return response['response']
Esempio n. 4
0
    def _call(self, controller, params, remainder=None, context=None):
        """Run the controller with the given parameters.

        _call is called by _perform_call in CoreDispatcher.

        Any of the before_validate hook, the validation, the before_call hook,
        and the controller method can return a FormEncode Invalid exception,
        which will give the validation error handler the opportunity to provide
        a replacement decorated controller method and output that will
        subsequently be rendered.

        This allows for validation to display the original page or an
        abbreviated form with validation errors shown on validation failure.

        The before_render hook provides a place for functions that are called
        before the template is rendered. For example, you could use it to
        add and remove from the dictionary returned by the controller method,
        before it is passed to rendering.

        The after_render hook can act upon and modify the response out of
        rendering.

        """
        if context is None:  #pragma: no cover
            #compatibility with old code that didn't pass request locals explicitly
            context = tg.request.environ['tg.locals']

        context_config = tg.config._current_obj()
        self._initialize_validation_context(context)

        #This is necessary to prevent spurious Content Type header which would
        #cause problems to paste.response.replace_header calls and cause
        #responses wihout content type to get out with a wrong content type
        resp_headers = context.response.headers
        if not resp_headers.get('Content-Type'):
            resp_headers.pop('Content-Type', None)

        if remainder:
            remainder = tuple(map(url2pathname, remainder or []))
        else:
            remainder = tuple()

        hooks.notify('before_validate',
                     args=(remainder, params),
                     controller=controller,
                     context_config=context_config)

        try:
            validate_params = get_params_with_argspec(controller, params,
                                                      remainder)

            # Validate user input
            params = self._perform_validate(controller, validate_params)
            context.request.validation['values'] = params

            params, remainder = remove_argspec_params_from_params(
                controller, params, remainder)
            bound_controller_callable = controller
        except validation_errors as inv:
            instance, controller = self._process_validation_errors(
                controller, remainder, params, inv, context=context)
            bound_controller_callable = partial(controller, instance)

        hooks.notify('before_call',
                     args=(remainder, params),
                     controller=controller,
                     context_config=context_config)

        #apply controller wrappers
        try:
            default_controller_caller = context_config['controller_caller']
        except KeyError:
            default_controller_caller = call_controller

        try:
            dedicated_controller_wrappers = context_config[
                'dedicated_controller_wrappers']
            controller_caller = dedicated_controller_wrappers.get(
                default_im_func(controller), default_controller_caller)
        except KeyError:
            controller_caller = default_controller_caller

        # call controller method
        output = controller_caller(bound_controller_callable, remainder,
                                   params)

        # Render template
        hooks.notify('before_render',
                     args=(remainder, params, output),
                     controller=controller,
                     context_config=context_config)

        response = self._render_response(context, controller, output)

        hooks.notify('after_render',
                     args=(response, ),
                     controller=controller,
                     context_config=context_config)

        return response['response']
Esempio n. 5
0
    def _call(self, action, params, remainder=None, context=None):
        """Run the controller with the given parameters.

        _call is called by _perform_call in CoreDispatcher.

        Any of the before_validate hook, the validation, the before_call hook,
        and the controller method can return a FormEncode Invalid exception,
        which will give the validation error handler the opportunity to provide
        a replacement decorated controller method and output that will
        subsequently be rendered.

        This allows for validation to display the original page or an
        abbreviated form with validation errors shown on validation failure.

        The before_render hook provides a place for functions that are called
        before the template is rendered. For example, you could use it to
        add and remove from the dictionary returned by the controller method,
        before it is passed to rendering.

        The after_render hook can act upon and modify the response out of
        rendering.

        """
        if context is None:  #pragma: no cover
            #compatibility with old code that didn't pass request locals explicitly
            context = tg.request.environ['tg.locals']

        hooks = tg.hooks
        context_config = tg.config._current_obj()
        context.request._fast_setattr('validation', _ValidationStatus())

        # This is necessary to prevent spurious Content Type header which would
        # cause problems to paste.response.replace_header calls and cause
        # responses without content type to get out with a wrong content type
        resp_headers = context.response.headers
        if not resp_headers.get('Content-Type'):
            resp_headers.pop('Content-Type', None)

        if remainder:
            remainder = tuple(map(url2pathname, remainder or []))
        else:
            remainder = tuple()

        hooks.notify('before_validate',
                     args=(remainder, params),
                     controller=action)

        validate_params = get_params_with_argspec(action, params, remainder)
        context.request.args_params = validate_params  # Update args_params with positional args

        try:
            params = self._perform_validate(action, validate_params, context)
        except validation_errors as inv:
            instance, error_handler, chain_validation = self._process_validation_errors(
                action, remainder, params, inv, context=context)
            while chain_validation:
                # The validation asked for chained validation,
                # go on and validate the error_handler too.
                try:
                    params = self._perform_validate(error_handler,
                                                    validate_params, context)
                except validation_errors as inv:
                    instance, error_handler, chain_validation = self._process_validation_errors(
                        error_handler, remainder, params, inv, context=context)
                else:
                    chain_validation = False
            action = error_handler
            bound_controller_callable = partial(error_handler, instance)
        else:
            bound_controller_callable = action
            context.request.validation.values = params
            remainder, params = flatten_arguments(action, params, remainder)

        hooks.notify('before_call',
                     args=(remainder, params),
                     controller=action)

        # call controller method with applied wrappers
        controller_caller = action.decoration.controller_caller
        output = controller_caller(context_config, bound_controller_callable,
                                   remainder, params)

        # Render template
        hooks.notify('before_render',
                     args=(remainder, params, output),
                     controller=action)

        response = self._render_response(context, action, output)

        hooks.notify('after_render', args=(response, ), controller=action)

        return response['response']
Esempio n. 6
0
    def _call(self, controller, params, remainder=None):
        """Run the controller with the given parameters.

        _call is called by _perform_call in Pylons' WSGIController.

        Any of the before_validate hook, the validation, the before_call hook,
        and the controller method can return a FormEncode Invalid exception,
        which will give the validation error handler the opportunity to provide
        a replacement decorated controller method and output that will
        subsequently be rendered.

        This allows for validation to display the original page or an
        abbreviated form with validation errors shown on validation failure.

        The before_render hook provides a place for functions that are called
        before the template is rendered. For example, you could use it to
        add and remove from the dictionary returned by the controller method,
        before it is passed to rendering.

        The after_render hook can act upon and modify the response out of
        rendering.

        """

        self._initialize_validation_context()

        #This is necessary to prevent spurious Content Type header which would
        #cause problems to paste.response.replace_header calls and cause
        #responses wihout content type to get out with a wrong content type
        if not pylons.response.headers.get('Content-Type'):
            pylons.response.headers.pop('Content-Type', None)

        pylons.request.start_response = getattr(self, 'start_response', None)

        remainder = map(url2pathname, remainder or [])

        tg_decoration = controller.decoration
        try:
            if 'tg_format' in params:
                pylons.request.headers['tg_format'] = params['tg_format']

            tg_decoration.run_hooks('before_validate', remainder, params)

            validate_params = get_params_with_argspec(
                controller, params, remainder)

            for ignore in config.get('ignore_parameters', []):
                if params.get(ignore):
                    del params[ignore]

            # Validate user input
            params = self._perform_validate(controller, validate_params)

            pylons.tmpl_context.form_values = params

            tg_decoration.run_hooks('before_call', remainder, params)

            params, remainder = remove_argspec_params_from_params(
                controller, params, remainder)

            #apply controller wrappers
            controller_callable = tg_decoration.wrap_controller(controller)

            # call controller method
            output = controller_callable(*remainder, **dict(params))

        except validation_errors, inv:
            controller, output = self._handle_validation_errors(
                controller, remainder, params, inv)
Esempio n. 7
0
    def _call(self, controller, params, remainder=None, context=None):
        """Run the controller with the given parameters.

        _call is called by _perform_call in CoreDispatcher.

        Any of the before_validate hook, the validation, the before_call hook,
        and the controller method can return a FormEncode Invalid exception,
        which will give the validation error handler the opportunity to provide
        a replacement decorated controller method and output that will
        subsequently be rendered.

        This allows for validation to display the original page or an
        abbreviated form with validation errors shown on validation failure.

        The before_render hook provides a place for functions that are called
        before the template is rendered. For example, you could use it to
        add and remove from the dictionary returned by the controller method,
        before it is passed to rendering.

        The after_render hook can act upon and modify the response out of
        rendering.

        """
        if context is None: #pragma: no cover
            #compatibility with old code that didn't pass request locals explicitly
            context = tg.request.environ['tg.locals']

        hooks = tg.hooks
        context_config = tg.config._current_obj()
        context.request._fast_setattr('validation', _ValidationStatus())

        #This is necessary to prevent spurious Content Type header which would
        #cause problems to paste.response.replace_header calls and cause
        #responses wihout content type to get out with a wrong content type
        resp_headers = context.response.headers
        if not resp_headers.get('Content-Type'):
            resp_headers.pop('Content-Type', None)

        if remainder:
            remainder = tuple(map(url2pathname, remainder or []))
        else:
            remainder = tuple()

        hooks.notify('before_validate', args=(remainder, params),
                     controller=controller, context_config=context_config)

        try:
            validate_params = get_params_with_argspec(controller, params, remainder)

            # Validate user input
            params = self._perform_validate(controller, validate_params, context)
            context.request.validation.values = params

            params, remainder = remove_argspec_params_from_params(controller, params, remainder)
            bound_controller_callable = controller
        except validation_errors as inv:
            instance, controller = self._process_validation_errors(controller,
                                                                   remainder, params,
                                                                   inv, context=context)
            bound_controller_callable = partial(controller, instance)

        hooks.notify('before_call', args=(remainder, params),
                     controller=controller, context_config=context_config)

        # call controller method with applied wrappers
        controller_caller = controller.decoration.controller_caller
        output = controller_caller(context_config, bound_controller_callable, remainder, params)

        # Render template
        hooks.notify('before_render', args=(remainder, params, output),
                     controller=controller, context_config=context_config)

        response = self._render_response(context, controller, output)

        hooks.notify('after_render', args=(response,),
                     controller=controller, context_config=context_config)

        return response['response']
Esempio n. 8
0
    def _call(self, controller, params, remainder=None):
        """Run the controller with the given parameters.

        _call is called by _perform_call in Pylons' WSGIController.

        Any of the before_validate hook, the validation, the before_call hook,
        and the controller method can return a FormEncode Invalid exception,
        which will give the validation error handler the opportunity to provide
        a replacement decorated controller method and output that will
        subsequently be rendered.

        This allows for validation to display the original page or an
        abbreviated form with validation errors shown on validation failure.

        The before_render hook provides a place for functions that are called
        before the template is rendered. For example, you could use it to
        add and remove from the dictionary returned by the controller method,
        before it is passed to rendering.

        The after_render hook can act upon and modify the response out of
        rendering.

        """

        self._initialize_validation_context()

        #This is necessary to prevent spurious Content Type header which would
        #cause problems to paste.response.replace_header calls and cause
        #responses wihout content type to get out with a wrong content type
        if not pylons.response.headers.get('Content-Type'):
            pylons.response.headers.pop('Content-Type', None)

        pylons.request.start_response = getattr(self, 'start_response', None)

        remainder = map(url2pathname, remainder or [])

        tg_decoration = controller.decoration
        try:
            if 'tg_format' in params:
                pylons.request.headers['tg_format'] = params['tg_format']

            tg_decoration.run_hooks('before_validate', remainder, params)

            validate_params = get_params_with_argspec(controller, params,
                                                      remainder)

            for ignore in config.get('ignore_parameters', []):
                if params.get(ignore):
                    del params[ignore]

            # Validate user input
            params = self._perform_validate(controller, validate_params)

            pylons.tmpl_context.form_values = params

            tg_decoration.run_hooks('before_call', remainder, params)

            params, remainder = remove_argspec_params_from_params(
                controller, params, remainder)

            #apply controller wrappers
            controller_callable = tg_decoration.wrap_controller(controller)

            # call controller method
            output = controller_callable(*remainder, **dict(params))

        except validation_errors, inv:
            controller, output = self._handle_validation_errors(
                controller, remainder, params, inv)
Esempio n. 9
0
 def tg_get_params_with_argspec(controller, *args, **kw):
     return get_params_with_argspec(*args, **kw)