From 5f7225e3ab05adfd112ea49c83a7225b9d9b838d Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Fri, 18 Aug 2023 17:10:44 -0400 Subject: [PATCH] Improving the CRUD functions and other clean up. --- app/methods/hosted_file_methods.py | 17 ++++------------ app/models/archive_content_models.py | 6 +++--- app/routers/api_crud.py | 30 +++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/app/methods/hosted_file_methods.py b/app/methods/hosted_file_methods.py index c8b5467..52212dd 100644 --- a/app/methods/hosted_file_methods.py +++ b/app/methods/hosted_file_methods.py @@ -34,6 +34,7 @@ def create_hosted_file_obj(hosted_file_obj_new:Hosted_File_Base): # ### BEGIN ### API Hosted File Methods ### load_hosted_file_obj() ### +# Updated 2023-08-18 @logger_reset def load_hosted_file_obj( hosted_file_id: int|str, @@ -44,7 +45,7 @@ def load_hosted_file_obj( enabled: str = 'enabled', # enabled, disabled, all inc_hosted_file_link_list: bool = False, ) -> Hosted_File_Base|dict|bool: - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if hosted_file_id := redis_lookup_id_random(record_id_random=hosted_file_id, table_name='hosted_file'): pass @@ -53,7 +54,6 @@ def load_hosted_file_obj( if hosted_file_rec := sql_select(table_name='v_hosted_file', record_id=hosted_file_id): pass elif hosted_file_rec is None: return None else: return False - log.debug(hosted_file_rec) try: @@ -61,25 +61,16 @@ def load_hosted_file_obj( except ValidationError as e: log.error(e.json()) return False - # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.info(f'Filename: {hosted_file_obj.filename}; Size: {hosted_file_obj.size}; Hash SHA256: {hosted_file_obj.hash_sha256}; ') log.debug(hosted_file_obj) - # if inc_x: - # x_id = hosted_file_rec.get('x_id', None) - # if x_obj_result := load_x_obj(x_id=x_id): - # x_obj = x_obj_result - # hosted_file_obj.x = x_obj - # else: hosted_file_obj.x = None - if model_as_dict: - return hosted_file_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member + return hosted_file_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) else: return hosted_file_obj # ### END ### API Hosted File Methods ### load_hosted_file_obj() ### - - # ### BEGIN ### API Hosted File Methods ### lookup_file_hash() ### # Updated 2022-08-09 @logger_reset diff --git a/app/models/archive_content_models.py b/app/models/archive_content_models.py index ed48b0f..0b8e42b 100644 --- a/app/models/archive_content_models.py +++ b/app/models/archive_content_models.py @@ -14,9 +14,9 @@ class Archive_Content_Base(BaseModel): log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) - def testing(test_var=None): - log.debug(test_var) - return test_var + # def testing(test_var=None): + # log.debug(test_var) + # return test_var id_random: Optional[str] = Field( # **base_fields['archive_content_id_random'], diff --git a/app/routers/api_crud.py b/app/routers/api_crud.py index b08e5ce..32edf51 100644 --- a/app/routers/api_crud.py +++ b/app/routers/api_crud.py @@ -518,6 +518,8 @@ async def post_obj( # for_obj_type: Optional[str] = Query(None, max_length=50), # for_obj_id: Optional[str] = Query(None, max_length=22), + return_obj: bool = True, + commons: Common_Route_Params = Depends(common_route_params), ): """ @@ -594,8 +596,10 @@ async def post_obj( return mk_resp(data=False, status_code=400, response=commons.response) table_name = obj_type_li[obj_name].get('tbl_name_update') + table_name_select = obj_type_li[obj_name].get('table_name') exclude = obj_type_li[obj_name].get('exclude_for_db') + # ### SECTION ### Secondary data validation # if obj_id := redis_lookup_id_random(record_id_random=obj_id, table_name=table_name): pass # else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The object ID was invalid or not found.') @@ -620,14 +624,34 @@ async def post_obj( # NOTE: Add a check for the object ID... assuming it is a random ID string for now. Using rm_id_random. That helps with some field names. if sql_result := sql_insert(data=crud_data, table_name=table_name, rm_id_random=True, log_lvl=logging.INFO): + log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.info('The record was inserted.') log.debug(sql_result) resp_data = {} resp_data['table_name'] = table_name resp_data['request_data'] = obj_dict - resp_data['obj_id'] = sql_result # The ID should be returned + obj_id = sql_result # The ID should be returned + resp_data['obj_id'] = obj_id + obj_id_random = get_id_random(record_id=obj_id, table_name=table_name) + resp_data['obj_id_random'] = obj_id_random + if return_obj: + log.info('Returning object created from POST data') + if sql_select_result := sql_select(table_name=table_name_select, record_id=obj_id): + log.debug(sql_select_result) + + log.debug(base_name) + + resp_data = base_name(**sql_select_result).dict(by_alias=True, exclude_unset=commons.exclude_unset) + + log.debug(resp_data) + + return mk_resp(data=resp_data, response=commons.response) + else: + log.debug(sql_select_result) + return mk_resp(data=False, status_code=404, response=commons.response) + log.info('Returning IDs only created from POST data') return mk_resp(data=resp_data, response=commons.response) #, details=debug_data) elif sql_result == None: log.info('The record was probably not found to be updated.') @@ -759,8 +783,8 @@ def post_obj_template( obj_type: str, data: dict, id_random_length: int = 8, # Added 2023-04-13; need to move away from this - return_obj: bool=True, - by_alias: bool=True, + return_obj: bool = True, + by_alias: bool = True, include: Optional[list] = [], exclude: Optional[list] = [], exclude_unset: Optional[bool] = True,