Work on simplifying functions. ID lookups are better. Person and User related is being worked on.
This commit is contained in:
@@ -844,14 +844,33 @@ def sql_delete(
|
||||
def redis_lookup_id_random(
|
||||
record_id_random: int|str,
|
||||
table_name: str,
|
||||
):
|
||||
check_int_id: bool = False,
|
||||
) -> str|int|bool|None:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if isinstance(record_id_random, str) and len(record_id_random) >= 11 and len(record_id_random) <= 22: pass
|
||||
elif isinstance(record_id_random, int): return record_id_random
|
||||
elif isinstance(record_id_random, int):
|
||||
record_id = record_id_random
|
||||
if check_int_id:
|
||||
log.info(f'Checking the int ID if exists. Table Name: {table_name} ID: {record_id}')
|
||||
if get_id_random_result := get_id_random(
|
||||
record_id = record_id,
|
||||
table_name = table_name,
|
||||
):
|
||||
log.info(f'The int ID exists. Returning the int ID. ID Random: {get_id_random_result}')
|
||||
return record_id
|
||||
else:
|
||||
log.info(f'The int ID does not exists. Returning False. Table Name: {table_name} ID: {record_id}')
|
||||
return False
|
||||
else:
|
||||
log.info(f'Not checking if the int ID exists. Returning the int ID. ID: {record_id}')
|
||||
return record_id
|
||||
elif record_id_random is None:
|
||||
log.info(f'No record ID was passed. Returning None')
|
||||
return None
|
||||
else:
|
||||
log.info(f'Unexpected data type or string format: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.')
|
||||
log.warning(f'Unexpected data type or string format: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.')
|
||||
return False
|
||||
|
||||
if record_id_random and table_name:
|
||||
@@ -920,13 +939,12 @@ def redis_lookup_id_random(
|
||||
|
||||
|
||||
# ### BEGIN ### API DB SQL ### get_id_random() ###
|
||||
# Changed name from lookup_id_random() to get_id_random()
|
||||
# Updated 2021-08-23
|
||||
# Updated 2022-01-06
|
||||
@logger_reset
|
||||
def get_id_random(
|
||||
record_id: int,
|
||||
table_name: str
|
||||
):
|
||||
) -> str|bool|None:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -938,7 +956,6 @@ def get_id_random(
|
||||
"""
|
||||
|
||||
if select_results := sql_select(sql=sql, data=data):
|
||||
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(select_results)
|
||||
log.debug(type(select_results))
|
||||
if isinstance(select_results, dict):
|
||||
@@ -946,18 +963,24 @@ def get_id_random(
|
||||
if record_id_random := select_results.get('id_random'):
|
||||
return str(record_id_random)
|
||||
else:
|
||||
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.error('The SQL result was not what was expected.')
|
||||
return False
|
||||
else:
|
||||
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.error('More than one record may have been found. There may be a duplicate id.')
|
||||
elif isinstance(select_results, list):
|
||||
log.exception('More than one record may have been found. There may be a duplicate id. This should never happen.')
|
||||
log.error(select_results)
|
||||
return False
|
||||
else:
|
||||
#log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.info('Record ID random was not found')
|
||||
else:
|
||||
log.exception(f'Got an unexpected result while trying to look up the ID. Is the table name correct? {table_name} Is the record ID valid? {record_id}')
|
||||
log.error(select_results)
|
||||
return False
|
||||
elif select_results is None:
|
||||
log.warning(f'No results with: Table Name: {table_name} ID: {record_id}')
|
||||
log.debug(select_results)
|
||||
return None
|
||||
else: # False or something else not True
|
||||
log.error(f'Something went wrong while trying to look up the ID. Is the table name correct? {table_name} Is the record ID valid? {record_id}')
|
||||
log.error(select_results)
|
||||
return False
|
||||
# ### END ### API DB SQL ### get_id_random() ###
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user