async def files(request): try: info = request.json c_id = int(info['c_id']) ci_id = info['ci_id'] ci_name = info['ci_name'] except: do_log(4, "get_report:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) try: studata=await db_select("userCourseRole",c_id=c_id) for stu in studata: if stu['report_res'] != None and stu['report_res'] != '': report_res = innerjson.loads(stu['report_res']) for report in report_res: if(report.get(str(ci_id))!=None): reportTime = report.get(str(ci_id)).get('size') stu['report_res']=reportTime break else: stu['report_res']=None return json({'state': 0, 'student': studata}) except: do_log(4, "get_report:数据库错误。") return json({'state': -1, 'info': '数据库错误。'})
async def exp_post(request): divide = { "0": ["RegionOne", "RegionTwo", "RegionThree"], "1": ["RegionTwo", "RegionThree"], "2": ["RegionOne", "RegionThree"], "3": ["RegionThree"], "4": ["RegionTwo", "RegionOne"], "5": ["RegionTwo"], "6": ["RegionOne"] } result = [] try: info = await db_select("courseTemplate") for inf in info: if inf['state'] == '7': continue for div in divide[inf['state']]: mid = innerjson.dumps(inf) temp = innerjson.loads(mid) temp["region"] = div result.append(temp) do_log(4, "get_exp:获取成功") return json({'state': 0, 'info': result}) except Exception as e: # print(e) do_log(4, "get_exp:state解析错误") return json({'state': -1, 'info': 'state解析错误'})
async def exp_post(request): try: info = request.json resp = await aio_post(vmURL + 'image', info) return json(resp) except: do_log(4, "get_exp:OpenStack交互失败") return json({'state': -1, 'info': 'OpenStack交互失败'})
async def get_userCourseOne(request): try: info = request.json resp = await aio_patch(vmURL + 'serveraction', info) return json(resp) except: do_log(4, "get_server:OpenStack交互失败") return json({'state': -1, 'info': 'OpenStack交互失败'})
async def get_userCourseOne(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "get_user:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) info = await db_select("user", id=id) return json({'state': 0, 'user': info})
def start(): try: with open(pid_path, "a") as f: f.write(str(os.getpid()) + "\n") app.add_task(save_pid()) app.run(host="0.0.0.0", port=port, log_config=LOGGING, workers=4) except Exception as e: do_log(4, "start failed!") raise
async def course_get(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "select_course:id不规范或不存在") return json({'state': -1, 'info': 'id不规范或不存在'}) course = await db_select("teacherView", author=id) do_log(2, "select_course:课程查询成功![id=%d]" % (id, )) return json({'state': 0, 'course': course})
async def course_state(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "select_course:id不规范或不存在") return json({'state': -1, 'info': 'id不规范或不存在'}) course = await db_select("userCourse", user_id=id) time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") do_log(2, "select_course:课程查询成功![id=%d]" % (id, )) return json({'state': 0, 'course': course, 'time': time})
async def get_user(request): """ get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "get_user:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) return_info = await db_select("userView", user_id = id ) """ return_info = await db_select("userView") do_log(2, "get_user:用户信息查询成功![admin]") return json({'state': 0, 'user': return_info})
async def get_software(request): """ get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "get_software:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) return_info = await db_select("software", find = ['name', 'resource_path', 'description', 'author', 'is_public'], id = id ) """ return_info = await db_select("softwareView") do_log(2, "get_software:软件查询成功![admin]") return json({'state': 0, 'software': return_info})
async def get_image(request): """ get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "get_image:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) return_info = await db_select("image", find = ['name', 'author', 'container_format', 'disk_format'], id = id ) """ return_info = await db_select("imageView") do_log(2, "get_image:镜像查询成功![admin]") return json({'state': 0, 'image': return_info})
async def get_userCourse(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "get_userCourse:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) info = await db_select("userCourse", course_author=id) ##print(info) #infos=[] #for i in info: # infos.append({'uc_id':i[0],'user_id':i[1],'course_id':i[2],'role_id':i[3],'course_author':i[4],'user_name':i[5],'user_email':i[6],'user_number':i[7],'user_phone':i[8],'course_name':i[9],'course_res':i[10],'course_state':i[11],'role_name':i[12],'role_description':i[13],'uc_state':i[14],'course_expired':i[15]}) return json({'state': 0, 'userCourse': info})
async def get_software(request): try: get_info = request.raw_args id = int(get_info['id']) except: do_log(4, "get_software:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) softwares = await db_select( "software", find=["id", "name", "resource_path", "description", "is_public"], author=id) do_log(2, "get_software:软件信息查询成功![id=%d]" % (id, )) return json({"state": 0, "software": softwares})
async def show_course(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "show_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) course_detail = [] chapters = await db_select("courseChapter", find=["id", "name"], parent=id) for i in chapters: item = await db_select("courseItem", find=["id", "name"], parent=i['id']) course_detail.append({"chapter": i, "item": item}) do_log(2,"show_course:查询课程详细信息成功![id=%d]" % (id,)) return json({'state': 0, 'course': course_detail})
async def get_course(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "select_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) course = await db_select("course", author=id) # #print(course) # if len(course) < 1: # do_log(2, "select_course:该教师无课程[id=%d]" % (id,)) # return json({'state': 1, 'info': '该教师无课程'}) do_log(2, "select_course:课程查询成功![id=%d]" % (id,)) return json({'state': 0, 'course': course})
async def update_user(request): try: get_info = request.raw_args id = int(get_info['id']) data = request.json except: do_log(4, "update_userRole:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("ur", set={ "updated_at": time, "u_id": int(data['u_id']), "r_id": int(data['r_id']) }, id=id): do_log(2, "update_userRole:用户信息修改成功![id=%d]" % (id, )) return json({'state': 0, 'info': '成功'}) else: do_log(4, "update_userRole:数据库操作失败[id=%d]" % (id, )) return json({'state': -2, 'info': '数据库操作失败'}) except: do_log(4, "update_userRole:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def update_flavor(request): try: get_info = request.raw_args id = int(get_info['id']) data = request.json except: do_log(4, "update_flavor:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("flavor", set={ "updated_at": time, "name": data['name'], "author": int(data['author']), "ram": int(data['ram']), "cpu": int(data['cpu']) }, id=id): do_log(2, "update_flavor:配置修改成功![id=%d]" % (id, )) return json({'state': 0, 'info': '成功'}) else: do_log(4, "update_flavor:数据库操作失败[id=%d]" % (id, )) return json({'state': -2, 'info': '数据库操作失败'}) except: do_log(4, "add_flavor:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def update_image(request): try: get_info = request.raw_args id = int(get_info['id']) data = request.json except: do_log(4, "update_image:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("image", set={ "updated_at": time, "name": data['name'], "author": int(data['author']), "resource_path": data['resource_path'], "container_format": data['container_format'], "disk_format": data['disk_format'], }, id=id): do_log(2, "update_image:镜像修改成功![id=%d]" % (id, )) return json({'state': 0, 'info': '成功'}) else: do_log(4, "update_image:数据库操作失败[id=%d]" % (id, )) return json({'state': -2, 'info': '数据库操作失败'}) except: do_log(4, "update_image:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def update_role(request): try: get_info = request.raw_args id = int(get_info['id']) data = request.json except: do_log(4, "update_role:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("role", set={ "updated_at": time, "name": data['name'], "description": data['description'], "privilege": data['privilege'], }, id=id): do_log(2, "update_role:用户组修改成功![id=%d]" % (id, )) return json({'state': 0, 'info': '成功'}) else: do_log(4, "update_role:数据库操作失败[id=%d]" % (id, )) return json({'state': -2, 'info': '数据库操作失败'}) except: do_log(4, "update_role:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def update_software(request): try: get_info = request.raw_args id = int(get_info['id']) data = request.json assert len(data) == 4 except: do_log(4, "update_software:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("software", set={ "updated_at": time, "name": data['name'], "description": data['description'], "author": int(data['author']), "is_public": int(data['is_public']) }, id=id): do_log(2, "update_software:软件修改成功![id=%d]" % (id, )) return json({'state': 0, 'info': '成功'}) else: do_log(4, "update_software:数据库操作失败[id=%d]" % (id, )) return json({'state': -2, 'info': '数据库操作失败'}) except: do_log(4, "update_software:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def update_user(request): try: get_info = request.raw_args id = int(get_info['id']) data = request.json except: do_log(4, "update_user:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("user", set={ "updated_at": time, "name": data['name'], "email": data['email'], "stu_number": data['stu_number'], "phone": data['phone'], }, id=id): do_log(2, "update_user:用户信息修改成功![id=%d]" % (id, )) return json({'state': 0, 'info': '成功'}) else: do_log(4, "update_user:数据库操作失败[id=%d]" % (id, )) return json({'state': -2, 'info': '数据库操作失败'}) except: do_log(4, "update_user:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def update_course(request): get_info = request.raw_args try: id = int(get_info['id']) data = request.json # assert len(data) == 1 except: do_log(4, "update_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") try: if await db_update("uc", set={ 'updated_at': time, 'state': data['state'] }, id=id): do_log(2, "update_course:课程更新成功![id=%d]" % (id, )) return json({'state': 0, 'info': '操作成功'}) else: do_log(4, "update_course:写入数据库时发生错误[id=%d]" % (id, )) return json({'state': -2, 'info': '写入数据库时发生错误'}) except: do_log(4, "update_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'})
async def new_uc(request): get_info = request.raw_args try: c_id = int(get_info['id']) data = request.json role = data['role'] except: do_log(4, "update_uc:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) for rol in role: info = await db_select("ur", find=["u_id"], r_id=int(rol)) time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") for inf in info: #print(inf['u_id']) try_get = await db_select("uc", c_id=c_id, u_id=inf['u_id']) if not (try_get == []): continue try: if await db_insert("uc", updated_at=time, created_at=time, u_id=inf['u_id'], c_id=c_id, state=''): do_log(2, "add:新增选课成功![id=%d]" % (inf['u_id'])) except Exception as e: #print(e) do_log(4, "add:新增选课失败![id=%d]" % (inf['u_id'])) return json({'state': -1, 'info': '数据库插入失败'}) do_log(2, "insert_uc:用户组发布成功!") return json({'state': 0, 'info': '成功'})
async def update_template(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "select_template:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) template = await db_select("template", id=id) # #print(course) # if len(course) < 1: # do_log(2, "select_course:该教师无课程[id=%d]" % (id,)) # return json({'state': 1, 'info': '该教师无课程'}) do_log(2, "select_template:template查询成功![id=%d]" % (id, )) return json({'state': 0, 'template': template})
async def add_user(request): total = 0 unlist = [] try: users = await db_select("user") for user in users: if (user['op_user'] == None or user['op_user'] == ''): total += 1 unlist.append(user) do_log(2, "get_invalid:查询成功![admin]") return json({'state': 0, 'total': total, 'list': unlist}) except Exception as e: do_log(4, "add_soft:参数缺失或不规范") return json({'state': -1, 'info': 'database select error.'})
async def exp_post(request): get_info = request.raw_args try: id = int(get_info['id']) template = int(get_info['template']) except: do_log(4, "get_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) try: resp = await aio_delete(vmURL + 'environment?' + 'template_id=' + str(template) + '&user_id=' + str(id)) return json(resp) except Exception as e: do_log(4, "get_exp:虚拟机交互失败") return json({'state': -1, 'info': '虚拟机交互失败'})
async def exp_post(request): get_info = request.raw_args try: id = int(get_info['id']) template = int(get_info['template']) except: do_log(4, "get_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) try: data = {"user_id": id, "template_id": template} resp = await aio_post(vmURL + 'environment', data) return json(resp) except: do_log(4, "get_exp:参数缺失或不规范") return json({'state': -1, 'info': '虚拟机交互失败'})
async def files(request): try: file = request.files.get('file') if not file: do_log(4, "文件为空") return json({'state': -1, 'info': '操作失败'}) filefix = file.name.split('.')[-1] filenameS = ospath + str(uuid.uuid4()) + '.' +filefix async with aiofiles.open( filenameS , 'wb+') as f: await f.write(file.body) # #print(str(file.name + filenameS )) return json({ "state": 0, "state":file.name , "res": filenameS }) except Exception as e: #print(e) do_log(4, "file_upload:upload failed") return json({'state': -1, 'info': 'file_upload:upload failed'})
async def delete_role(request): get_info = request.raw_args try: id = int(get_info['id']) except: do_log(4, "get_course:参数缺失或不规范") return json({'state': -1, 'info': '参数缺失或不规范'}) try: total = 0 success = 0 tids = await db_select("target", find=["template", "user"], deleted=0, user=id) print(tids) return json(tids) except Exception as e: do_log(4, "delete_user:用户存在未关闭的实验,且删除环境失败。") return json({'state': -1, 'info': '用户存在未关闭的实验,且删除环境失败。'})
async def files(request): try: # #print(dir(request)) # #print(dir(request.stream._format)) file = request.files.get('file') if not file: do_log(4, "文件为空") return json({'state': -1, 'info': '操作失败'}) filefix = file.name.split('.')[-1] filenameS = str(uuid.uuid4()) + '.' +filefix time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") async with aiofiles.open( filepath + filenameS , 'wb+') as f: await f.write(file.body) # #print(str(file.name + filenameS )) return json({ "state": 0, "state":file.name ,"time": time,"res": fileGetpath +filenameS }) except Exception as e: #print(e) do_log(4, "file_upload:upload failed") return json({'state': -1, 'info': 'file_upload:upload failed'})