def remove_label_to_subject(): # 数据头需为json格式 if request.headers['Content-Type'] == 'application/json': args = request.json current_app.logger.debug('get_token args: {}'.format(args)) else: raise InvalidMessage('only support json data', 404) subject_id = args['subject_id'] label_ids = args['labels'] # 获取题对象 try: subject = com_get(Subject, id=subject_id) except Exception as e: current_app.logger.error("[subject][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) for label_id in label_ids: # 获取标签对象 try: label = com_get(Label, id=label_id) except Exception as e: current_app.logger.error("[label][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) try: paper_helper.remove_label_to_subject(subject, label) except Exception as e: current_app.logger.error("[subject][remove_label] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) db.session.commit() return return_data('update success', 200)
def remove_user_to_paper(): # 数据头需为json格式 if request.headers['Content-Type'] == 'application/json': args = request.json current_app.logger.debug('get_token args: {}'.format(args)) else: raise InvalidMessage('only support json data', 404) paper_id = args['paper_id'] user_ids = args['users'] # 获取题对象 try: paper = com_get(Paper, id=paper_id) except Exception as e: current_app.logger.error("[paper][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) for user_id in user_ids: # 获取用户对象 try: user = com_get(User, id=user_id) except Exception as e: current_app.logger.error("[user][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 添加用户给考卷 try: paper_helper.remove_user_to_paper(paper, user) except Exception as e: current_app.logger.error("[paper][remove_user] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) try: # 同步数据到数据库 db.session.commit() except Exception as e: current_app.logger.error("{} model update exception: {}".format(Paper, e)) raise e return return_data('update success', 200)
def remove_group_to_head(): # 数据头需为json格式 if request.headers['Content-Type'] == 'application/json': args = request.json current_app.logger.debug('get_token args: {}'.format(args)) else: raise InvalidMessage('only support json data', 404) head_id = args['head_id'] group_ids = args['groups'] # 获取题对象 try: head = com_get(Head, id=head_id) except Exception as e: current_app.logger.error("[head][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) for group_id in group_ids: # 获取组对象 try: group = com_get(Group, id=group_id) except Exception as e: current_app.logger.error("[group][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 添加组给用户 try: paper_helper.remove_group_to_head(head, group) except Exception as e: current_app.logger.error("[head][remove_group] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) db.session.commit() return return_data('update success', 200)
def create_paper(): # 数据头需为json格式 if request.headers['Content-Type'] == 'application/json': args = request.json current_app.logger.debug('get_token args: {}'.format(args)) else: raise InvalidMessage('only support json data', 404) paper_dict = args.get('paper', '') user_list = args.get('users', '') question_list = args.get('questions', '') try: # 获取post内容 paper = Paper(**paper_dict) except Exception as e: current_app.logger.error("{} model init exception: {}".format(Paper, e)) current_app.logger.error("model_data: {}".format(paper_dict)) raise e if user_list: for user_id in user_list: # 获取组对象 try: user = User.query.filter_by(id=user_id).one() except Exception as e: current_app.logger.error("[user][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 添加标题给组 try: paper_helper.add_user_to_paper(paper, user) except Exception as e: current_app.logger.error("[paper][add_user] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) if question_list: for question_dict in question_list: try: # 获取post内容 question = Question(**question_dict) except Exception as e: current_app.logger.error("{} model init exception: {}".format(Paper, e)) current_app.logger.error("model_data: {}".format(paper_dict)) raise e question.paper = paper db.session.add(question) # 添加对象 db.session.add(paper) # 获取head对象, add有时可能加载不到paper.head try: head = com_get(Head, id=paper.head_id) except Exception as e: current_app.logger.error("[head][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) paper_helper.compute_score(head) try: # 同步数据到数据库 db.session.commit() except Exception as e: current_app.logger.error("{} model init exception: {}".format(Paper, e)) current_app.logger.error("model_data: {}".format(args)) raise e data = paper_helper.make_paper_reponse_body(paper) return return_data(data, 201)
def remove_role_from_user(): # 数据头需为json格式 if request.headers['Content-Type'] == 'application/json': args = request.json current_app.logger.debug('get_token args: {}'.format(args)) else: raise InvalidMessage('only support json data', 404) user_id = args['user_id'] role_ids = args['roles'] try: user = user_datastore.find_user(id=user_id) except Exception as e: current_app.logger.error("[user][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) for role_id in role_ids: try: role = com_get(Role, id=role_id) except Exception as e: current_app.logger.error( "[role][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) try: user_datastore.remove_role_from_user(user, role) except Exception as e: current_app.logger.error( "[user][remove_role] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) db.session.commit() return return_data('update success', 200)
def get_label(id): try: label = com_get(Label, id=id) except Exception as e: current_app.logger.error("[label][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(label) return return_data(data, 200)
def get_head(id): try: head = com_get(Head, id=id) except Exception as e: current_app.logger.error("[head][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = paper_helper.make_head_reponse_body(head) return return_data(data, 200)
def get_question(id): try: question = com_get(Question, id=id) except Exception as e: current_app.logger.error("[question][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(question) return return_data(data, 200)
def get_subject(id): try: subject = com_get(Subject, id=id) except Exception as e: current_app.logger.error("[subject][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = paper_helper.make_subject_reponse_body(subject) return return_data(data, 200)
def get_scene(id): try: scene = com_get(Scene, id=id) except Exception as e: current_app.logger.error("[scene][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(scene) return return_data(data, 200)
def get_role(id): try: role = com_get(Role, id=id) except Exception as e: current_app.logger.error("[role][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) data = user_helper.make_role_reponse_body(role) return return_data(data, 200)
def get_paper(id): try: paper = com_get(Paper, id=id) except Exception as e: current_app.logger.error("[paper][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = paper_helper.make_paper_reponse_body(paper) return return_data(data, 200)
def create_user(): # 数据头需为json格式 if request.headers['Content-Type'] == 'application/json': args = request.json current_app.logger.debug('get_token args: {}'.format(args)) else: raise InvalidMessage('only support json data', 404) user_dict = args.get('user', '') role_list = args.get('roles', '') group_list = args.get('groups', '') user_dict['password'] = hash_password(user_dict['password']) try: # 创建用户 user = user_datastore.create_user(**user_dict) except Exception as e: current_app.logger.error("[user][post] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) if role_list: for role_id in role_list: # 获取角色对象 try: role = com_get(Role, id=role_id) except Exception as e: current_app.logger.error( "[role][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 添加角色给用户 try: user_datastore.add_role_to_user(user, role) except Exception as e: current_app.logger.error( "[user][add_role] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) if group_list: for group_id in group_list: # 获取组对象 try: group = Group.query.filter_by(id=group_id).one() except Exception as e: current_app.logger.error( "[group][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 添加组给用户 try: user_helper.add_group_to_user(user, group) except Exception as e: current_app.logger.error( "[user][add_group] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) try: # 同步数据到数据库 db.session.commit() except Exception as e: current_app.logger.error("{} model init exception: {}".format(User, e)) current_app.logger.error("model_data: {}".format(args)) raise e data = user_helper.make_user_reponse_body(user) return return_data(data, 201)
def get_user_head(id): try: user_head = com_get(UserHead, id=id) except Exception as e: current_app.logger.error( "[user_head][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(user_head) return return_data(data, 200)
def delete_head(id): try: head = com_get(Head, id=id) except Exception as e: current_app.logger.error("[head][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) head.groups = [] # 删除场景 try: com_del(db, Head, id=id) except Exception as e: current_app.logger.error("[head][del] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) return return_data('delete success', 204)
def delete_subject(id): try: subject = com_get(Subject, id=id) except Exception as e: current_app.logger.error("[subject][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) # 清除外键多对多数据关联 subject.labels = [] # 删除试题 try: com_del(db, Subject, id=id) except Exception as e: current_app.logger.error("[subject][del] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) return return_data('delete success', 204)
def delete_paper(id): try: paper = com_get(Paper, id=id) except Exception as e: current_app.logger.error("[paper][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) head = paper.head paper.users = [] paper_helper.compute_score(head) db.session.delete(paper) try: # 同步数据到数据库 db.session.commit() except Exception as e: current_app.logger.error("{} model delete exception: {}".format(Paper, e)) raise e return return_data('delete success', 204)
def delete_group(id): # 获取组对象 try: group = com_get(Group, id=id) except Exception as e: current_app.logger.error("[group][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 从组中移除所有用户 try: user_helper.remove_all_user_to_group(group) except Exception as e: current_app.logger.error( "[group][remove_all_user] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 删除组 try: com_del(db, Group, id=id) except Exception as e: current_app.logger.error("[group][del] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) return return_data('delete success', 204)
def delete_question(): # 获取post内容 if request.headers['Content-Type'] == 'application/json': datas = request.json.get('dic_delete',[]) current_app.logger.debug('com_post args: {}'.format(datas)) else: raise 'only support json data' for data in datas: id = data['id'] try: question = com_get(Question, id=id) except Exception as e: current_app.logger.error("[question][get] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) head = question.paper.head db.session.delete(question) paper_helper.compute_score(head) try: # 同步数据到数据库 db.session.commit() except Exception as e: current_app.logger.error("{} model delete exception: {}".format(Question, e)) raise e return return_data('delete success', 204)