async def upload(file): filename, content_type = file.filename, file.content_type print(filename, content_type) file_name,file_suffix = Parse.file2name_suffix(filename) # mysql connection # 获取文件名称 result = db.fetch_one(CommonSQL.TYPE_NAME, content_type, file_suffix) dir_name = result.get("name", "") # 校验目录是否为系统支持资源目录 if dir_name in TypePath: _directory_name_path = os.path.join(RESOURCE_PATH, dir_name) # 拼接资源存放目录 if os.path.exists(_directory_name_path): pass else: os.chdir(RESOURCE_PATH) os.mkdir(dir_name) u_filename = Tools.uuid_name(filename) filename_full_path = os.path.join(_directory_name_path, u_filename) # 校验是否需要创建资源记录 if db.fetch_one( CommonSQL.FILENAME_CHECK, file_name).get("number") == 0: resource_id = Tools.uid() file_data = await file.read() with open(filename_full_path, 'wb') as f: f.write(file_data) # 获取文件大小 size = os.path.getsize(filename_full_path) db.insert(CommonSQL.RESOURCE_CREATE, resource_id, file_name, size, file_suffix, filename_full_path) return { "id": resource_id, "path": filename_full_path, "filename": filename, "content_type": content_type, "size": size, } else: raise UnicornException(StatusCode.R20004["code"], filename + StatusCode.R20004["msg"]) else: raise UnicornException(StatusCode.R20003["code"], filename + StatusCode.R20003["msg"])
async def read(script_id): result = db.fetch_one(CommonSQL.AGENT_SCRIPT_ONE,script_id,0) if result: return result else: raise UnicornException(StatusCode.R20002["code"], StatusCode.R20002["msg"])
async def random_question(): question_list = db.fetch_all(CommonSQL.IQ_VALIDATE_ALL_ID) if question_list: random_id = random.choice(question_list).get('id') return db.fetch_one(CommonSQL.IQ_VALIDATE_GET_ONE, random_id) code = ErrorCode.no_resources raise UnicornException(code, ErrorINFO[code])
def check_user_status_record(username): sql = """ SELECT status FROM todo_user WHERE `ID`=(SELECT `user_id` FROM todo_user_auth WHERE `identifier`=%s) AND is_del=%s """ result = db.fetch_one(sql, (username, 0)) status = result.get("status") if result else False return True if status != False else status
def get_rsa_record(access_id): """ 获取rsa """ sql = "SELECT `private_key` FROM todo_rsa WHERE `id`=%s" result = db.fetch_one(sql, access_id) private_key = result.get("private_key", "") return private_key if private_key else False
def check_sec_record(cn_name): """ 校验去重秘钥类型 """ sql = "SELECT COUNT(*) AS number FROM sec_code WHERE `cn_name`= %s" result = db.fetch_one(sql, cn_name) count = result.get("number") return True if count == 0 else False
def get_user_name(id): """ 查询具体用户 """ sql = "SELECT `nick_name` FROM todo_user WHERE `id`= %s" record = db.fetch_one(sql, id) result = record.get("login_name", "") return result
async def answer(answer_model): id = answer_model.id answer = answer_model.answer result = db.fetch_one(CommonSQL.IQ_VALIDATE_ANSWER, id, answer) if result: return ErrorCode.success code = ErrorCode.IQ_verification_failed raise UnicornException(code, ErrorINFO[code])
async def create(py_model): _name = py_model.name _version = py_model.py_version _remark = py_model.remark _resource_id = py_model.resource_id _user = py_model.create_by id = Tools.uid() status = db.fetch_one(CommonSQL.AGENT_SCRIPT_CHECK,_name).get("number") if status == 0: _user_id = db.fetch_one(CommonSQL.GET_USER_ID,_user) if _user_id: user_id = _user_id.get("id") db.insert(CommonSQL.AGENT_SCRIPT_CREATE,id,_name,_version,_remark,_resource_id,user_id) else: raise UnicornException(StatusCode.P40001["code"],StatusCode.P40001["msg"]) else: raise UnicornException(StatusCode.R20001["code"],StatusCode.R20001["msg"])
async def update(script_id,py_model): _name = py_model.name _version = py_model.py_version _remark = py_model.remark _status = py_model.status _user = py_model.update_by if db.fetch_one(CommonSQL.AGENT_SCRIPT_ONE,script_id): db.update(CommonSQL.AGENT_SCRIPT_UPDATE, _name, _version, _status, _remark,_user,script_id) else: raise UnicornException(StatusCode.R20006["code"],StatusCode.R20006["msg"])
async def code_detail(id): _path = db.fetch_one(CommonSQL.AGENT_SCRIPT_CODE,id) if _path: path = _path.get("path") print(path,111) if os.path.exists(path): with open(path,'r',encoding='utf-8') as f: content = f.read() return {"py_code":content} raise UnicornException(StatusCode.R20005["code"], StatusCode.R20005["msg"]) else: raise UnicornException(StatusCode.R20002["code"],StatusCode.R20002["msg"])
async def update_code(id,code_model): code = code_model.code _path = db.fetch_one(CommonSQL.AGENT_SCRIPT_CODE, id) if _path: path = _path.get("path") if os.path.exists(path): with open(path, 'w', encoding='utf-8') as f: f.write(code) else: raise UnicornException(StatusCode.R20005["code"], StatusCode.R20005["msg"]) else: raise UnicornException(StatusCode.R20002["code"], StatusCode.R20002["msg"])
async def create_bill(bill_model): _title = bill_model.title _money = bill_model.money _remark = bill_model.remark _payway = bill_model.payway _expenses_kind_id = bill_model.expenses_kind_id _user = bill_model.create_by id = Tools.uid() status = db.fetch_one(CommonSQL.BILL_CHECK, _title).get("number") if status == 0: _user_id = db.fetch_one(CommonSQL.GET_USER_ID, _user) if _user_id: user_id = _user_id.get("id") db.insert(CommonSQL.BILL_CREATE, id, _title, _expenses_kind_id, _payway, _money, _remark, user_id) else: raise UnicornException(StatusCode.P40001["code"], StatusCode.P40001["msg"]) else: raise UnicornException(StatusCode.R20001["code"], StatusCode.R20001["msg"])
async def read(id): result = db.fetch_one(CommonSQL.IQ_VALIDATE_GET_ONE, id) return result if result else None
def get_ancestors_record(parent_id): sql = "SELECT `ancestors` FROM sys_kinds WHERE `id`=%s" _ancestors_dict = db.fetch_one(sql, parent_id) result = _ancestors_dict.get("ancestors", "") return result if result else False
def check_diff_record(cn_name, en_name,parent_id): """硬件库类型校验去重""" sql = "SELECT COUNT(*) as number FROM sys_kinds WHERE `cn_name`=%s AND `en_name`= %s AND `parent_id`=%s" result = db.fetch_one(sql, cn_name, en_name,parent_id) count = result.get("number") return True if count == 0 else False
def check_auth_record(username): sql = """ SELECT `identifier`,`password` FROM todo_user_auth WHERE `identifier`=%s """ return db.fetch_one(sql, username)
def get_user_record(username): print(username) sql = "SELECT `id`,`nick_name` FROM todo_user WHERE `id`=(SELECT `user_id` FROM todo_user_auth WHERE identifier=%s)" return db.fetch_one(sql, username)
async def delete(script_id): script_info = db.fetch_one(CommonSQL.AGENT_SCRIPT_ONE, script_id) if script_info: db.delete(CommonSQL.AGENT_SCRIPT_DEL,script_id) else: raise UnicornException(StatusCode.R20006["code"], StatusCode.R20006["msg"])