Ejemplo n.º 1
0
def create_bucket(bucket_name, region=None):
	"""Create an S3 bucket in a specified region

	If a region is not specified, the bucket is created in the S3 default
	region (us-east-1).

	:param bucket_name: Bucket to create
	:param region: String region to create bucket in, e.g., 'us-west-2'
	:return: True if bucket created, else False
	"""

	# Create bucket
	try:
		if region is None:
			s3_client = boto3.client('s3')
			s3_client.create_bucket(Bucket=bucket_name)
		else:
			s3_client = boto3.client('s3', region_name=region)
			location = {'LocationConstraint': region}
			s3_client.create_bucket(Bucket=bucket_name,
									CreateBucketConfiguration=location)
	except ClientError as e:
		logger.error(e)
		return False
	return True
Ejemplo n.º 2
0
def check_database(conn):
    '''
    find total number of rows in 'sales_records.sales' and return ``int``

    Return:
        total_rows of mysql database
    '''
    # conn=MySqlHook(mysql_conn_id='mysql_localhost').get_conn()
    cur = conn.cursor()  #mysql conn
    try:
        cur.execute('use sales_records')
        cur.execute('select count(*) from sales')
        total_rows = cur.fetchone()[0]
        # task_instance.xcom_push(key='mysql_total_rows', value=total_rows)
        if type(total_rows) is int:
            print('appending new data')
            return total_rows
        # elif total_rows==50000:
        #     print('up to date')
        #     return 'check_dataset'
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_NO_DB_ERROR:
            print('no database found')
            logger.error(err)
            total_rows = None
        else:
            print('something wrong with database')
            logger.error(err)
            total_rows = None
    return total_rows
Ejemplo n.º 3
0
    def post(self):
        data = request.json
        logger.info(f'Register Post data {data} [{type(data)}]')
        if data is None:
            return {'status': 'fail', 'message': 'No data passed'}, 400

        try:
            user = User.query.filter(
                or_(User.email == data.get('email'),
                    User.username == data.get('username'))).first()
            if user:
                logger.info(f"Resister found pre-existing User: {user}")
                return {
                    'status': 'fail',
                    'message': 'Username/Email Already exist!'
                }, 401
            user = User(
                email=data.get('email'),
                username=data.get('username'),
                password=data.get('password'),
            )
            db.session.add(user)
            db.session.commit()
            auth_token = user.encode_auth_token(user.id)
            return {
                'status': 'success',
                'message': 'Successfully registered',
                'auth_token': auth_token.decode()
            }, 201
        except Exception as e:
            logger.error(e)
            return {
                'status': 'fail',
                'message': 'An error has occurred',
            }, 401
Ejemplo n.º 4
0
 def post(self):
     # def blacklist_token(auth_token):
     # 	blacklisted_token = BlacklistToken(token=auth_token)
     # 	try:
     # 		db.session.add(blacklisted_token)
     # 		db.session.commit()
     # 		return {
     # 			       'status': 'success',
     # 			       'message': 'Successfully logged out'
     # 		       }, 200
     # 	except Exception as e:
     # 		logger.error(e)
     # 		return {
     # 			'status': 'fail',
     # 			'message': e
     # 		}
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_token = auth_header.split(" ")[1]
         resp = User.decode_auth_token(auth_token)
         if isinstance(resp, str):
             return {
                 'status': 'success',
                 'message': resp
             }, 201  # todo changed from 401
         else:
             # return blacklist_token(auth_token)
             blacklisted_token = BlacklistToken(token=auth_token)
             try:
                 db.session.add(blacklisted_token)
                 db.session.commit()
                 return {
                     'status': 'success',
                     'message': 'Successfully logged out'
                 }, 200
             except Exception as e:
                 logger.error(e)
                 return {'status': 'fail', 'message': e}
     else:
         # return blacklist_token(auth_token)
         return {
             'status': 'success',
             'message': 'Invalid auth provided'
         }, 201  # todo changed from 401
Ejemplo n.º 5
0
	def get(self):
		data = request.args
		logger.info(f"Post get data[{type(data)}]: {data}")
		try:
			if 'id' in data:
				logger.info('REQUEST BY ID')
				posts = [dbPost.query.get(data['id']),]
			elif 'feed' in data:
				feed_query = json.loads(data['feed'])
				posts = [post for _, post in dbUserFollow.query.filter(
					dbUserFollow.follower_id == feed_query['userId']
				).join(
					dbPost, dbUserFollow.followed_id == dbPost.author_id
				).add_entity(
					dbPost
				).all()]
			else:
				posts = dbPost.query.join(
					dbUser, dbUser.id == dbPost.author_id
				).filter_by(
					# **{'id': data.get('id')}
				).add_entity(
					dbUser
				)
				if data:
					posts = posts.filter_by(
						**{'id': data.get('author_id')}
					)
				posts = [post for post, user in posts.all()]
			posts = [post.resp_dict() for post in posts]
			return {
				'status': 'success',
				'message': 'post retrieved',
				'posts': posts
			}, 200
		except Exception as e:
			logger.error(e)
			return {
					'status': 'fail',
					'message': 'An error has occurred',
				}, 401
Ejemplo n.º 6
0
 def authenticate(*args, **kwargs):
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_token = auth_header.split(" ")[1]
         decode_response = User.decode_auth_token(auth_token)
         if isinstance(decode_response, str):
             return {
                 'status': 'fail',
                 'message': f" decode error: {decode_response}"
             }, 401
         else:
             try:
                 return func(*args, **kwargs)
             except Exception as e:
                 logger.error(e)
                 return {
                     'status': 'fail',
                     'message': f'An error has occurred: {e}'
                 }, 401
     else:
         return {'status': 'fail', 'message': 'Invalid auth provided'}, 401
Ejemplo n.º 7
0
    def __init__(self, USER, HOST, DATABASE, PASSWORD):

        self.USER = USER
        self.HOST = HOST
        self.DATABASE = DATABASE
        self.PASSWORD = PASSWORD

        try:
            conn = mysql.connector.connect(
                user=USER,
                host=HOST,
                database=DATABASE,
                password=PASSWORD,
            )
            self.conn = conn
            logger.info('connection database success')
            print('Connection success')

        except mysql.connector.Error as err:

            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                logger.error('umm something wrong with user name or password')
                print('umm something wrong with user name or password')

            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                logger.error('database does not exist')
                print('database does not exist')

            else:
                logger.error(err)
                print(err)
Ejemplo n.º 8
0
 def post(self):
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_token = auth_header.split(" ")[1]
         resp = User.decode_auth_token(auth_token)
         if isinstance(resp, str):
             return {'status': 'fail', 'message': resp}, 401
         else:
             data = request.json
             logger.info(f"Comment post data[{type(data)}]: {data}")
             if data is None:
                 return {'status': 'fail', 'message': 'No data passed'}, 400
             else:
                 try:
                     post_id = data['postId']
                     author_id = data['authorId']
                     comment = data['comment']
                     post = dbPost.query.filter(
                         dbPost.id == post_id).first()
                     new_comment = dbComment(post_id=post_id,
                                             author_id=author_id,
                                             body=comment)
                     post.comments.append(new_comment)
                     db.session.add(new_comment)
                     db.session.add(post)
                     db.session.commit()
                     return {
                         'status': 'success',
                         'message': 'comment submitted',
                     }, 200
                 except Exception as e:
                     logger.error(e)
                     return {
                         'status': 'fail',
                         'message': 'An error has occurred',
                     }, 401
     else:
         return {'status': 'fail', 'message': 'Invalid auth provided'}, 401
Ejemplo n.º 9
0
    def post(self):
        data = request.json
        logger.info(f'Login Post data {data} [{type(data)}]')
        if data is None:
            return {'status': 'fail', 'message': 'No data passed'}, 400

        try:
            # user = User.query.filter_by(
            # 	email=data.get('email'),
            # 	username=data.get('username')
            # ).first()
            user = User.query.filter(
                (User.username == data.get('username'))
                | (User.email == data.get('email'))).first()
            logger.info(f'Login Post user query result: {user}')
            if user and bcrypt.check_password_hash(user.password,
                                                   data.get('password')):
                auth_token = user.encode_auth_token(user.id)
                if auth_token:
                    return {
                        'status': 'success',
                        'message': 'Success',
                        'user': user.resp_dict(include_private=True),
                        # 'user': user.resp_dict(),
                        'auth_token': auth_token.decode()
                    }, 200
            else:
                return {
                    'status': 'fail',
                    'message': 'Username or Password are invalid!'
                }, 401
        except Exception as e:
            logger.error(e)
            return {
                'status': 'fail',
                'message': 'An error has occurred',
            }, 401
Ejemplo n.º 10
0
	def get(self):
		authenticated = check_auth(request)
		try:
			data = dict(request.args)
			logger.info(f"User get data[{type(data)}]: {data}")
			user = dbUser.query.filter_by(**data).first()
			logger.warning(f'authenticated: {authenticated}')
			if isinstance(authenticated, int):
				same_user = authenticated == user.id
				return {
					'status': 'success',
					'user': user.resp_dict(include_private=same_user),
				}, 200
			else:
				return {
					'status': 'success',
					'user': user.resp_dict()
				}, 200
		except Exception as e:
			logger.error(e)
			return {
				'status': 'fail',
				'message': 'An error has occurred',
			}, 401