def post(id=None): _help = 'This field cannot be blank!' data = BodyParser.bodyParser([{ 'key': 'firstName', '_type': str, '_required': True, '_help': _help }, { 'key': 'lastName', '_type': str, '_required': True, '_help': _help }, { 'key': 'age', '_type': int, '_required': True, '_help': _help }, { 'key': 'email', '_type': str, '_required': True, '_help': _help }]) newUser = User(None, data['firstName'], data['lastName'], data['email'], data['age'], None) transaction = Transactions() userVerification = transaction.findUserByEmail(newUser.email) status = 400 message = 'User not created' if userVerification: message = 'User already exists with that email' return {'message': message, 'data': newUser.json()}, status userDAO = MYSQL_UserDAO() userCreated = userDAO.create(newUser) if userCreated: message = 'User created' status = 201 return {'message': message, 'data': userCreated.json()}, status
def findUserByEmail(self, email): conn = DBManager() cursor = conn.connection.cursor() query = 'SELECT idUser, name, age, lastName, password, email, latitude, longitude FROM User WHERE email = %s' cursor.execute(query, (email, )) userByEmail = cursor.fetchone() if userByEmail: user = User(userByEmail['idUser'], userByEmail['name'], userByEmail['lastName'], userByEmail['age'], userByEmail['email'], userByEmail['password'], userByEmail['latitude'], userByEmail['longitude']) return user return None
def readALL(self): conn = DBManager() cursor = conn.connection.cursor() query = 'SELECT idUser, name, lastName, age, email, latitude, longitude FROM User' cursor.execute(query) users = cursor.fetchall() if users: return [ User(user['idUser'], user['name'], user['lastName'], user['age'], user['email'], '', user['latitude'], user['longitude']).json() for user in users ] return []
def readAll(self): conn = DBManager() cursor = conn.connection.cursor() query = 'SELECT id, name, lastName, email, age FROM User' cursor.execute(query) users = cursor.fetchall() if users: return [ User(user['id'], user['name'], user['lastName'], user['email'], user['age'], None).json() for user in users ] return []
def read(self, _id): user = None if _id: conn = DBManager() cursor = conn.connection.cursor() query = 'SELECT id, name, lastName, email, age FROM User WHERE id = %s' cursor.execute(query, (_id, )) firstUser = cursor.fetchone() if firstUser: user = User(firstUser['id'], firstUser['name'], firstUser['lastName'], firstUser['email'], firstUser['age'], None) return user
def read(self, _id): if _id: conn = DBManager() cursor = conn.connection.cursor() query = 'SELECT idUser, name, lastName, age, email, latitude, longitude FROM User WHERE idUser = %s' cursor.execute(query, (_id, )) firstUser = cursor.fetchone() if firstUser: user = User(firstUser['idUser'], firstUser['name'], firstUser['lastName'], firstUser['age'], firstUser['email'], '', firstUser['latitude'], firstUser['longitude']) return user return firstUser return None
def put(self, id=None): _help = 'This field cannot be blank!' data = BodyParser.bodyParser([{ 'key': 'id', '_type': int, '_required': True, '_help': _help }, { 'key': 'firstName', '_type': str, '_required': True, '_help': _help }, { 'key': 'lastName', '_type': str, '_required': True, '_help': _help }, { 'key': 'age', '_type': int, '_required': True, '_help': _help }, { 'key': 'email', '_type': str, '_required': True, '_help': _help }]) userToUpdate = User(data['id'], data['firstName'], data['lastName'], data['email'], data['age'], None) userDAO = MYSQL_UserDAO() status = 400 if userDAO.update(userToUpdate): message = 'User updated' status = 201 else: message = 'Not allowed' status = 405 return {'message': message, 'data': data}, status
def post(self, id=None): _help = 'This field cannot be blank!' data = BodyParser.bodyParser([ { 'key': 'name', '_type': str, '_required': True, '_help': _help }, { 'key': 'lastName', '_type': str, '_required': True, '_help': _help }, { 'key': 'age', '_type': int, '_required': True, '_help': _help }, { 'key': 'password', '_type': str, '_required': True, '_help': _help }, { 'key': 'email', '_type': str, '_required': True, '_help': _help }, { 'key': 'latitude', '_type': float, '_required': True, '_help': _help }, { 'key': 'longitude', '_type': float, '_required': True, '_help': _help }, ]) encryptedPassword = hashlib.sha224( data['password'].encode('utf-8')).hexdigest() newUser = User(None, data['name'], data['lastName'], data['age'], data['email'], encryptedPassword, data['latitude'], data['longitude']) userDAO = UserDAO() transaction = Transactions() userVerification = transaction.findUserByEmail(newUser.email) status = 400 message = 'User not created' if userVerification: message = 'User already exists with that email' return { 'message': message, 'data': { 'name': userVerification.name, 'lastName': userVerification.lastName, 'email': userVerification.email } }, status if userDAO.create(newUser): message = 'User created' status = 201 return {'message': message, 'data': data}, status
def put(self, id=None): _help = 'This field cannot be blank!' data = BodyParser.bodyParser([{ 'key': 'id', '_type': int, '_required': True, '_help': _help }, { 'key': 'name', '_type': str, '_required': True, '_help': _help }, { 'key': 'lastName', '_type': str, '_required': True, '_help': _help }, { 'key': 'email', '_type': str, '_required': True, '_help': _help }, { 'key': 'age', '_type': int, '_required': True, '_help': _help }, { 'key': 'latitude', '_type': float, '_required': True, '_help': _help }, { 'key': 'longitude', '_type': float, '_required': True, '_help': _help }, { 'key': 'oldPassword', '_type': str, '_required': True, '_help': _help }, { 'key': 'newPassword', '_type': str, '_required': True, '_help': _help }]) transaction = Transactions() if transaction.checkOldPassword(current_identity.id, data['oldPassword']): message = 'Not allowed' status = 405 if current_identity.id == data['id']: userToUpdate = User(data['id'], data['name'], data['lastName'], data['age'], data['email'], data['newPassword'], data['latitude'], data['longitude']) userDAO = UserDAO() status = 400 if userDAO.update(userToUpdate): message = 'User updated' status = 201 else: message = 'Invalid password' status = 403 del data['newPassword'] del data['oldPassword'] return {'message': message, 'data': data}, status