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 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_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 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 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 get_token(): # 数据头需为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) username = args.get('username', '') password = args.get('password', '') if username: # 获取用户对象 try: user = user_datastore.find_user(username=username) except Exception as e: current_app.logger.error( "[user][get] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) if user: # 验证密码 isok = verify_password(password, user.password) if not isok: raise InvalidMessage("password is invalid") else: raise InvalidMessage("username is not found") # 获取token try: token = user.get_auth_token() except Exception as e: current_app.logger.error( "[user][get_auth_token] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) data = user_helper.make_user_reponse_body(user) data['token'] = token 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 update_user(id): # 数据头需为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) if "password" in args: args['password'] = hash_password(args['password']) try: com_put(db, User, args, **{'id': id}) except Exception as e: current_app.logger.error("[user][put] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) return return_data('update success', 200)
def create_group(): try: group = com_post(db, Group) except Exception as e: current_app.logger.error("[group][post] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) data = user_helper.make_group_reponse_body(group) return return_data(data, 201)
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 create_label(): try: label = com_post(db, Label) except Exception as e: current_app.logger.error("[label][post] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(label) return return_data(data, 201)
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_scenes(): try: scenes = com_gets(Scene) except Exception as e: current_app.logger.error("[scene][gets] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) datas = model_helper.obj_list_to_list_dict(scenes) return return_data(datas, 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_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 delete_label(id): # 删除场景 try: com_del(db, Label, id=id) except Exception as e: current_app.logger.error("[label][del] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) return return_data('delete success', 204)
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 create_scene(): try: scene = com_post(db, Scene) except Exception as e: current_app.logger.error("[scene][post] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(scene) return return_data(data, 201)
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_questions(): try: questions = Question.query.order_by('number') except Exception as e: current_app.logger.error("[question][gets] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) datas = model_helper.obj_list_to_list_dict(questions) return return_data(datas, 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 get_labels(): try: labels = com_gets(Label) except Exception as e: current_app.logger.error("[label][gets] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) datas = model_helper.obj_list_to_list_dict(labels) return return_data(datas, 200)
def create_paper_question(): try: paper_question = com_post(db, PaperQuestion) except Exception as e: current_app.logger.error( "[paper_question][post] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(paper_question) 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 get_user_heads(): try: user_heads = com_gets(UserHead) except Exception as e: current_app.logger.error( "[user_head][gets] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) datas = model_helper.obj_list_to_list_dict(user_heads) return return_data(datas, 200)
def create_user_head(): try: user_head = com_post(db, UserHead) except Exception as e: current_app.logger.error( "[user_head][post] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) data = model_helper.obj_to_dict(user_head) return return_data(data, 201)
def get_papers(): try: papers = com_gets(Paper) except Exception as e: current_app.logger.error("[paper][gets] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) datas = [] for paper in papers: datas.append(paper_helper.make_paper_reponse_body(paper)) return return_data(datas, 200)
def get_heads(): try: heads = com_gets(Head) except Exception as e: current_app.logger.error("[head][gets] fail expection: {}".format(e)) return InvalidMessage(str(e), 500) datas = [] for head in heads: datas.append(paper_helper.make_head_reponse_body(head)) return return_data(datas, 200)
def create_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_dict = args.get('head', '') group_list = args.get('groups', '') group_list = args.get('groups', '') try: # 获取post内容 head = Head(**head_dict) except Exception as e: current_app.logger.error("{} model init exception: {}".format(Head, e)) current_app.logger.error("model_data: {}".format(head_dict)) raise e 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: paper_helper.add_group_to_head(head, group) except Exception as e: current_app.logger.error("[head][add_group] fail expection: {}".format(e)) raise InvalidMessage(str(e), 500) # 添加对象 db.session.add(head) try: # 同步数据到数据库 db.session.commit() except Exception as e: current_app.logger.error("{} model init exception: {}".format(Subject, e)) current_app.logger.error("model_data: {}".format(args)) raise e data = paper_helper.make_head_reponse_body(head) return return_data(data, 201)