Exemplo n.º 1
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)
Exemplo n.º 2
0
def update_paper(id):
    try:
        paper = com_put(db, Paper, **{'id': id})
    except Exception as e:
        current_app.logger.error("[paper][put] fail expection: {}".format(e))
    paper_helper.compute_score(paper.head)
    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)
Exemplo n.º 3
0
def update_question():
    questions = None
    try:
        questions = com_puts(db, Question)
    except Exception as e:
        current_app.logger.error("[questions][put] fail expection: {}".format(e))
    for question in questions:
        paper_helper.compute_score(question.paper.head)
    try:
        # 同步数据到数据库
        db.session.commit()
    except Exception as e:
        current_app.logger.error("{} model update exception: {}".format(Question, e))
        raise e
    return return_data('update success', 200)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def create_question():
    try:
        questions = com_posts(db, Question)
    except Exception as e:
        current_app.logger.error("[questions][post] fail expection: {}".format(e))
        return InvalidMessage(str(e), 500)
    for question in questions:
        paper_helper.compute_score(question.paper.head)
    try:
        # 同步数据到数据库
        db.session.commit()
    except Exception as e:
        current_app.logger.error("{} model init exception: {}".format(Question, e))
        raise e
    datas = []
    for question in questions:
        data_obj = model_helper.obj_to_dict(question)
        datas.append(data_obj)
    return return_data(datas, 201)
Exemplo n.º 6
0
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)