def login(): username = request.json.get("username") password = request.json.get("password") print(username) user_id = db_session.query(User).filter_by(username=username) if user_id is None: return make_response(jsonify({'code': 401, 'error_msg': u'用户不存在!'})) passwd = db_session.query(User.password).filter_by(id=user_id) if password == passwd: return make_response(jsonify({'code': 200, 'msg': u'登陆成功!'})) else: return make_response(jsonify({'code': 200, 'msg': u'密码错误!'}))
def api_scriptslist(*args,**kwargs): api_id = request.json.get("api_id") try: user_apis = db_session.query(UserScript.id).filter_by(api_id=api_id).all() scripts = db_session.query(Script).filter(Script.api_user_id.in_(user_apis)).all() data = {} for script in scripts: data = {"id": script.id, "description": script.description, "script_content": script.script_content} return make_response(jsonify({"code": 200, "msg": data})) except Exception as e: return make_response(jsonify({"code": 404,"error_msg":e}))
def script_delete(*args,**kwargs): user_id = request.json.get('user_id') script_id = request.json.get('script_id') try: script = db_session.query(UserScript).filter_by(user_id=user_id).filter_by(script_id=script_id).one() if script.id is not None: db_session.query(Script).filter_by(script_id=script_id).delete() db_session.commit() return make_response(jsonify({"code": 200, "msg": u'脚本删除成功'})) except Exception as e: return make_response(jsonify({"code": 404, "msg": e}))
def api_upgrade(*args,**kwargs): api_id = request.json.get('api_id') # 接口描述 # project_name = request.json.get("project_name") module_sub = request.json.get("module_sub") api_description = request.json.get("api_description") try: db_session.query(Api).filter_by(id=api_id).upgrade(module_sub=module_sub, api_description=api_description) db_session.commit() return make_response(jsonify({"code":200,"msg": u'修改接口成功'})) except Exception as e: return make_response(jsonify({"code":404,"error_msg": u'新增接口失败'}))
def project_update(*args, **kwargs): # 判断是否还有挂接的接口 project_id = request.json.get("project_id") project = request.json.get("project_name") version = request.json.get("project_version") try: db_session.query(Project).filter_by(id=project_id).update( project=project, version=version) db_session.commit() return make_response(jsonify({'code': 200, 'msg': u'删除项目成功!'})) except Exception: return make_response(jsonify({'code': 401, 'error_msg': u'删除项目失败!'}))
def script_upgrade(*args,**kwargs): user_id = request.json.get('user_id') script_id = request.json.get('script_id') script_content = request.json.get('script_content') description = request.json.get('script_description') try: script = db_session.query(UserScript).filter_by(user_id=user_id).filter_by(script_id=script_id).one() if script.id is not None: db_session.query(Script).filter_by(script_id=script_id).update(script_content=script_content,description=description) db_session.commit() return make_response(jsonify({"code": 200, "msg": u'脚本更新成功'})) except Exception as e: return make_response(jsonify({"code": 404, "msg": e}))
def api_list(*args,**kwargs): project_name = request.json.get("project") try: data = {} project = db_session.query(Project).filter_by(project=project_name).one() apis = db_session.query(Api).filter_by(project_id=project.id).all() for api in apis: data = { "id":api.id, "project_id":api.project_id, "module_sub":api.module_sub, "api_description":api.api_description } # data = {"test": '我是test'} return make_response(jsonify({"code":200,"msg":data})) except Exception as e: return make_response(jsonify({"code": 404,"error_msg": e}))
def api_delete(*args,**kwargs): api_id = request.json.get('api_id') # 接口描述 try: api = db_session.query(Api).filter_by(id=api_id).one() db_session.delete(api) db_session.commit() return make_response(jsonify({"code":200,"msg": u'删除接口成功'})) except Exception as e: return make_response(jsonify({"code":404,"error_msg": u'新增接口失败'}))
def results_delete(*args, **kwargs): results_id = request.json.get("results_id") # 有可能报错这里 try: result = db_session.query(Results).filter( Results.id.in_(results_id)).all() db_session.delete(result) db_session.commit() return make_response(jsonify({"code": 200, "msg": u'删除成功'})) except Exception as e: print('results_delete接口有可能results_id参数格式错误') return make_response(jsonify({"code": 404, "error_msg": u'删除失败'}))
def script_add(*args,**kwargs): # 用户自己添加脚本或者, username = request.json.get('username') api_description = request.json.get('api_description') try: user = db_session.query(User).filter_by(username=username).one() user_id = user.id api = db_session.query(Api).filter_by(api_description=api_description).one() api_id = api.id api_user = db_session.query(ApiUser).filter_by(user_id=user_id,api_id=api_id).one() api_user_id = api_user.id except Exception as e: return make_response(jsonify({"code": 404,"error_msg":e})) script_content = request.json.get('script_content') description = request.json.get('script_description') try: script = Script(api_user_id=api_user_id,script_content=script_content,description=description) db_session.add(script) db_session.commit() return make_response(jsonify({"code": 200,"msg": u'脚本添加成功'})) except Exception as e: return make_response(jsonify({"code": 404, "error_msg": e}))
def api_add(*args,**kwargs): project_name = request.json.get("project_name") module_sub = request.json.get("module_sub") api_description = request.json.get("api_description") # 接口描述 try: project = db_session.query(Project).filter_by(project=project_name).one() api = Api(project_id=project.id,module_sub=module_sub,api_description=api_description) db_session.add(api) db_session.commit() return make_response(jsonify({"code":200,"msg": u'新增接口成功'})) except Exception as e: return make_response(jsonify({"code":404,"error_msg": u'新增接口失败'}))
def project_list(*args, **kwargs): # project_name = request.values.get("project") try: datas = [] projects = db_session.query(Project).all() for project in projects: data = { # "id":project.id, "project": project.project, # "module_sub":project.module_sub, "version": project.version } datas.append(data) return make_response(jsonify({"code": 200, "msg": datas})) except Exception as e: return make_response(jsonify({"code": 404, "error_msg": e}))
def script_list(*args,**kwargs): # db_session.query() username = request.json.get("username") api = request.json.get("api") try: if username is not None and api is None: user = db_session.query(User).filter_by(username=username).one() user_apis = db_session.query(UserScript.id).filter_by(user_id=user.id).all() scripts = db_session.query(Script).filter(Script.api_user_id.in_(user_apis)).all() content = [] for script in scripts: data = {"id":script.id,"description":script.description, "script_content": script.script_content} content.append(data) return make_response(jsonify({"code": 200,"msg":content})) elif username is None and api is not None: content = [] api = db_session.query(Api).filter_by(api=api).one() user_apis = db_session.query(UserScript.id).filter_by(api_id=api.id).all() scripts = db_session.query(Script).filter(Script.api_user_id.in_(user_apis)).all() for script in scripts: data = {"id":script.id,"description":script.description, "script_content": script.script_content} content.append(data) return make_response(jsonify({"code": 200, "msg": content})) else: user_api = db_session.query(UserScript.id).filter_by(user_id=api.id).filter_by(api_id=api.id).one() script = db_session.query(Script).filter_by(api_user_id=user_api).one() content = [] data = {"id": script.id, "description": script.description, "script_content": script.script_content} content.append(data) return make_response(jsonify({"code": 200, "msg": content})) except Exception as e: return make_response(jsonify({"code": 404, "error_msg": e}))
def results_list(*args, **kwargs): user_id = request.json.get('user_id') api_id = request.json.get('api_id') status = request.json.get('status') try: if user_id is not None and api_id is None and status is None: content = [] results = db_session.query(Results).filter_by( user_id=user_id).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) elif user_id is not None and api_id is not None and status is None: content = [] results = db_session.query(Results).filter_by( user_id=user_id).filter_by(api_id=api_id).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) elif user_id is not None and api_id is not None and status is not None: content = [] results = db_session.query(Results).filter_by( user_id=user_id).filter_by(api_id=api_id).filter_by( status=status).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) elif user_id is not None and api_id is None and status is not None: content = [] results = db_session.query(Results).filter_by( user_id=user_id).filter_by(status=status).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) elif user_id is None and api_id is not None and status is not None: content = [] results = db_session.query(Results).filter_by( api_id=api_id).filter_by(status=status).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) elif user_id is None and api_id is None and status is not None: content = [] results = db_session.query(Results).filter_by(status=status).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) elif user_id is None and api_id is not None and status is None: content = [] results = db_session.query(Results).filter_by(api_id=api_id).all() for result in results: data = { "id": result.id, "api_id": result.api_id, "status": result.status, "request_data": result.request_dat, "response_data": result.response_data, "date": result.date, "response_code": result.response_code } content.append(data) return make_response(jsonify({"code": 200, "msg": content})) except Exception as e: print(e) return make_response(jsonify({"code": 200, "error_msg": u'查询失败'}))