コード例 #1
0
    def post(self, **kwargs):
        """Create a user"""
        body = request.get_json()

        # check user doesn't exist
        if db.session.query(User.id).filter_by(
                username=body["username"]).scalar() is not None:
            current_app.logger.info(f"user {body['username']} exists")
            raise ValidationError("Sorry, that username is already taken!")

        # create user
        new_user = User(
            username=body["username"],
            email=body["email"],
            age=body.get("age"),
            city=body.get("city"),
        )
        new_user.save()

        current_app.logger.info(f"created user {new_user.username}")

        return new_user.asdict(), 201