Exemple #1
0
def ajax_form(request, result):
    flash = views.pop_flash(request)

    if isinstance(result, httpexceptions.HTTPRedirection):
        request.response.headers.extend(result.headers)
        result = {'status': 'okay'}
    elif isinstance(result, httpexceptions.HTTPError):
        request.response.status_code = result.code
        result = {'status': 'failure', 'reason': str(result)}
    else:
        errors = result.pop('errors', [])
        if errors:
            status_code = result.pop('code', 400)
            request.response.status_code = status_code
            result['status'] = 'failure'

            result.setdefault('errors', {})
            for e in errors:
                if isinstance(e, colander.Invalid):
                    result['errors'].update(e.asdict())
                elif isinstance(e, dict):
                    result['errors'].update(e)

        reasons = flash.pop('error', [])
        if reasons:
            assert(len(reasons) == 1)
            request.response.status_code = 400
            result['status'] = 'failure'
            result['reason'] = reasons[0]

    result['flash'] = flash

    return result
Exemple #2
0
 def wrapper(context, request):
     if request.method == 'POST':
         data = request.json_body
         data.update(request.params)
         request.content_type = 'application/x-www-form-urlencoded'
         request.POST.clear()
         request.POST.update(data)
     inst = view(request)
     meth = getattr(inst, self.attr)
     result = meth()
     result = ajax_form(request, result)
     result['flash'] = views.pop_flash(request)
     result['model'] = views.model(request)
     result.pop('form', None)
     return result
Exemple #3
0
 def wrapper(context, request):
     if request.method == 'POST':
         data = request.json_body
         data.update(request.params)
         request.content_type = 'application/x-www-form-urlencoded'
         request.POST.clear()
         request.POST.update(data)
     inst = view(request)
     meth = getattr(inst, self.attr)
     result = meth()
     result = ajax_form(request, result)
     result['flash'] = views.pop_flash(request)
     result['model'] = views.model(request)
     result.pop('form', None)
     return result