Esempio n. 1
0
    def outputResponse(self, data, data_type, filename=None):
        outputFormData = self.data.copy() # need copy, else it's unmutable
        outputFormData['template'] = self.output_template
        if self.output == 'json-html': # special output that runs the webscript with html output,
                                       # then wraps around this the action buttons and other stuff for the amcat navigator website
            if data_type == unicode:
                scriptoutput = data
            else:
                cls = scriptmanager.findScript(data_type, 'html')
                if not cls: raise Exception('html output not supported')
                scriptoutput = cls(outputFormData).run(data)
            return self.outputJsonHtml(scriptoutput)
        else:
            cls = scriptmanager.findScript(data_type, self.output)
            if not cls: raise Exception('invalid output type')
            #cnf = {'template':self.output_template} if self.output == 'html' else None
            mimetype = mimetypeDict[self.output]
            out = cls(outputFormData).run(data)

            response = HttpResponse(mimetype=mimetype['mime'])
            response.write(out)
            if filename or mimetype['download'] == True:
                if not filename:
                    filename = 'AmCAT %s' % self.name
                response['Content-Disposition'] = 'attachment; filename="%s.%s"' % (filename, mimetype['extension'])
            return response
Esempio n. 2
0
def handler(request, action):
    script = get_script(action)
    result = None
    output = "json"
    output = request.GET.get("format", "json")
    if request.POST:
        form = script.options_form(request.POST)
        if form.is_valid():
            try:
                if hasattr(script, "get_response"):
                    return script(request.POST).get_response()
                else:
                    result = script(request.POST).run(None)
                    converter = scriptmanager.findScript(script.output_type, output)
                    if converter:
                        result = converter().run(result)
                        mimetype = "text/csv"
                    else:
                        result = json.dumps(result)
                        mimetype = "application/json"
                    return HttpResponse(result, status=201, mimetype=mimetype)
            except Exception as e:
                log.exception("Error on running action: {script.__class__.__name__}".format(**locals()))
                error = {
                    "error-class": e.__class__.__name__,
                    "error-message": str(e),
                    "error-traceback": traceback.format_exc(),
                }
                return HttpResponse(json.dumps(error), mimetype="application/json", status=500)
        else:
            log.error(
                "Error on form validation: \nPOST:{request.POST}\nform.data:{form.data}\nerrors:{form.errors}".format(
                    **locals()
                )
            )
            return HttpResponse(json.dumps(form.errors), mimetype="application/json", status=400)

    else:
        form = script.options_form
        help_text = script.__doc__
        status = 200
        return render(request, "api/action.html", locals())
Esempio n. 3
0
def showErrorMsg(text, outputType, fields=None):
    errormsg = types.ErrorMsg(text, fields=fields)
    cls = scriptmanager.findScript(types.ErrorMsg, outputType)
    output = cls().run(errormsg) if cls else text
    return HttpResponse(output, mimetype='text/plain')
def handleOutput(out, output_type):
    cls = scriptmanager.findScript(output_type, str)
    if not cls:
        print(out)
    else:
        print cls().run(out)