示例#1
0
 def safe_render(self, request, content):
     content = maybe_async(content)
     try:
         if is_renderer(content):
             self.content_type = content.content_type()
             content = maybe_async(content.render(request))
         return content
     except Exception as e:
         return as_failure(e)
示例#2
0
 def safe_render(self, request, content):
     content = maybe_async(content)
     try:
         if is_renderer(content):
             self.content_type = content.content_type()
             content = maybe_async(content.render(request))
         return content
     except Exception as e:
         return as_failure(e)
示例#3
0
def _finish(request, editing, fhtml, force_redirect, response):
    view = request.view
    f = fhtml.form
    if is_renderer(response):
        return response
    elif response == f:
        return fhtml.maker.json_messages(f)
    data = f.rawdata
    success_message = fhtml.success_message or view.success_message
    msg = success_message(request, response)
    f.add_message(msg)
    url = None
    # Save and continue. Redirect to referrer if not AJAX or send messages
    if forms.SAVE_AND_CONTINUE_KEY in data:
        if editing:
            if request.is_xhr:
                return fhtml.maker.json_messages(f)
            else:
                #editing and continuing to edit
                curr = request.environ.get('HTTP_REFERER')
                set_request_message(f,request)
                raise HttpRedirect(curr)
        else:
            url = view.redirect_url(request, response, 'change')
    elif force_redirect:
        url = get_redirect(request, data, response)

    if not url:
        if request.is_xhr:
            return fhtml.maker.json_messages(f)
        else:
            set_request_message(f,request)
            return view.get_response(request)
    else:
        set_request_message(f, request)
        raise HttpRedirect(url)
示例#4
0
def _finish(request, editing, fhtml, force_redirect, response):
    view = request.view
    f = fhtml.form
    if is_renderer(response):
        return response
    elif response == f:
        return fhtml.maker.json_messages(f)
    data = f.rawdata
    success_message = fhtml.success_message or view.success_message
    msg = success_message(request, response)
    f.add_message(msg)
    url = None
    # Save and continue. Redirect to referrer if not AJAX or send messages
    if forms.SAVE_AND_CONTINUE_KEY in data:
        if editing:
            if request.is_xhr:
                return fhtml.maker.json_messages(f)
            else:
                #editing and continuing to edit
                curr = request.environ.get('HTTP_REFERER')
                set_request_message(f, request)
                raise HttpRedirect(curr)
        else:
            url = view.redirect_url(request, response, 'change')
    elif force_redirect:
        url = get_redirect(request, data, response)

    if not url:
        if request.is_xhr:
            return fhtml.maker.json_messages(f)
        else:
            set_request_message(f, request)
            return view.get_response(request)
    else:
        set_request_message(f, request)
        raise HttpRedirect(url)
示例#5
0
    def handle_error(self, request, response):
        '''Render an error into text or Html depending on *content_type*.
This function can be overwritten by user implementation.'''
        exc_info = request.exc_info
        settings = request.settings
        status = 500
        if exc_info and exc_info[0] is not None:
            status = getattr(exc_info[1], 'status', status)
        else:
            exc_info = None
        # 302 is a special case, we redirect
        content_type = request.content_type
        if status in REDIRECT_CODES:
            location = dict(exc_info[1].headers)['location']
            if request.is_xhr:
                content = ajax.jredirect(request.environ, location)
            else:
                content_type = None
                response.headers['Location'] = iri_to_uri(location)
                content = ''
        else:
            err_cls = '%s%s' % (classes.error, status)
            err_title = '%s %s' % (status, error_title(status))
            if content_type == 'text/plain':
                content = err_title
                if settings.DEBUG:
                    content += '\n\n' + html_trace(exc_info, plain=True)
            else:
                inner = Widget('div', cn=(classes.error, err_cls))
                if settings.DEBUG:
                    inner.addClass('debug')
                    inner.add(Widget('h2', err_title))
                    inner.add(Widget('a', request.path, href=request.path))
                    inner.add(html_trace(exc_info))
                else:
                    func_name = 'render_error_%s' % status
                    if hasattr(self, func_name):
                        text = getattr(self, func_name)(request, status)
                    else:
                        text = error_title
                    inner.add(text)
                if request.is_xhr:
                    content = ajax.jservererror(request.environ,
                                                inner.render(request))
                else:
                    try:
                        layout = request.view.root.get_page_layout(err_cls,
                                                                   classes.error,
                                                                   'default')
                        outer = layout()
                        content = outer.render(request,
                                               context={'content': inner})
                    except:
                        logger.error('Could not render %s error on %s layout',
                                     status, layout, exc_info=True)
                        content = inner.render(request)
        if is_renderer(content):
            response.status_code = 200
            response.content_type = content.content_type()
            content = content.render(request)
        else:
            response.content_type = content_type
            response.status_code = status
        if status == 500:
            logger.critical('Interval server error', exc_info=exc_info)
        return content
示例#6
0
    def handle_error(self, request, response):
        '''Render an error into text or Html depending on *content_type*.
This function can be overwritten by user implementation.'''
        exc_info = request.exc_info
        settings = request.settings
        status = 500
        if exc_info and exc_info[0] is not None:
            status = getattr(exc_info[1], 'status', status)
        else:
            exc_info = None
        # 302 is a special case, we redirect
        content_type = request.content_type
        if status in REDIRECT_CODES:
            location = dict(exc_info[1].headers)['location']
            if request.is_xhr:
                content = ajax.jredirect(request.environ, location)
            else:
                content_type = None
                response.headers['Location'] = iri_to_uri(location)
                content = ''
        else:
            err_cls = '%s%s' % (classes.error, status)
            err_title = '%s %s' % (status, error_title(status))
            if content_type == 'text/plain':
                content = err_title
                if settings.DEBUG:
                    content += '\n\n' + html_trace(exc_info, plain=True)
            else:
                inner = Widget('div', cn=(classes.error, err_cls))
                if settings.DEBUG:
                    inner.addClass('debug')
                    inner.add(Widget('h2', err_title))
                    inner.add(Widget('a', request.path, href=request.path))
                    inner.add(html_trace(exc_info))
                else:
                    func_name = 'render_error_%s' % status
                    if hasattr(self, func_name):
                        text = getattr(self, func_name)(request, status)
                    else:
                        text = error_title
                    inner.add(text)
                if request.is_xhr:
                    content = ajax.jservererror(request.environ,
                                                inner.render(request))
                else:
                    try:
                        layout = request.view.root.get_page_layout(
                            err_cls, classes.error, 'default')
                        outer = layout()
                        content = outer.render(request,
                                               context={'content': inner})
                    except:
                        logger.error('Could not render %s error on %s layout',
                                     status,
                                     layout,
                                     exc_info=True)
                        content = inner.render(request)
        if is_renderer(content):
            response.status_code = 200
            response.content_type = content.content_type()
            content = content.render(request)
        else:
            response.content_type = content_type
            response.status_code = status
        if status == 500:
            logger.critical('Interval server error', exc_info=exc_info)
        return content