Saving these changes in a working state. Just in case.
This commit is contained in:
@@ -39,6 +39,9 @@ async def get_obj(
|
||||
):
|
||||
"""
|
||||
Get a single top-level object by its random ID.
|
||||
Examples:
|
||||
- /v3/crud/journal/{journal_id}
|
||||
- /v3/crud/account/{account_id}
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -84,6 +87,10 @@ async def get_obj_li(
|
||||
):
|
||||
"""
|
||||
Get a list of top-level objects.
|
||||
Examples:
|
||||
- /v3/crud/journal/
|
||||
- /v3/crud/journal/?for_obj_type=account&for_obj_id={account_id_random}
|
||||
- /v3/crud/journal/?jp={"qry":[{"type":"AND","field":"for_type","operator":"=","value":"user"},{"type":"AND","field":"for_id","operator":"=","value":<user_id>}]}
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -199,6 +206,8 @@ async def post_obj(
|
||||
):
|
||||
"""
|
||||
Create a new top-level object.
|
||||
Examples:
|
||||
- POST /v3/crud/journal/ (with Journal_Base in body)
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -261,6 +270,8 @@ async def patch_obj(
|
||||
):
|
||||
"""
|
||||
Update a top-level object.
|
||||
Examples:
|
||||
- PATCH /v3/crud/journal/{journal_id} (with Journal_Base fields in body)
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -319,6 +330,9 @@ async def delete_obj(
|
||||
):
|
||||
"""
|
||||
Delete a top-level object.
|
||||
Examples:
|
||||
- DELETE /v3/crud/journal/{journal_id}
|
||||
- DELETE /v3/crud/journal/{journal_id}?method=disable
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -369,6 +383,8 @@ async def get_child_obj_li(
|
||||
):
|
||||
"""
|
||||
Get a list of child objects belonging to a parent.
|
||||
Examples:
|
||||
- /v3/crud/journal/{journal_id}/journal_entry/
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -475,6 +491,8 @@ async def post_child_obj(
|
||||
):
|
||||
"""
|
||||
Create a new child object for a given parent.
|
||||
Examples:
|
||||
- POST /v3/crud/journal/{journal_id}/journal_entry/ (with Journal_Entry_Base in body)
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -549,6 +567,8 @@ async def get_child_obj(
|
||||
):
|
||||
"""
|
||||
Get a single child object by its ID, ensuring it belongs to the correct parent.
|
||||
Examples:
|
||||
- /v3/crud/journal/{journal_id}/journal_entry/{entry_id}
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -570,32 +590,47 @@ async def get_child_obj(
|
||||
if not resolved_parent_id:
|
||||
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f"Parent object '{parent_obj_name}' with ID '{parent_obj_id}' not found.")
|
||||
|
||||
obj_cfg = obj_type_kv_li[child_obj_name]
|
||||
table_name = obj_cfg.get('tbl_default', obj_cfg.get('tbl'))
|
||||
base_name = obj_cfg.get('mdl_default', obj_cfg.get('mdl'))
|
||||
|
||||
if not table_name or not base_name:
|
||||
return mk_resp(data=False, status_code=500, response=commons.response, status_message=f"Configuration for object type '{child_obj_name}' is incomplete.")
|
||||
|
||||
resolved_child_id = redis_lookup_id_random(record_id_random=child_obj_id, table_name=child_obj_name)
|
||||
if not resolved_child_id:
|
||||
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f"Child object with ID '{child_obj_id}' not found.")
|
||||
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f"Child object '{child_obj_name}' with ID '{child_obj_id}' not found.")
|
||||
|
||||
if sql_result := sql_select(table_name=table_name, record_id=resolved_child_id):
|
||||
# Verify the child belongs to the parent
|
||||
# Get config for child object
|
||||
obj_cfg = obj_type_kv_li[child_obj_name]
|
||||
table_name_update = obj_cfg.get('tbl_update', obj_cfg.get('tbl'))
|
||||
table_name_select = obj_cfg.get('tbl_default', obj_cfg.get('tbl'))
|
||||
output_model = obj_cfg.get('mdl_out', obj_cfg.get('mdl_default', obj_cfg.get('mdl')))
|
||||
|
||||
if not table_name_update or not table_name_select or not output_model:
|
||||
return mk_resp(data=False, status_code=500, response=commons.response, status_message=f"Configuration for child object type '{child_obj_name}' is incomplete.")
|
||||
|
||||
# Verify parentage before updating
|
||||
if existing_child := sql_select(table_name=table_name_select, record_id=resolved_child_id):
|
||||
parent_fk_field_name = f'{parent_obj_name}_id'
|
||||
if sql_result.get(parent_fk_field_name) != resolved_parent_id:
|
||||
if existing_child.get(parent_fk_field_name) != resolved_parent_id:
|
||||
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f"Child object '{child_obj_id}' not found under parent '{parent_obj_id}'.")
|
||||
|
||||
resp_data = base_name(**sql_result).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
return mk_resp(data=resp_data, response=commons.response)
|
||||
else:
|
||||
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f"Child object with ID '{child_obj_id}' not found in database.")
|
||||
return mk_resp(data=False, status_code=404, response=commons.response, status_message=f"Child object '{child_obj_id}' not found.")
|
||||
|
||||
# The sql_update function will only update the fields provided in the dict.
|
||||
data_to_update = obj_data
|
||||
|
||||
if sql_update(data=data_to_update, table_name=table_name_update, record_id=resolved_child_id):
|
||||
if return_obj:
|
||||
if updated_child := sql_select(table_name=table_name_select, record_id=resolved_child_id):
|
||||
resp_data = output_model(**updated_child).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
return mk_resp(data=resp_data, response=commons.response)
|
||||
else:
|
||||
return mk_resp(data=True, status_code=404, response=commons.response, status_message="Object updated but could not be retrieved post-update.")
|
||||
else:
|
||||
return mk_resp(data=True, response=commons.response, status_message="Object updated successfully.")
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message="Failed to update object in database.")
|
||||
|
||||
|
||||
@router.patch('/{parent_obj_type}/{parent_obj_id}/{child_obj_type}/{child_obj_id}', response_model=Resp_Body_Base)
|
||||
async def patch_child_obj(
|
||||
request: Request,
|
||||
response: Response,
|
||||
parent_obj_type: str = Path(min_length=2, max_length=50),
|
||||
parent_obj_id: str = Path(min_length=11, max_length=22),
|
||||
child_obj_type: str = Path(min_length=2, max_length=50),
|
||||
@@ -607,6 +642,8 @@ async def patch_child_obj(
|
||||
):
|
||||
"""
|
||||
Update a child object by its ID, ensuring it belongs to the correct parent.
|
||||
Examples:
|
||||
- PATCH /v3/crud/journal/{journal_id}/journal_entry/{entry_id}
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -670,6 +707,7 @@ async def patch_child_obj(
|
||||
|
||||
@router.delete('/{parent_obj_type}/{parent_obj_id}/{child_obj_type}/{child_obj_id}', response_model=Resp_Body_Base)
|
||||
async def delete_child_obj(
|
||||
response: Response,
|
||||
parent_obj_type: str = Path(min_length=2, max_length=50),
|
||||
parent_obj_id: str = Path(min_length=11, max_length=22),
|
||||
child_obj_type: str = Path(min_length=2, max_length=50),
|
||||
@@ -681,6 +719,9 @@ async def delete_child_obj(
|
||||
):
|
||||
"""
|
||||
Delete a child object by its ID, ensuring it belongs to the correct parent.
|
||||
Examples:
|
||||
- DELETE /v3/crud/journal/{journal_id}/journal_entry/{entry_id}
|
||||
- DELETE /v3/crud/journal/{journal_id}/journal_entry/{entry_id}?method=disable
|
||||
"""
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
@@ -734,12 +775,4 @@ async def delete_child_obj(
|
||||
if sql_result:
|
||||
return mk_resp(data=True, response=commons.response, status_message=f"Object with ID '{child_obj_id}' action '{method}' completed successfully.")
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message=f"Failed to perform '{method}' on object in database.")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message=f"Failed to perform '{method}' on object in database.")
|
||||
Reference in New Issue
Block a user