def create_table(): if not User.exists(): app.logger.debug('Creating DynamoDB User table..') User.create_table(read_capacity_units=app.config['DDB_RCU'], write_capacity_units=app.config['DDB_WCU'], wait=True) if not Photo.exists(): app.logger.debug('Creating DynamoDB Photo table..') Photo.create_table(read_capacity_units=app.config['DDB_RCU'], write_capacity_units=app.config['DDB_WCU'], wait=True)
def get(self, user_id): """Get a single user details""" try: user = User.get(hash_key=user_id) if user is None: app.logger.error('ERROR:user_id not exist:{}'.format(user_id)) return err_response({'user_id': user_id}, 404) data = { 'user': { 'id': user.id, 'username': user.username, 'email': user.email } } app.logger.debug("success:user_get_by_id:%s" % data['user']) return m_response(data, 200) except ValueError as e: app.logger.error("ERROR:user_get_by_id:{}".format(user_id)) app.logger.error(e) return err_response("ERROR:user_get_by_id:{}".format(user_id), 500) except Exception as e: app.logger.error("ERROR:user_get_by_id:{}".format(user_id)) app.logger.error(e) return err_response("ERROR:user_get_by_id:{}".format(user_id), 500)
def setUp(self): # Delete any test data that may be remained. for item in User.scan(User.username.startswith('test')): item.delete() for item in Photo.scan(Photo.filename_orig.startswith('test')): item.delete() # Create test user test_user = User(uuid.uuid4().hex) test_user.email = user['email'] test_user.username = user['username'] test_user.password = generate_password_hash(user['password']) test_user.save()
def seed_db(): """Seeds the database.""" try: # Insert test user test_user = User(uuid.uuid4().hex) test_user.email = user['email'] test_user.username = user['username'] test_user.password = generate_password_hash(user['password']) test_user.save() except Exception as e: app.logger.error(e) print(user)
def solution_put_new_user(new_user_id, user_data): app.logger.info("RUNNING TODO#1 SOLUTION CODE:") app.logger.info("Put new signup user information!") app.logger.info( "Follow the steps in the lab guide to replace this method with your own implementation." ) user = User(new_user_id) user.email = user_data['email'] user.password = generate_password_hash(user_data['password']) user.username = user_data['username'] user.save()
def get(self): """Get all users as list""" try: data = [] for user in User.scan(): one_user = { 'id': user.id, 'email': user.email, 'username': user.username } data.append(one_user) app.logger.debug('success:users_list: {0}'.format(data)) return make_response({'ok': True, 'users': data}, 200) except Exception as e: app.logger.error("users list failed") app.logger.error(e) raise InternalServerError('Retrieve user list failed')
def get(self): """Get all users as list""" try: data = [] for user in User.scan(): one_user = { 'id': user.id, 'email': user.email, 'username': user.username } data.append(one_user) app.logger.debug("success:users_list:%s" % data) return m_response(data, 200) except Exception as e: app.logger.error("users list failed") app.logger.error(e) return err_response("users list failed", 500)
def get(self, user_id): """Get a single user details""" try: user = User.get(hash_key=user_id) if user is None: app.logger.error('ERROR:user_id not exist:{}'.format(user_id)) raise BadRequest('User not exist') user = { 'id': user.id, 'username': user.username, 'email': user.email } app.logger.debug('success:user_get_by_id: {0}'.format(user)) return make_response({'ok': True, 'users': user}, 200) except ValueError as e: app.logger.error('user_get_by_id:{0}, {1}'.format(user_id, e)) raise BadRequest(e) except Exception as e: app.logger.error('Unexpected Error: {0}, {1}'.format(user_id, e)) raise InternalServerError('Unexpected Error:{0}'.format(e))
def solution_put_new_user(new_user_id, user_data): user = User(new_user_id) user.email = user_data['email'] user.password = generate_password_hash(user_data['password']) user.username = user_data['username'] user.save()
def delete_table(): if User.exists(): User.delete_table() if Photo.exists(): Photo.delete_table()
def tearDown(self): # Delete test data for item in User.scan(User.username.startswith('test')): item.delete() for item in Photo.scan(Photo.filename_orig.startswith('test')): item.delete()
def delete_table(): User.delete_table() app.logger.debug("Dynamodb Users table deleted")
def create_table(): User.create_table() app.logger.debug("Dynamodb Users table created")
from cloudalbum.database.model_ddb import User from flask import current_app as app if not User.exists(): User.create_table(read_capacity_units=app.config['DDB_RCU'], write_capacity_units=app.config['DDB_WCU'], wait=True) app.logger.debug('DynamoDB User table created!') def create_table(): User.create_table() app.logger.debug("Dynamodb Users table created") def delete_table(): User.delete_table() app.logger.debug("Dynamodb Users table deleted")
def test_user_table(self): """Ensure the DDB User table is available.""" self.assertEqual(User.exists(), True)