def post(self): post_data = request.get_json() response_object = {'status': 'fail', 'message': 'Invalid payload.'} if not post_data: return response_object, 400 username = post_data.get('username') email = post_data.get('email') try: # open connection to the database db = database.get_mongoDB() user = db.user.find_one({"email": email}) if not user: newUser = User(username=username, email=email) newUser.save() response_object = { 'status': 'success', 'message': f'{email} was added!' } return response_object, 200 else: response_object[ 'message'] = 'Sorry. That email already exists.' return response_object, 400 except ValidationError: # if the write fails, OperationalError is thrown return response_object, 400
def seed_db(): """Seeds the database.""" from project.db import database from project.api.models import User # get mongoDB connection and add two users db = database.get_mongoDB() userOne = User(username='******', email="*****@*****.**") userOne.save() userTwo = User(username='******', email="*****@*****.**") userTwo.save()
def add_user(username, email): db = database.get_mongoDB() user = User(username=username, email=email) user.save() return user