Updating event device

This commit is contained in:
Scott Idem
2024-08-15 08:42:54 -04:00
parent 18293764fd
commit de35856749
2 changed files with 159 additions and 52 deletions

View File

@@ -271,6 +271,7 @@ async def get_obj_li_l3(
)
# Updated 2024-08-14
def handle_get_obj_li(
obj_type_l1: str,
obj_type_l2: Optional[str] = None,
@@ -328,6 +329,7 @@ def handle_get_obj_li(
log.info(jp_obj)
# Updated 2024-08-14
if jp_obj.get('qry'): # NOTE: This is for specific additional WHERE clauses in the SQL statement
# Example JSON:
# jp: {
@@ -436,27 +438,43 @@ def handle_get_obj_li(
log.info(f'obj_name: {obj_name}')
log.debug(obj_type_kv_li[obj_name])
if tbl_alt:
table_name = obj_type_kv_li[obj_name][f'tbl_{tbl_alt}']
# table_name = obj_type_kv_li[obj_name].get(f'tbl_{tbl_alt}')
log.info(f'tbl_alt was found. Using {table_name} table.')
# Updated 2024-08-14
# Check the table (view) name to make sure it is valid.
if tbl_alt: # and tbl_alt != 'default':
try:
table_name = obj_type_kv_li[obj_name][f'tbl_{tbl_alt}']
# table_name = obj_type_kv_li[obj_name].get(f'tbl_{tbl_alt}')
except:
table_name = obj_type_kv_li[obj_name]['tbl']
log.warning(f'tbl_alt ({tbl_alt}) was not found. Using {table_name} table.')
log.info(f'tbl_alt ({tbl_alt}) was found. Using {table_name} table.')
else:
table_name = obj_type_kv_li[obj_name]['tbl']
log.warning(f'tbl_alt ({tbl_alt}) was not found. Using {table_name} table.')
# Check the model name to make sure it is valid.
if mdl_alt:
base_name = obj_type_kv_li[obj_name][f'mdl_{mdl_alt}']
# base_name = obj_type_kv_li[obj_name].get(f'mdl_{mdl_alt}')
log.info(f'mdl_alt was found. Using {base_name} model.')
if exp_alt:
column_name_li = obj_type_kv_li[obj_name].get(f'exp_{exp_alt}')
log.info(f'exp_alt was found. Using {column_name_li} column list.')
try:
base_name = obj_type_kv_li[obj_name][f'mdl_{mdl_alt}']
# base_name = obj_type_kv_li[obj_name].get(f'mdl_{mdl_alt}')
except:
base_name = obj_type_kv_li[obj_name]['mdl']
log.info(f'mdl_alt ({mdl_alt}) was found. Using {base_name} model.')
else:
base_name = obj_type_kv_li[obj_name]['mdl']
log.warning(f'mdl_alt ({mdl_alt}) was not found. Using {base_name} model.')
# if use_alt_table:
# table_name = obj_type_kv_li[obj_name]['table_name_alt']
# If we are returning a file, check the column name list to make sure it is valid.
if return_file and exp_alt:
try:
column_name_li = obj_type_kv_li[obj_name][f'exp_{exp_alt}']
# column_name_li = obj_type_kv_li[obj_name].get(f'exp_{exp_alt}')
except:
column_name_li = obj_type_kv_li[obj_name]['exp']
log.info(f'exp_alt ({exp_alt}) was found. Using {column_name_li} column list.')
# else:
# table_name = obj_type_kv_li[obj_name]['table_name']
# if use_alt_base:
# base_name = obj_type_kv_li[obj_name]['base_name_alt']
# else:
# base_name = obj_type_kv_li[obj_name]['base_name']
# column_name_li = obj_type_kv_li[obj_name]['exp']
# log.warning(f'exp_alt ({exp_alt}) was not found. Using {column_name_li} column list.')
if for_obj_type and for_obj_id:
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type)
@@ -525,13 +543,15 @@ def handle_get_obj_li(
log.warning('base_name model was not found. Returning raw data.')
resp_data = record
resp_data_li.append(resp_data)
if not column_name_li:
column_name_li = list(sql_result[0].keys()) # This should be the same for all records in the list.
additional_column_name_li = []
if return_file:
log.setLevel(logging.INFO)
if not column_name_li:
column_name_li = list(sql_result[0].keys()) # This should be the same for all records in the list.
log.info(f'Column names: {column_name_li}')
additional_column_name_li = []
# We want to handle any field that has a suffix of _json. It should be expanded into a list of fields that are prefixed with the original field name minus _json.
# This will also allow us to export the data to a CSV or Excel file with the correct column names.
@@ -639,7 +659,7 @@ def handle_get_obj_li(
else: return mk_resp(data=None, response=commons.response, status_code=404)
# Updated 2023-11-03
# Updated 2024-08-14
@router.get('/{obj_type_l1}/{obj_id}')
@router.get('/{obj_type_l1}/{obj_type_l2}/{obj_id}')
@router.get('/{obj_type_l1}/{obj_type_l2}/{obj_type_l3}/{obj_id}')
@@ -649,8 +669,11 @@ async def get_obj(
obj_type_l3: str=None,
obj_id: str=None,
use_alt_table: bool = False, # NOTE: This will use table_name_alt if they exist. -2023-12-01
use_alt_base: bool = False, # NOTE: This will use base_name_alt if they exist. -2023-12-01
tbl_alt: Optional[str] = 'default',
mdl_alt: Optional[str] = 'default',
# use_alt_table: bool = False, # NOTE: This will use table_name_alt if they exist. -2023-12-01
# use_alt_base: bool = False, # NOTE: This will use base_name_alt if they exist. -2023-12-01
# for_obj_type: Optional[str] = Query(None, max_length=50), # NOTE: This is not currently used. It is here for future use.
# for_obj_id: Optional[str] = Query(None, max_length=22), # NOTE: This is not currently used. It is here for future use.
@@ -690,8 +713,8 @@ async def get_obj(
debug_data['obj_type_l2'] = obj_type_l2
debug_data['obj_type_l3'] = obj_type_l3
debug_data['obj_id'] = obj_id
debug_data['use_alt_table'] = use_alt_table
debug_data['use_alt_base'] = use_alt_base
debug_data['tbl_alt'] = tbl_alt
debug_data['mdl_alt'] = mdl_alt
log.debug(debug_data)
@@ -721,17 +744,31 @@ async def get_obj(
log.warning('We should not be here')
return mk_resp(data=False, status_code=400, response=commons.response)
if use_alt_table:
table_name = obj_type_kv_li[obj_name]['table_name_alt']
# Updated 2024-08-14
# Check the table (view) name to make sure it is valid.
if tbl_alt: # and tbl_alt != 'default':
try:
table_name = obj_type_kv_li[obj_name][f'tbl_{tbl_alt}']
# table_name = obj_type_kv_li[obj_name].get(f'tbl_{tbl_alt}')
except:
table_name = obj_type_kv_li[obj_name]['tbl']
log.warning(f'tbl_alt ({tbl_alt}) was not found. Using {table_name} table.')
log.info(f'tbl_alt ({tbl_alt}) was found. Using {table_name} table.')
else:
table_name = obj_type_kv_li[obj_name]['table_name']
table_name = obj_type_kv_li[obj_name]['tbl']
log.warning(f'tbl_alt ({tbl_alt}) was not found. Using {table_name} table.')
if use_alt_base:
base_name = obj_type_kv_li[obj_name]['base_name_alt']
# log.setLevel(logging.DEBUG)
log.debug(debug_data)
# Check the model name to make sure it is valid.
if mdl_alt:
try:
base_name = obj_type_kv_li[obj_name][f'mdl_{mdl_alt}']
# base_name = obj_type_kv_li[obj_name].get(f'mdl_{mdl_alt}')
except:
base_name = obj_type_kv_li[obj_name]['mdl']
log.info(f'mdl_alt ({mdl_alt}) was found. Using {base_name} model.')
else:
base_name = obj_type_kv_li[obj_name]['base_name']
base_name = obj_type_kv_li[obj_name]['mdl']
log.warning(f'mdl_alt ({mdl_alt}) was not found. Using {base_name} model.')
# NOTE: Add a check for the object ID... assuming it is a random ID string for now.
if sql_result := sql_select(table_name=table_name, record_id_random=obj_id):