def read_b_row_and_check_if_same_user(connection, rows): for i in range(0, len(rows)): first_value = str(rows[i][0]) user_id = str(rows[i][2]) if get_test_id(connection, first_value) == 1: if get_user_id(connection, user_id) == 0: #app.logger.info("user id doesn't match") raise UploadProcessException( "user id doesn't match. You are not authorized to make these changes." )
def execute_oracle_queries(connection, sql_statements): try: with connection.cursor() as cursor: if isinstance(sql_statements, list): for sql_statement in sql_statements: cursor.execute(sql_statement) else: cursor.execute(sql_statements) connection.commit() except cx_Oracle.Error as e: raise UploadProcessException('Could not save data!: {0} -> {1}'.format( e, sql_statement)) from e
def get_unique_id(connection): """Getting unique primary id from SQL""" sql_statement = 'select {0}.shared_sequence.nextval from dual'.format( db_schema) try: # create a cursor with connection.cursor() as cursor: cursor.execute(sql_statement) return int(cursor.fetchone()[0]) except cx_Oracle.Error as e: raise UploadProcessException( "Could not allocate sequence ID: {0}".format(e)) from e
def upload_file_to_s3_bucket(logger, file_name, file_id, parent_id): logger.info('Starting upload of {0} attached to #{1}...'.format(file_name, parent_id)) name, ext = os.path.splitext(os.path.basename(file_name)) object_name = '{0}/{1}{2}'.format(parent_id, name, ext) if parent_id else 'json/{0}{1}'.format(name, ext) try: # Insert a tracking record in our database to track the async cloud upload process. with oracle_db.get_connection() as connection: if not file_id: file_id = oracle_db.get_unique_id(connection) oracle_db.upsert_cloud_upload_status(connection, file_id, parent_id or 0, os.path.basename(file_name), object_name, 'Pending') # Start the actual upload process asynchronously in a separate thread. thread_upload = threading.Thread(target=_async_upload_function, args=(logger, file_name, file_id, parent_id or 0, object_name)) thread_upload.start() except Exception as e: raise UploadProcessException('Cannot upload file to S3: {0}'.format(e)) from e
def get_test_id(connection, test_id): sql_statement = "select B_TESTS_ID from {0}.B_TESTS where\"Test ID\"='{1}'".format( db_schema, test_id) try: # create a cursor with connection.cursor() as cursor: cursor.execute(sql_statement) while True: rows = cursor.fetchone() if not rows: return 0 else: return 1 except cx_Oracle.Error as e: raise UploadProcessException( "Could not allocate sequence ID: {0}".format(e)) from e
def get_crd_id(connection, crd): """Getting unique primary id from SQL""" sql_statement = "select A_CCSD_ID from {0}.A_CCSD where \"CRD-revision-side ID\"=\'{1}\'".format( db_schema, crd) try: # create a cursor with connection.cursor() as cursor: cursor.execute(sql_statement) while True: rows = cursor.fetchone() resp = 0 if not rows: resp = 0 else: resp = int(rows[0]) # app.logger.info('executed ' + sql_statement + 'response> ' + str(resp)) return resp except cx_Oracle.Error as e: raise UploadProcessException( "Could not find CCSD ID: {0}".format(e)) from e