Пример #1
0
def render_ajax_form(model, request, name):
    """Render ajax form.
    """
    try:
        result = render_tile(model, request, name)
        selector = request.environ.get('cone.app.form.selector', '#content')
        mode = request.environ.get('cone.app.form.mode', 'inner')
        continuation = request.environ.get('cone.app.continuation')
        form_continue = AjaxFormContinue(result, continuation)
        rendered_form = form_continue.form
        rendered = ajax_form_template % {
            'form': rendered_form,
            'selector': selector,
            'mode': mode,
            'next': form_continue.next
        }
        request.response.body = safe_encode(rendered)
        return request.response
    except Exception:
        result = '<div>Form rendering error</div>'
        selector = request.environ.get('cone.app.form.selector', '#content')
        mode = request.environ.get('cone.app.form.mode', 'inner')
        tb = format_traceback()
        continuation = AjaxMessage(tb, 'error', None)
        form_continue = AjaxFormContinue(result, [continuation])
        rendered = ajax_form_template % {
            'form': form_continue.form.replace(u'\n', u' '),  # XXX: replace?
            'selector': selector,
            'mode': mode,
            'next': form_continue.next
        }
        return Response(rendered)
Пример #2
0
def render_ajax_form(model, request, name):
    """Render ajax form.
    """
    try:
        result = render_tile(model, request, name)
        selector = request.environ.get('cone.app.form.selector', '#content')
        mode = request.environ.get('cone.app.form.mode', 'inner')
        continuation = request.environ.get('cone.app.continuation')
        form_continue = AjaxFormContinue(result, continuation)
        rendered_form = form_continue.form
        rendered = ajax_form_template % {
            'form': rendered_form,
            'selector': selector,
            'mode': mode,
            'next': form_continue.next
        }
        request.response.body = safe_encode(rendered)
        return request.response
    except Exception:
        result = '<div>Form rendering error</div>'
        selector = request.environ.get('cone.app.form.selector', '#content')
        mode = request.environ.get('cone.app.form.mode', 'inner')
        tb = format_traceback()
        continuation = AjaxMessage(tb, 'error', None)
        form_continue = AjaxFormContinue(result, [continuation])
        rendered = ajax_form_template % {
            'form': form_continue.form.replace(u'\n', u' '),  # XXX: replace?
            'selector': selector,
            'mode': mode,
            'next': form_continue.next
        }
        return Response(rendered)
Пример #3
0
def settings_tab_content(model, request):
    """Used by jquerytools tabs plugin to get settings section content.
    """
    try:
        rendered = render_tile(model, request, 'content')
    except Exception:
        localizer = get_localizer(request)
        error = localizer.translate(_('error', default='Error'))
        rendered = '<div>{}: {}</div>'.format(error, format_traceback())
    return Response('<div class="{}">{}</div>'.format(model.name, rendered))
Пример #4
0
def settings_tab_content(model, request):
    """Used by jquerytools tabs plugin to get settings section content.
    """
    try:
        rendered = render_tile(model, request, 'content')
    except Exception:
        localizer = get_localizer(request)
        error = localizer.translate(_('error', default='Error'))
        rendered = '<div>{}: {}</div>'.format(error, format_traceback())
    return Response('<div class="{}">{}</div>'.format(model.name, rendered))
Пример #5
0
 def test_format_traceback(self):
     class MyException(Exception):
         pass
     try:
         raise MyException('Error!')
     except MyException:
         self.checkOutput("""
         <pre>Traceback (most recent call last):
           File "...", line ..., in test_format_traceback
             raise MyException('Error!')
           ...MyException: Error!
         </pre>
         """, format_traceback())
Пример #6
0
    def test_format_traceback(self):
        class MyException(Exception):
            pass

        try:
            raise MyException('Error!')
        except MyException:
            self.checkOutput(
                """
            <pre>Traceback (most recent call last):
              File "...", line ..., in test_format_traceback
                raise MyException('Error!')
              ...MyException: Error!
            </pre>
            """, format_traceback())
Пример #7
0
def internal_server_error(request):
    """Internal server error view.
    """
    tb = format_traceback()
    if not request.is_xhr:
        return Response(ERROR_PAGE % {'error': tb})
    from cone.app.browser.ajax import (
        AjaxContinue,
        AjaxMessage,
    )
    continuation = AjaxContinue([AjaxMessage(tb, 'error', None)]).definitions
    ret = {
        'mode': 'NONE',
        'selector': 'NONE',
        'payload': '',
        'continuation': continuation,
    }
    response = Response(json.dumps(ret))
    response.content_type = 'application/json'
    return response
Пример #8
0
def internal_server_error(request):
    """Internal server error view.
    """
    tb = format_traceback()
    if not request.is_xhr:
        return Response(ERROR_PAGE % {'error': tb})
    from cone.app.browser.ajax import (
        AjaxContinue,
        AjaxMessage,
    )
    continuation = AjaxContinue([AjaxMessage(tb, 'error', None)]).definitions
    ret = {
        'mode': 'NONE',
        'selector': 'NONE',
        'payload': '',
        'continuation': continuation,
    }
    response = Response(json.dumps(ret))
    response.content_type = 'application/json'
    return response
Пример #9
0
def ajax_tile(model, request):
    """bdajax ``ajaxaction`` implementation for cone.

    * Renders tile with name ``bdajax.action``.

    * Uses definitions from ``request.environ['cone.app.continuation']``
      for continuation definitions.
    """
    try:
        name = request.params['bdajax.action']
        ActionContext(model, request, name)
        rendered = render_tile(model, request, name)
        continuation = request.environ.get('cone.app.continuation')
        if continuation:
            continuation = AjaxContinue(continuation).definitions
        else:
            continuation = False
        return {
            'mode': request.params.get('bdajax.mode'),
            'selector': request.params.get('bdajax.selector'),
            'payload': rendered,
            'continuation': continuation,
        }
    except Forbidden:
        request.response.status = 403
        return {}
    except Exception:
        logging.exception('Error within ajax tile')
        tb = format_traceback()
        continuation = AjaxContinue([AjaxMessage(tb, 'error',
                                                 None)]).definitions
        return {
            'mode': 'NONE',
            'selector': 'NONE',
            'payload': '',
            'continuation': continuation,
        }
Пример #10
0
def ajax_tile(model, request):
    """bdajax ``ajaxaction`` implementation for cone.

    * Renders tile with name ``bdajax.action``.

    * Uses definitions from ``request.environ['cone.app.continuation']``
      for continuation definitions.
    """
    try:
        name = request.params['bdajax.action']
        ActionContext(model, request, name)
        rendered = render_tile(model, request, name)
        continuation = request.environ.get('cone.app.continuation')
        if continuation:
            continuation = AjaxContinue(continuation).definitions
        else:
            continuation = False
        return {
            'mode': request.params.get('bdajax.mode'),
            'selector': request.params.get('bdajax.selector'),
            'payload': rendered,
            'continuation': continuation,
        }
    except Forbidden:
        request.response.status = 403
        return {}
    except Exception:
        logging.exception('Error within ajax tile')
        tb = format_traceback()
        continuation = AjaxContinue(
            [AjaxMessage(tb, 'error', None)]).definitions
        return {
            'mode': 'NONE',
            'selector': 'NONE',
            'payload': '',
            'continuation': continuation,
        }
Пример #11
0
def internal_server_error(request):
    """Internal server error view.
    """
    tb = format_traceback()
    if not request.is_xhr:
        # XXX: request.response.status = 500
        #      return {'errors': error_dict}
        return Response(ERROR_PAGE % {'error': tb})
    # XXX: Check request relates to bdajax action
    #      Check if json request and modify response as needed
    from cone.app.browser.ajax import (
        AjaxContinue,
        AjaxMessage,
    )
    continuation = AjaxContinue([AjaxMessage(tb, 'error', None)]).definitions
    ret = {
        'mode': 'NONE',
        'selector': 'NONE',
        'payload': '',
        'continuation': continuation,
    }
    response = Response(json.dumps(ret))
    response.content_type = 'application/json'
    return response