Пример #1
0
    def run(self, path=None):
        """
        This method wraps all the processing of an HTTP request.

        arguments:
        path -- path part of URL

        returns:
        a 4-tuple: HTTP status, HTTP headers, valid SID flag, response body
        """
        self._install_plugin_actions()
        self._proc_time = time.time()
        path = path if path is not None else self._import_req_path()
        named_args = {}
        headers = []
        action_metadata = self._get_method_metadata(path[0])
        try:
            self.init_session()
            if self.is_action(path[0], action_metadata):
                path, named_args = self.pre_dispatch(path, named_args, action_metadata)
                self._pre_action_validate()
                methodname, tmpl, result = self.process_action(path[0], path, named_args)
            else:
                raise NotFoundException(_('Unknown action [%s]') % path[0])
        except UserActionException as ex:
            self._status = ex.code
            self.add_system_message('error', fetch_exception_msg(ex))
            methodname, tmpl, result = self.process_action('message', path, named_args)
        except Exception as ex:
            # an error outside the action itself (i.e. pre_dispatch, action validation,
            # post_dispatch etc.)
            logging.getLogger(__name__).error(u'%s\n%s' % (ex, ''.join(get_traceback())))
            if settings.is_debug_mode():
                self._status = 500
                self.add_system_message('error', fetch_exception_msg(ex))
            else:
                self.handle_dispatch_error(ex)
            methodname, tmpl, result = self.process_action('message', path, named_args)

        # Let's test whether process_method actually invoked requested method.
        # If not (e.g. there was an error and a fallback has been used) then reload action metadata
        if methodname != path[0]:
            action_metadata = self._get_method_metadata(methodname)

        self._proc_time = round(time.time() - self._proc_time, 4)
        self.post_dispatch(methodname, action_metadata, tmpl, result)
        # response rendering
        headers += self.output_headers(action_metadata.get('return_type', 'html'))
        output = StringIO.StringIO()
        if self._status < 300 or self._status >= 400:
            self.output_result(methodname, tmpl, result, action_metadata, outf=output)
        ans_body = output.getvalue()
        output.close()
        return self._export_status(), headers, self._uses_valid_sid, ans_body
Пример #2
0
 def _create_user_action_err_result(self, ex, return_type):
     """
     arguments:
     ex -- a risen exception
     return_type --
     """
     e2 = self._normalize_error(ex)
     if settings.is_debug_mode() or isinstance(e2, UserActionException):
         user_msg = fetch_exception_msg(e2)
     else:
         user_msg = translate('Failed to process your request. '
                              'Please try again later or contact system support.')
     if return_type == 'json':
         return dict(error_code=getattr(ex, 'error_code', None),
                     error_args=getattr(ex, 'error_args', {}))
     else:
         return dict()
Пример #3
0
 def _create_user_action_err_result(self, ex, return_type):
     """
     arguments:
     ex -- a risen exception
     return_type --
     """
     e2 = self._normalize_error(ex)
     if settings.is_debug_mode() or isinstance(e2, UserActionException):
         user_msg = fetch_exception_msg(e2)
     else:
         user_msg = translate(
             'Failed to process your request. '
             'Please try again later or contact system support.')
     if return_type == 'json':
         return dict(messages=[user_msg],
                     error_code=getattr(ex, 'error_code', None),
                     error_args=getattr(ex, 'error_args', {}))
     else:
         return dict(messages=[user_msg])
Пример #4
0
    def run(self, path=None):
        """
        This method wraps all the processing of an HTTP request.

        arguments:
        path -- path part of URL

        returns:
        a 4-tuple: HTTP status, HTTP headers, valid SID flag, response body
        """
        self._install_plugin_actions()
        self._proc_time = time.time()
        path = path if path is not None else self._import_req_path()
        named_args = {}
        headers = []
        action_metadata = self._get_method_metadata(path[0])
        return_type = action_metadata.get('return_type', 'html')
        try:
            self.init_session()
            if self.is_action(path[0], action_metadata):
                named_args = self.pre_dispatch(named_args, action_metadata)
                self._pre_action_validate()
                methodname, tmpl, result = self.process_action(
                    path[0], named_args)
            else:
                raise NotFoundException(_('Unknown action [%s]') % path[0])
        except CorpusForbiddenException:
            methodname, tmpl, result = self._run_message_action(
                named_args, return_type)
        except UserActionException as ex:
            self._status = ex.code
            self.add_system_message('error', fetch_exception_msg(ex))
            methodname, tmpl, result = self._run_message_action(
                named_args, return_type)
        except werkzeug.exceptions.BadRequest as ex:
            self._status = ex.code
            self.add_system_message('error',
                                    '{0}: {1}'.format(ex.name, ex.description))
            methodname, tmpl, result = self._run_message_action(
                named_args, return_type)
        except Exception as ex:
            # an error outside the action itself (i.e. pre_dispatch, action validation,
            # post_dispatch etc.)
            logging.getLogger(__name__).error(u'%s\n%s' %
                                              (ex, ''.join(get_traceback())))
            if settings.is_debug_mode():
                self._status = 500
                self.add_system_message('error', fetch_exception_msg(ex))
            else:
                self.handle_dispatch_error(ex)
            methodname, tmpl, result = self._run_message_action(
                named_args, return_type)

        # Let's test whether process_method actually invoked requested method.
        # If not (e.g. there was an error and a fallback has been used) then reload action metadata
        if methodname != path[0]:
            action_metadata = self._get_method_metadata(methodname)

        self._proc_time = round(time.time() - self._proc_time, 4)
        self.post_dispatch(methodname, action_metadata, tmpl, result)
        # response rendering
        headers += self.output_headers(return_type)
        output = StringIO.StringIO()
        if self._status < 300 or self._status >= 400:
            self.output_result(methodname,
                               tmpl,
                               result,
                               action_metadata,
                               outf=output)
        ans_body = output.getvalue()
        output.close()
        return self._export_status(), headers, self._uses_valid_sid, ans_body
Пример #5
0
    def run(self, path=None):
        """
        This method wraps all the processing of an HTTP request.

        arguments:
        path -- path part of URL

        returns:
        a 4-tuple: HTTP status, HTTP headers, valid SID flag, response body
        """
        self._install_plugin_actions()
        self._proc_time = time.time()
        path = path if path is not None else self._import_req_path()
        methodname = path[0]
        named_args = {}
        headers = []
        action_metadata = self._get_method_metadata(methodname)
        if not action_metadata:

            def null():
                pass

            action_metadata = {}
            action_metadata.update(exposed()(null).__dict__)
        return_type = action_metadata['return_type']
        try:
            self.init_session()
            if self.is_action(methodname, action_metadata):
                named_args = self.pre_dispatch(methodname, named_args,
                                               action_metadata)
                self._pre_action_validate()
                tmpl, result = self.process_action(methodname, named_args)
            else:
                orig_method = methodname
                methodname = 'message'
                raise NotFoundException(
                    translate('Unknown action [%s]') % orig_method)
        except CorpusForbiddenException as ex:
            self._status = ex.code
            tmpl, result = self._run_message_action(named_args,
                                                    action_metadata, 'error',
                                                    ex.message)
        except UserActionException as ex:
            self._status = ex.code
            msg_args = self._create_user_action_err_result(ex, return_type)
            named_args.update(msg_args)
            tmpl, result = self._run_message_action(named_args,
                                                    action_metadata, 'error',
                                                    ex.message)
        except werkzeug.exceptions.BadRequest as ex:
            self._status = ex.code
            tmpl, result = self._run_message_action(
                named_args, action_metadata, 'error',
                '{0}: {1}'.format(ex.name, ex.description))
        except Exception as ex:
            # an error outside the action itself (i.e. pre_dispatch, action validation,
            # post_dispatch etc.)
            logging.getLogger(__name__).error(u'%s\n%s' %
                                              (ex, ''.join(get_traceback())))
            self._status = 500
            if settings.is_debug_mode():
                message = fetch_exception_msg(ex)
            else:
                message = translate(
                    'Failed to process your request. Please try again later or contact system support.'
                )
            tmpl, result = self._run_message_action(named_args,
                                                    action_metadata, 'error',
                                                    message)

        self._proc_time = round(time.time() - self._proc_time, 4)
        self.post_dispatch(methodname, action_metadata, tmpl, result)
        # response rendering
        headers += self.output_headers(return_type)
        output = StringIO.StringIO()
        if self._status < 300 or self._status >= 400:
            self.output_result(methodname,
                               tmpl,
                               result,
                               action_metadata,
                               outf=output)
        ans_body = output.getvalue()
        output.close()
        return self._export_status(), headers, self._uses_valid_sid, ans_body
Пример #6
0
    def run(self, path=None):
        """
        This method wraps all the processing of an HTTP request.

        arguments:
        path -- path part of URL

        returns:
        a 4-tuple: HTTP status, HTTP headers, valid SID flag, response body
        """
        self._install_plugin_actions()
        self._proc_time = time.time()
        path = path if path is not None else self._import_req_path()
        methodname = path[0]
        named_args = {}
        headers = []
        action_metadata = self._get_method_metadata(methodname)
        if not action_metadata:
            def null(): pass
            action_metadata = {}
            action_metadata.update(exposed()(null).__dict__)
        try:
            self.init_session()
            if self.is_action(methodname, action_metadata):
                named_args = self.pre_dispatch(methodname, named_args, action_metadata)
                self._pre_action_validate()
                tmpl, result = self.process_action(methodname, named_args)
            else:
                orig_method = methodname
                methodname = 'message'
                raise NotFoundException(translate('Unknown action [%s]') % orig_method)
        except CorpusForbiddenException as ex:
            self._status = ex.code
            tmpl, result = self._run_message_action(
                named_args, action_metadata, 'error', ex.message)
        except ImmediateRedirectException as ex:
            tmpl, result = None, None
            self.redirect(ex.url, ex.code)
        except UserActionException as ex:
            self._status = ex.code
            msg_args = self._create_user_action_err_result(ex, action_metadata['return_type'])
            tmpl, result = self._run_message_action(
                msg_args, action_metadata, 'error', ex.message)
        except werkzeug.exceptions.BadRequest as ex:
            self._status = ex.code
            tmpl, result = self._run_message_action(named_args, action_metadata,
                                                    'error', '{0}: {1}'.format(ex.name, ex.description))
        except Exception as ex:
            # an error outside the action itself (i.e. pre_dispatch, action validation,
            # post_dispatch etc.)
            logging.getLogger(__name__).error(u'%s\n%s' % (ex, ''.join(get_traceback())))
            self._status = 500
            if settings.is_debug_mode():
                message = fetch_exception_msg(ex)
            else:
                message = translate(
                    'Failed to process your request. Please try again later or contact system support.')
            tmpl, result = self._run_message_action(named_args, action_metadata, 'error', message)

        self._proc_time = round(time.time() - self._proc_time, 4)
        self.post_dispatch(methodname, action_metadata, tmpl, result)
        # response rendering
        headers += self.output_headers(action_metadata['return_type'])
        output = StringIO.StringIO()
        if self._status < 300 or self._status >= 400:
            self.output_result(methodname, tmpl, result, action_metadata,
                               return_type=action_metadata['return_type'], outf=output)
        ans_body = output.getvalue()
        output.close()
        return self._export_status(), headers, self._uses_valid_sid, ans_body