Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
def update_head(id):
    try:
        com_put(db, Head, **{'id': id})
    except Exception as e:
        current_app.logger.error("[head][put] fail expection: {}".format(e))
    
    return return_data('update success', 200)
Example #5
0
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_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_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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
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)
Example #13
0
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)
Example #15
0
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_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 update_paper_question(id):
    try:
        com_put(db, PaperQuestion, **{'id': id})
    except Exception as e:
        current_app.logger.error(
            "[paper_question][put] fail expection: {}".format(e))

    return return_data('update success', 200)
def update_user_paper(id):
    try:
        com_put(db, UserPaper, **{'id': id})
    except Exception as e:
        current_app.logger.error(
            "[user_paper][put] fail expection: {}".format(e))

    return return_data('update success', 200)
Example #19
0
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)
Example #20
0
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)
Example #21
0
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)
Example #22
0
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)
Example #23
0
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 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 submit_answer(id):
    # 获取post内容
    if request.headers['Content-Type'] == 'application/json':
        args = request.json
        current_app.logger.debug('submit_answer args: {}'.format(args))
    else:
        raise 'only support json data'
    user_answer = args['user_answer']
    try:
        # 查询数据
        paper_question = PaperQuestion.query.filter_by(id=id).one()
    except Exception as e:
        current_app.logger.error("{} key=value filter_by exception: {}".format(
            PaperQuestion, e))
        current_app.logger.error("key=value filter_by: {}".format(id))
        raise e
    paper_status = paper_question.question.paper.status
    if paper_status == 'new':
        return return_data('Examination did not begin', 200)
    elif paper_status == 'end':
        return return_data('The exam has ended', 200)

    paper_question.user_answer = user_answer
    paper_question.status = "pending"
    log_dict = {
        'user_id': current_user.id,
        'context': user_answer,
        'paper_question_id': id
    }
    try:
        # 获取post内容
        log = PaperQuestionLog(**log_dict)
    except Exception as e:
        current_app.logger.error("{} model init exception: {}".format(
            PaperQuestionLog, e))
        current_app.logger.error("model_data: {}".format(log_dict))
        raise e
    try:
        # 同步数据到数据库
        db.session.commit()
    except Exception as e:
        current_app.logger.error("{} update db commit exception: {}".format(
            Paper, e))
    return return_data('commit success', 200)
Example #30
0
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)