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(
|
def redis_lookup_id_random(
|
||||||
record_id_random: int|str,
|
record_id_random: int|str,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
):
|
check_int_id: bool = False,
|
||||||
|
) -> str|int|bool|None:
|
||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
if isinstance(record_id_random, str) and len(record_id_random) >= 11 and len(record_id_random) <= 22: pass
|
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:
|
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
|
return False
|
||||||
|
|
||||||
if record_id_random and table_name:
|
if record_id_random and table_name:
|
||||||
@@ -920,13 +939,12 @@ def redis_lookup_id_random(
|
|||||||
|
|
||||||
|
|
||||||
# ### BEGIN ### API DB SQL ### get_id_random() ###
|
# ### BEGIN ### API DB SQL ### get_id_random() ###
|
||||||
# Changed name from lookup_id_random() to get_id_random()
|
# Updated 2022-01-06
|
||||||
# Updated 2021-08-23
|
|
||||||
@logger_reset
|
@logger_reset
|
||||||
def get_id_random(
|
def get_id_random(
|
||||||
record_id: int,
|
record_id: int,
|
||||||
table_name: str
|
table_name: str
|
||||||
):
|
) -> str|bool|None:
|
||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
@@ -938,7 +956,6 @@ def get_id_random(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if select_results := sql_select(sql=sql, data=data):
|
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(select_results)
|
||||||
log.debug(type(select_results))
|
log.debug(type(select_results))
|
||||||
if isinstance(select_results, dict):
|
if isinstance(select_results, dict):
|
||||||
@@ -946,18 +963,24 @@ def get_id_random(
|
|||||||
if record_id_random := select_results.get('id_random'):
|
if record_id_random := select_results.get('id_random'):
|
||||||
return str(record_id_random)
|
return str(record_id_random)
|
||||||
else:
|
else:
|
||||||
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
||||||
log.error('The SQL result was not what was expected.')
|
log.error('The SQL result was not what was expected.')
|
||||||
return False
|
return False
|
||||||
else:
|
elif isinstance(select_results, list):
|
||||||
log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.exception('More than one record may have been found. There may be a duplicate id. This should never happen.')
|
||||||
log.error('More than one record may have been found. There may be a duplicate id.')
|
|
||||||
log.error(select_results)
|
log.error(select_results)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
#log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
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.info('Record ID random was not found')
|
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
|
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() ###
|
# ### END ### API DB SQL ### get_id_random() ###
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -424,6 +424,29 @@ def get_person_rec_list(
|
|||||||
# ### END ### API Person Methods ### get_person_rec_list() ###
|
# ### END ### API Person Methods ### get_person_rec_list() ###
|
||||||
|
|
||||||
|
|
||||||
|
# def quick_dict_obj_return(
|
||||||
|
# data_dict_obj,
|
||||||
|
# Obj_Type,
|
||||||
|
# dict_exclude: list,
|
||||||
|
# ) -> dict:
|
||||||
|
|
||||||
|
# log.debug(type(data_dict_obj))
|
||||||
|
# if isinstance(data_dict_obj, dict):
|
||||||
|
# data_dict = data_dict_obj
|
||||||
|
# try:
|
||||||
|
# data_obj = Person_Base(**data_dict)
|
||||||
|
# data_obj = Obj_Type(**data_dict)
|
||||||
|
# log.debug(data_obj)
|
||||||
|
# except ValidationError as e:
|
||||||
|
# log.error(e.json())
|
||||||
|
# return False
|
||||||
|
# else:
|
||||||
|
# data_obj = data_dict_obj
|
||||||
|
# data_obj.account_id = account_id
|
||||||
|
|
||||||
|
# data_dict = data_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude=['contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'])
|
||||||
|
|
||||||
|
|
||||||
# ### BEGIN ### API Person Methods ### create_person_kiss() ###
|
# ### BEGIN ### API Person Methods ### create_person_kiss() ###
|
||||||
# Updated 2022-01-05
|
# Updated 2022-01-05
|
||||||
def create_person_kiss(
|
def create_person_kiss(
|
||||||
@@ -436,13 +459,13 @@ def create_person_kiss(
|
|||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
|
# ### SECTION ### Secondary data validation
|
||||||
log.info('Create dictionary or Pydantic object')
|
log.info('Create dictionary or Pydantic object')
|
||||||
log.debug(type(person_dict_obj))
|
log.debug(type(person_dict_obj))
|
||||||
if isinstance(person_dict_obj, dict):
|
if isinstance(person_dict_obj, dict):
|
||||||
person_dict = person_dict_obj
|
person_dict = person_dict_obj
|
||||||
try:
|
try:
|
||||||
person_obj = Person_Base(**person_dict)
|
person_obj = Person_Base(**person_dict)
|
||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
||||||
log.debug(person_obj)
|
log.debug(person_obj)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
log.error(e.json())
|
log.error(e.json())
|
||||||
@@ -453,6 +476,7 @@ def create_person_kiss(
|
|||||||
|
|
||||||
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
|
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
|
||||||
|
|
||||||
|
# ### SECTION ### Process data
|
||||||
if user_id:
|
if user_id:
|
||||||
# Link to an existing user
|
# Link to an existing user
|
||||||
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||||
@@ -469,7 +493,11 @@ def create_person_kiss(
|
|||||||
|
|
||||||
if contact_id and person.contact:
|
if contact_id and person.contact:
|
||||||
log.info('Updating Contact object')
|
log.info('Updating Contact object')
|
||||||
if contact_update_result := update_contact(): pass
|
if contact_update_result := update_contact(
|
||||||
|
contact_dict_obj = person_obj.contact,
|
||||||
|
for_type = 'person',
|
||||||
|
for_id = person_id,
|
||||||
|
): pass
|
||||||
else: return False
|
else: return False
|
||||||
elif person.contact:
|
elif person.contact:
|
||||||
log.info('Creating Contact object')
|
log.info('Creating Contact object')
|
||||||
@@ -484,7 +512,10 @@ def create_person_kiss(
|
|||||||
|
|
||||||
if user_id and person.user:
|
if user_id and person.user:
|
||||||
log.info('Updating User object')
|
log.info('Updating User object')
|
||||||
if user_update_result := update_user(): pass
|
if user_update_result := update_user(
|
||||||
|
user_dict_obj = person_obj.user,
|
||||||
|
person_id = person_id,
|
||||||
|
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||||
else: return False
|
else: return False
|
||||||
elif person.user:
|
elif person.user:
|
||||||
log.info('Creating User object')
|
log.info('Creating User object')
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ router = APIRouter()
|
|||||||
|
|
||||||
|
|
||||||
# ### BEGIN ### API Person Routers ### post_person_obj() ###
|
# ### BEGIN ### API Person Routers ### post_person_obj() ###
|
||||||
# Updated 2022-01-05
|
# Updated 2022-01-06
|
||||||
@router.post('/person', response_model=Resp_Body_Base)
|
@router.post('/person', response_model=Resp_Body_Base)
|
||||||
async def post_person_obj(
|
async def post_person_obj(
|
||||||
person_obj: Person_Base,
|
person_obj: Person_Base,
|
||||||
@@ -33,6 +33,20 @@ async def post_person_obj(
|
|||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
|
# ### SECTION ### Secondary data validation
|
||||||
|
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||||
|
elif contact_id is None: pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The contact ID was invalid or not found.')
|
||||||
|
|
||||||
|
if organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass
|
||||||
|
elif organization_id is None: pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The organization ID was invalid or not found.')
|
||||||
|
|
||||||
|
if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass
|
||||||
|
elif user_id is None: pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The user ID was invalid or not found.')
|
||||||
|
|
||||||
|
# ### SECTION ### Process data
|
||||||
if person_id := create_person_kiss(
|
if person_id := create_person_kiss(
|
||||||
account_id = commons.x_account_id,
|
account_id = commons.x_account_id,
|
||||||
person_dict_obj = person_obj,
|
person_dict_obj = person_obj,
|
||||||
@@ -42,8 +56,9 @@ async def post_person_obj(
|
|||||||
): pass
|
): pass
|
||||||
else:
|
else:
|
||||||
log.warning('Likely bad request')
|
log.warning('Likely bad request')
|
||||||
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||||
|
|
||||||
|
# ### SECTION ### Return successful results
|
||||||
if return_obj:
|
if return_obj:
|
||||||
person_obj = load_person_obj(
|
person_obj = load_person_obj(
|
||||||
person_id = person_id,
|
person_id = person_id,
|
||||||
@@ -60,7 +75,7 @@ async def post_person_obj(
|
|||||||
|
|
||||||
|
|
||||||
# ### BEGIN ### API Person Routers ### patch_person_obj() ###
|
# ### BEGIN ### API Person Routers ### patch_person_obj() ###
|
||||||
# Updated 2022-01-05
|
# Updated 2022-01-06
|
||||||
@router.patch('/person/{person_id}', response_model=Resp_Body_Base)
|
@router.patch('/person/{person_id}', response_model=Resp_Body_Base)
|
||||||
async def patch_person_obj(
|
async def patch_person_obj(
|
||||||
person_obj: Person_Base,
|
person_obj: Person_Base,
|
||||||
@@ -71,10 +86,28 @@ async def patch_person_obj(
|
|||||||
return_obj: Optional[bool] = True,
|
return_obj: Optional[bool] = True,
|
||||||
commons: Common_Route_Params = Depends(common_route_params),
|
commons: Common_Route_Params = Depends(common_route_params),
|
||||||
):
|
):
|
||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
|
# ### SECTION ### Secondary data validation
|
||||||
|
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.')
|
||||||
|
|
||||||
|
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||||
|
elif contact_id is None: pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The contact ID was invalid or not found.')
|
||||||
|
|
||||||
|
if organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass
|
||||||
|
elif organization_id is None: pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The organization ID was invalid or not found.')
|
||||||
|
|
||||||
|
if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass
|
||||||
|
elif user_id is None: pass
|
||||||
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The user ID was invalid or not found.')
|
||||||
|
|
||||||
|
# ### SECTION ### Process data
|
||||||
if person_update_result := update_person_kiss(
|
if person_update_result := update_person_kiss(
|
||||||
|
person_id = person_id,
|
||||||
person_dict_obj = person_obj,
|
person_dict_obj = person_obj,
|
||||||
contact_id = contact_id,
|
contact_id = contact_id,
|
||||||
organization_id = organization_id,
|
organization_id = organization_id,
|
||||||
@@ -82,8 +115,9 @@ async def patch_person_obj(
|
|||||||
): pass
|
): pass
|
||||||
else:
|
else:
|
||||||
log.warning('Likely bad request')
|
log.warning('Likely bad request')
|
||||||
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||||
|
|
||||||
|
# ### SECTION ### Return successful results
|
||||||
if return_obj:
|
if return_obj:
|
||||||
person_obj = load_person_obj(
|
person_obj = load_person_obj(
|
||||||
person_id = person_id,
|
person_id = person_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user