def post(self):
"""Create a new user.
If a user already exists with the provided
username, an HTTP 400 is sent.
Returns:
dict: If the user was successfully created,
return a dictionary describing that
created user.
None: If aborted.
"""
json_data = get_valid_json(self.SCHEMA_POST)
bio = json_data.get('bio')
username = json_data['username']
password = json_data['password']
new_user = models.User(username, password, bio=bio)
db.session.add(new_user)
try:
db.session.commit()
except sqlalchemy.exc.IntegrityError:
message = "A user already exists with username: %s" % username
flask_restful.abort(400, message=message)
else:
# will happen only if successful/no
# error raised
return new_user.to_dict()
评论列表
文章目录