Exemplo n.º 1
0
def calculate():
    try:
        # NOTE: request must be "Content-Type: application/json"
        if not flask.request.is_json:
            res = {"reason": "Request content-type must be json"}
            return flask.make_response((json.dumps(res),
                                        exc.HTTPBadRequest.code))

        params = flask.request.get_json()

        if 'src_filename' in params:
            # NOTE: UI只能获取文件名,限定文件必须在当前目录
            src_filename = params['src_filename']
            dst_filename = os.path.splitext(src_filename)[0] + '-result.xls'
            params['source_data'] = os.path.join(work_path, src_filename)
            params['output_file'] = os.path.join(work_path, dst_filename)

        global calculator
        calculator = cal_mitama.Calculator(params)
        result_num = calculator.run()
        ret = exc.HTTPOk.code
        res = {"result_num": result_num,
               "output_file": params['output_file']}
    except IOError:
        ret = exc.HTTPForbidden.code
        res = {"reason": "Please check: 1.source_file exists;"
               " 2.output_file is writable and not be opened by other process."
               " %s" % traceback.format_exc()}
    except Exception:
        ret = exc.HTTPInternalServerError.code
        res = {"reason": traceback.format_exc()}

    return flask.make_response((json.dumps(res), ret))
Exemplo n.º 2
0
def calculate():
    try:
        # NOTE: request must be "Content-Type: application/json"
        if not flask.request.is_json:
            res = {"reason": "Request content-type must be json"}
            return flask.make_response(
                (json.dumps(res), exc.HTTPBadRequest.code))

        params = flask.request.get_json()

        if 'src_filename' in params:
            # NOTE: UI只能获取文件名,限定文件必须在当前目录
            src_filename = params['src_filename']
            dst_filename = os.path.splitext(src_filename)[0] + '-result.xls'
            work_path = os.getcwd()
            params['source_data'] = os.path.join(work_path, src_filename)
            params['output_file'] = os.path.join(work_path, dst_filename)

        calculator = cal_mitama.Calculator(params)
        result_num = calculator.run()
        ret = exc.HTTPOk.code
        res = {"result_num": result_num, "output_file": params['output_file']}
    except Exception:
        ret = exc.HTTPInternalServerError.code
        res = {"reason": traceback.format_exc()}

    return flask.make_response((json.dumps(res), ret))
def calculate():
    try:
        # NOTE: request must be "Content-Type: application/json"
        if not flask.request.is_json:
            return flask.make_response(('Request content-type must be json',
                                        400))
        params = flask.request.get_json()
        calculator = cal_mitama.Calculator(params)
        calculator.run()
        return flask.make_response(('Calculate finished', 200))
    except Exception:
        return flask.make_response((traceback.format_exc(), 500))