api.py 3.5 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
# -*- coding: utf_8 -*-
import os
import json
import datetime
import logging
import traceback
import uuid

from flask import Blueprint, request, redirect, url_for, json, Response, send_file, make_response, send_from_directory
wanli's avatar
wanli committed
11
from werkzeug.utils import secure_filename
wanli's avatar
wanli committed
12
from app import config, signalManager
wanli's avatar
wanli committed
13
from app.setting import conf
wanli's avatar
wanli committed
14 15 16
from fullstack.login import Auth
from fullstack.response import ResponseCode, response_result
from fullstack.validation import validate_schema
wanli's avatar
wanli committed
17
from schema.api import UpdatePasswordSchema, ApplicationBuildSchema
wanli's avatar
wanli committed
18 19 20 21 22 23 24

logger = logging.getLogger("api")

api = Blueprint("api", __name__, url_prefix="/api/v1/%s" % config['NAME'])

def stopApp():
    fpath = os.sep.join([os.getcwd(), "restart.json"])
wanli's avatar
wanli committed
25
    with open(fpath, "w+") as f:
wanli's avatar
wanli committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        ret = json.loads(f.read())
        ret["count"] = ret["count"] + 1
        f.write(json.dumps(ret, indent=4))
    return ret

def actionShowReport():
    fpath = os.sep.join([os.getcwd(), "results", "TPC-E_dm.pdf"])
    with open(fpath, "rb") as f:
        ret = f.read()
    return ret

@api.route("/updatePassword", methods=['POST'])
@validate_schema(UpdatePasswordSchema)
@Auth.auth_required
def update_password():
    result, message = signalManager.actionUpdatePassword.emit(request.schema_data)
    if result:
        return response_result(ResponseCode.OK, data=result, msg=message)
    else:
        return response_result(ResponseCode.NOTHING_CHANGE, msg=message)

@api.route("/upload", methods=['POST']) # 上传文件
def upload_file():
    try:
        result, message = signalManager.actionUploadFile.emit()
        if result:
            return response_result(ResponseCode.OK, data=result, msg=message)
        else:
            return response_result(ResponseCode.REQUEST_ERROR, msg=message)
    except Exception as e:
        traceback.print_exc()
        logger.error(str(e))
        return response_result(ResponseCode.SERVER_ERROR, msg=str(e))

@api.route("/system/updateDatabase", methods=['GET'])
def update_db():
    result = []
wanli's avatar
wanli committed
63 64 65 66
    for index in range(16):
        print(index)
        result.append(str(uuid.uuid1()))

wanli's avatar
wanli committed
67
    return response_result(ResponseCode.OK, data=result)
wanli's avatar
wanli committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81

@api.route("/application/build", methods=["post"])
@validate_schema(ApplicationBuildSchema)
def application_build():
    try:
        if request.method == 'POST' and 'binfiles' in request.files:
            files = []
            data = request.schema_data
            dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
            upload_path = os.sep.join([config["UPLOAD_PATH"], config["UPLOAD_DIR"], conf.get('uploads', 'temp_dir'), str(data['access_key']), dt])
            if not os.path.exists(upload_path):
                os.makedirs(upload_path)
            for f in request.files.getlist('binfiles'):
                filename = secure_filename(f.filename)
wanli's avatar
wanli committed
82
                print(filename)
wanli's avatar
wanli committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                file_path = os.sep.join([upload_path, filename])
                f.save(file_path)
                files.append(file_path)

            result, message = signalManager.actionApplicationBuild.emit(files, data)
            if result:
                return response_result(ResponseCode.OK, data=result, msg=message)
            else:
                return response_result(ResponseCode.REQUEST_ERROR, msg=message)
        else:
            return response_result(ResponseCode.REQUEST_ERROR, msg="files can not be null")
    except Exception as e:
        traceback.print_exc()
        logger.error(str(e))
        return response_result(ResponseCode.SERVER_ERROR, msg=str(e))