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 get_user(id):
    try:
        user = com_get(User, id=id)
    except Exception as e:
        current_app.logger.error("[user][get] fail expection: {}".format(e))
        raise InvalidMessage(str(e), 500)
    data = user_helper.make_user_reponse_body(user)
    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_users():
    try:
        users = com_gets(User)
    except Exception as e:
        current_app.logger.error("[user][gets] fail expection: {}".format(e))
        raise InvalidMessage(str(e), 500)
    datas = []
    for user in users:
        data = user_helper.make_user_reponse_body(user)
        datas.append(data)
    return return_data(datas, 200)
def make_paper_question_reponse_body(paper_question):
    data = {
        'paper_question': {
            'id': paper_question.id,
            'status': paper_question.status,
            'question_score': paper_question.question_score,
            'user_paper_id': paper_question.user_paper_id,
            'user_answer': paper_question.user_answer
        },
        'question': {},
        'user': {}
    }
    question = paper_question.question
    user = paper_question.user_paper.user
    if question:
        data['question'] = make_question_reponse_body(question)
    if user:
        data['user'] = make_user_reponse_body(user)
    return data