July 2017
Intermediate to advanced
374 pages
8h
English
This API function adds new users to the users list.
In this code, we will rewrite the add_user(new_user) function to interact with MongoDB to add a user to the users collection:
def add_user(new_user):
api_list=[]
print (new_user)
db = connection.cloud_native.users
user = db.find({'$or':[{"username":new_user['username']} , {"email":new_user['email']}]})
for i in user:
print (str(i))
api_list.append(str(i))
if api_list == []:
db.insert(new_user)
return "Success"
else :
abort(409)
Now that we have modified our function, one more thing needs to be done--earlier, IDs were generated by SQLite 3, but now, we need to generate them with a random module by adding it to its route function, as follows:
def create_user(): if not ...
Read now
Unlock full access