Cleaned up and easier debugging for SQL statements. A lot less redundant information.

This commit is contained in:
Scott Idem
2021-09-07 14:01:40 -04:00
parent 64b77ec238
commit af0da563f1
5 changed files with 241 additions and 56 deletions

View File

@@ -70,7 +70,7 @@ async def post_event_session_obj_new_v4(
return mk_resp(data=data, response=response, status_message='The event session was created.')
else:
return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to create an event session was unexpected.')
# ### BEGIN ### API Event Session ### post_event_session_obj_new_v4() ###
# ### END ### API Event Session ### post_event_session_obj_new_v4() ###
# ### BEGIN ### API Event Session ### patch_event_session_obj_exist_v4() ###

View File

@@ -185,6 +185,10 @@ async def upload_files_fake(
hosted_file_list = []
for file_info in file_info_li:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(file_info)
# "saved" means that the file was or is now saved on the file server
if file_info['saved']:
# Create a new host_file object entry
log.info('Check and create a new host_file object entry...')
@@ -199,7 +203,8 @@ async def upload_files_fake(
field_value = file_info['hash_sha256'],
):
# NOTE: Since the file already exists and something was in the database, it may need to be updated with the new subdirectory_path.
hosted_file_id = hosted_file_sel_result.get('id_random', None)
hosted_file_id = hosted_file_sel_result.get('id', None)
hosted_file_id_random = hosted_file_sel_result.get('id_random', None)
hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True)
# ******************************************************
@@ -208,21 +213,49 @@ async def upload_files_fake(
# NOTE: Working on moving all hosted files to subdirectories because there are a lot of files. The database needs to be updated if the file already exists and it does not exist in the new subdirectory.
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if file_info['already_exists'] and not file_info['already_exists_subdir']: # This means the database record probably needs to be updated with the new subdirectory_path field.
subdirectory_path_from_passed_data = file_info['subdirectory_path']
log.info(f'The database record probably needs to be updated with the new subdirectory_path field. Subdirectory Path (from passed data): {subdirectory_path_from_passed_data}')
log.debug(file_info['already_exists'])
log.debug(file_info['already_exists_subdir'])
log.debug(file_info['subdirectory_path'])
# if not hosted_file_dict.get('subdirectory_path', None): # This means the database record probably needs to be updated with the new subdirectory_path field.
# subdirectory_path = file_info['subdirectory_path']
# log.info(f'The database record probably needs to be updated with the new subdirectory_path field. Subdirectory Path (from passed data): {subdirectory_path}')
# hosted_file_data_up = {}
# hosted_file_data_up['id'] = hosted_file_id
# hosted_file_data_up['subdirectory_path'] = subdirectory_path
# if hosted_file_up_result := sql_update(
# table_name = 'hosted_file',
# data = hosted_file_data_up,
# ): log.info(f'The hosted_file record has been updated with the new subdirectory_path. Hosted File ID: {hosted_file_id} Subdirectory Path: {subdirectory_path}')
# else:
# log.warning(f'The hosted_file record was probably not updated with the new subdirectory_path. Hosted File ID: {hosted_file_id} Subdirectory Path: {subdirectory_path}')
# log.debug(hosted_file_up_result)
if subdirectory_path := hosted_file_dict.get('subdirectory_path'):
log.info(f'The new subdirectory_path field was found? Subdirectory Path: {subdirectory_path}')
else:
log.info(f'The new subdirectory_path field was not found? This needs to be updated. Subdirectory Path: {subdirectory_path}')
log.info(f'The new subdirectory_path field was found in the database record? Subdirectory Path: {subdirectory_path}')
elif subdirectory_path := file_info.get('subdirectory_path', None):
log.info(f'The new subdirectory_path field was not found in the database record. This needs to be updated. Subdirectory Path: {subdirectory_path}')
hosted_file_data_up = {}
hosted_file_data_up['id'] = hosted_file_id
hosted_file_data_up['subdirectory_path'] = subdirectory_path
if hosted_file_up_result := sql_update(
table_name = 'hosted_file',
data = hosted_file_data_up,
): log.info(f'The hosted_file record has been updated with the new subdirectory_path. Hosted File ID: {hosted_file_id} Subdirectory Path: {subdirectory_path}')
else:
log.warning(f'The hosted_file record was probably not updated with the new subdirectory_path. Hosted File ID: {hosted_file_id} Subdirectory Path: {subdirectory_path}')
log.debug(hosted_file_up_result)
else:
log.warning(f'The new subdirectory_path field was not found in the database record or the passed file info.')
# ******************************************************
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(hosted_file_dict)
else:
# SOMETHING WENT WRONG
# NOTE: SOMETHING WENT WRONG
# Going to try and create a new host_file entry...
log.warning('For some reason a host_file object entry with the has was not found.')
# file_info['id_random'] = None
@@ -248,8 +281,42 @@ async def upload_files_fake(
# Got existing host_file object_entry!
# Odd... the hash was found in the database, but the file had to be copied again.
# If this happens then the file on the host server was probably deleted at some point.
hosted_file_id = hosted_file_sel_result.get('id_random', None)
hosted_file_id = hosted_file_sel_result.get('id', None)
hosted_file_id_random = hosted_file_sel_result.get('id_random', None)
hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True)
# ******************************************************
# New as of 2021-08-26
# NOTE: Working on moving all hosted files to subdirectories because there are a lot of files. The database needs to be updated if the file already exists and it does not exist in the new subdirectory.
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(file_info['already_exists'])
log.debug(file_info['already_exists_subdir'])
log.debug(file_info['subdirectory_path'])
if subdirectory_path := hosted_file_dict.get('subdirectory_path'):
log.info(f'The new subdirectory_path field was found in the database record? Subdirectory Path: {subdirectory_path}')
elif subdirectory_path := file_info.get('subdirectory_path', None):
log.info(f'The new subdirectory_path field was not found in the database record. This needs to be updated. Subdirectory Path: {subdirectory_path}')
hosted_file_data_up = {}
hosted_file_data_up['id'] = hosted_file_id
hosted_file_data_up['subdirectory_path'] = subdirectory_path
if hosted_file_up_result := sql_update(
table_name = 'hosted_file',
data = hosted_file_data_up,
): log.info(f'The hosted_file record has been updated with the new subdirectory_path. Hosted File ID: {hosted_file_id} Subdirectory Path: {subdirectory_path}')
else:
log.warning(f'The hosted_file record was probably not updated with the new subdirectory_path. Hosted File ID: {hosted_file_id} Subdirectory Path: {subdirectory_path}')
log.debug(hosted_file_up_result)
else:
log.warning(f'The new subdirectory_path field was not found in the database record or the passed file info.')
# ******************************************************
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(hosted_file_dict)
else:
# This is normal since the file was not found on the host server and not found in the DB.
# Create a new host_file object entry and new host_file.id_random.
@@ -264,7 +331,7 @@ async def upload_files_fake(
hosted_file_dict = hosted_file_obj.dict(by_alias=True, exclude_unset=True, exclude={'id', 'id_random'}) # pylint: disable=no-member
log.debug(hosted_file_obj_result)
log.debug(hosted_file_sel_result)
else:
else: # The file was not and is not saved on the file server
file_info['id_random'] = None
hosted_file_obj = Hosted_File_Base(**file_info)
hosted_file_id = None