Пример #1
0
    def post(self):
        # 获取用户传过来的数据
        data = request.json

        # 验证参数有效性
        # RequestParse: 验证参数类型 => 弱
        # wtforms:更灵活,参数类型、参数值... => 推荐!!
        # 构建表单 => 设置参数的要求 => data与表单绑定 => 验证数据有效性
        form = RegisterForm(data=data)
        if form.validate():

            # 注意: form.email.data
            UserProfile.create_user(
                user_profile_email=form.email.data,
                user_profile_name=form.username.data,
                password=form.password.data,
                user_profile_mobile=form.phone.data,
                user_height=form.height.data,
                user_weight=form.weight.data,
                user_age=form.age.data,
            )
            user = UserProfile.query.filter_by(
                user_profile_email=data.get("email")).first()
            result = user_schema.dump(user)
            # 返回结果
            return generate_response(data=result)
        else:
            result = form.errors
            raise FormValidateException(message=result)
Пример #2
0
    def post(self):
        # 获取用户传过来的数据
        data = request.json

        # 验证参数有效性
        form = UserForm(data=data)
        if form.validate():
            # # 创建用户
            # user = UserProfile(user_profile_email=data.get("email"),
            #                    user_profile_name=data.get("name"),
            #                    password=data.get("password"))
            # db.session.add(user)
            # db.session.commit()
            # result = user_schema.dump(user)
            UserProfile.create_user(user_profile_email=form.email.data,
                                    user_profile_name=form.name.data,
                                    password=form.password.data)
            user = UserProfile.query.filter_by(user_profile_email=data.get("email")).first()
            result = user_schema.dump(user)

            # 返回结果
            return generate_response(data=result)
        else:
            result = form.errors
            raise FormValidateException(message=result)
Пример #3
0
 def post(self):
     # 获取用户传过来的参数(api的参数为json数据)
     data = request.json
     # 检查参数的合法性(RequestParser/WTForms)
     form = RegisterForm(data=data)
     # 如果合法,创建
     if form.validate():
         # 创建用户UserProfile.create_user()
         user = UserProfile.create_user(user_profile_email=form.email.data,
                                 user_profile_name=form.name.data,
                                 password=form.password.data)
         return generate_response(data=user_schema.dump(user))
     else:
         raise ArgsTypeException(message=form.errors)