user.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:ecommerce_api 作者: nbschool 项目源码 文件源码
def post(self):
        """ Add an user to the database."""
        data = request.get_json(force=True)

        errors = User.validate_input(data)
        if errors:
            return errors, BAD_REQUEST

        # Extract the user attributes to check and generate the User row
        data = data['data']['attributes']

        # If email is present in the database return a BAD_REQUEST response.
        if User.exists(data['email']):
            msg = {'message': 'email already present.'}
            return msg, CONFLICT

        new_user = User.create(
            uuid=uuid.uuid4(),
            first_name=data['first_name'],
            last_name=data['last_name'],
            email=data['email'],
            password=User.hash_password(data['password'])
        )
        notify_new_user(first_name=new_user.first_name,
                        last_name=new_user.last_name)

        # If everything went OK return the newly created user and CREATED code
        # TODO: Handle json() return value (data, errors) and handle errors not
        # empty
        return generate_response(new_user.json(), CREATED)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号