From 3c13b625020ed7e3e000481cb51e5a72d730cea5 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Wed, 25 Aug 2021 20:06:07 -0400 Subject: [PATCH] Working on contact and address create update v4 along with person and user creation and update. --- app/methods/person_methods.py | 437 ++++++++++++++++++++++------------ app/methods/user_methods.py | 2 + app/routers/person.py | 10 +- 3 files changed, 295 insertions(+), 154 deletions(-) diff --git a/app/methods/person_methods.py b/app/methods/person_methods.py index ed01589..d79817e 100644 --- a/app/methods/person_methods.py +++ b/app/methods/person_methods.py @@ -8,7 +8,7 @@ from app.db_sql import redis_lookup_id_random, sql_insert, sql_insert_or_update, from app.lib_general import log, logging # from app.methods.address_methods import load_address_obj -from app.methods.contact_methods import create_contact_obj, create_update_contact_obj, load_contact_obj, update_contact_obj +from app.methods.contact_methods import create_contact_obj, create_update_contact_obj, create_update_contact_obj_v4, load_contact_obj, update_contact_obj from app.methods.order_methods import load_order_obj, get_order_rec_list from app.methods.organization_methods import create_update_organization_obj, load_organization_obj, update_organization_obj # from app.methods.user_methods import create_user_obj # , load_user_obj, update_user_obj @@ -382,7 +382,7 @@ def create_person_obj_v3( person_obj_new: Person_Base, create_sub_obj: bool = False, fail_any: bool = True, # Fail if any thing goes wrong for sub objects - return_dict: bool = False, + return_outline: bool = False, ) -> bool|dict|int: log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) @@ -412,77 +412,106 @@ def create_person_obj_v3( person_id = person_obj_in_result - return_dict = {} - return_dict['person_id'] = None - return_dict['contact_id'] = None - return_dict['contact'] = {} - return_dict['contact']['address_id'] = None - return_dict['organization_id'] = None - return_dict['user_id'] = None + person_outline = {} + person_outline['person_id'] = None + person_outline['contact_id'] = None + person_outline['contact'] = {} + person_outline['contact']['address_id'] = None + person_outline['organization_id'] = None + person_outline['user_id'] = None - if person_obj_new.contact and isinstance(person_obj_new.contact, dict): - log.info(f'Contact was found. Create a new Contact and link it to the new Person or update existing Contact. Person ID: {person_id}') - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - contact_obj_unknown = person_obj_new.contact - log.debug(contact_obj_unknown) - if contact_id := contact_obj_unknown.get('contact_id_random', None): - log.warning('Contact ID found. This is not expected, but should be ok.') - # from app.methods.contact_methods import update_contact_obj_v3 - if update_contact_obj_result := update_contact_obj( - contact_id = contact_id, - contact_obj_up = contact_obj_unknown, - create_sub_obj = create_sub_obj, - fail_any = fail_any, - ): - contact_id = update_contact_obj_result - log.info(f'Contact updated. Contact ID: {contact_id}') - else: - log.warning(f'Contact not updated. Person ID: {person_id}') - log.debug(update_contact_obj_result) - contact_id = None - if fail_any: return False - - if isinstance(update_contact_obj_result, int): - contact_id = update_contact_obj_result - log.info(f'Contact updated. Contact ID: {contact_id}') - else: - log.warning(f'Contact not updated. Person ID: {person_id}') - log.debug(update_contact_obj_result) - contact_id = None - if fail_any: return False - else: - log.info(f'No Contact ID found.') - # from app.methods.contact_methods import create_contact_obj - - contact_obj_unknown = person_obj_new.contact - contact_obj_unknown.for_type = 'person' - contact_obj_unknown.for_id = person_id - - if create_contact_obj_result := create_contact_obj( + # NOTE: Use object model version because of better type checking and validations + if person_obj_new.contact: + person_outline['contact_id'] = None + contact_obj = person_obj_new.contact + if contact_id := person_obj_new.contact_id: pass + elif contact_id := contact_obj.id: pass + else: contact_id = None + contact_obj.id + contact_obj.for_type = 'person' + contact_obj.for_id = person_id + create_update_contact_obj_result = create_update_contact_obj_v4( + contact_dict_obj = contact_obj, + contact_id = contact_id, account_id = account_id, - for_type = 'person', - for_id = person_id, - contact_obj_new = contact_obj_unknown, - create_sub_obj = create_sub_obj, - fail_any = fail_any, - ): - if isinstance(create_contact_obj_result, int): - contact_id = create_contact_obj_result - log.info(f'Contact created. Contact ID: {contact_id}') - else: - log.warning(f'Contact not created. Person ID: {person_id}') - log.debug(create_contact_obj_result) - contact_id = None - if fail_any: return False - else: - log.warning(f'Contact not created. Person ID: {person_id}') - log.debug(create_contact_obj_result) - contact_id = None - if fail_any: return False - return_dict['contact_id'] = contact_id - else: - log.info('Contact not found or not in a dict.') - pass + for_type = 'person', + for_id = person_id, + fail_any = fail_any, + return_outline = return_outline, + ) + if isinstance(create_update_contact_obj_result, int): + contact_id = create_update_contact_obj_result + elif create_update_contact_obj_result == True: pass + else: + log.warning(f'Create or Update failed while trying create_update_contact_obj_v4(): {create_update_contact_obj_result}') + contact_id = None + + person_outline['contact_id'] = contact_id + + # NOTE: This is the older pre v4 version + # if person_obj_new.contact and isinstance(person_obj_new.contact, dict): + # log.info(f'Contact was found. Create a new Contact and link it to the new Person or update existing Contact. Person ID: {person_id}') + # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + # contact_obj_unknown = person_obj_new.contact + # log.debug(contact_obj_unknown) + # if contact_id := contact_obj_unknown.get('contact_id_random', None): + # log.warning('Contact ID found. This is not expected, but should be ok.') + # # from app.methods.contact_methods import update_contact_obj_v3 + # if update_contact_obj_result := update_contact_obj( + # contact_id = contact_id, + # contact_obj_up = contact_obj_unknown, + # create_sub_obj = create_sub_obj, + # fail_any = fail_any, + # ): + # contact_id = update_contact_obj_result + # log.info(f'Contact updated. Contact ID: {contact_id}') + # else: + # log.warning(f'Contact not updated. Person ID: {person_id}') + # log.debug(update_contact_obj_result) + # contact_id = None + # if fail_any: return False + + # if isinstance(update_contact_obj_result, int): + # contact_id = update_contact_obj_result + # log.info(f'Contact updated. Contact ID: {contact_id}') + # else: + # log.warning(f'Contact not updated. Person ID: {person_id}') + # log.debug(update_contact_obj_result) + # contact_id = None + # if fail_any: return False + # else: + # log.info(f'No Contact ID found.') + # # from app.methods.contact_methods import create_contact_obj + + # contact_obj_unknown = person_obj_new.contact + # contact_obj_unknown.for_type = 'person' + # contact_obj_unknown.for_id = person_id + + # if create_contact_obj_result := create_contact_obj( + # account_id = account_id, + # for_type = 'person', + # for_id = person_id, + # contact_obj_new = contact_obj_unknown, + # create_sub_obj = create_sub_obj, + # fail_any = fail_any, + # ): + # if isinstance(create_contact_obj_result, int): + # contact_id = create_contact_obj_result + # log.info(f'Contact created. Contact ID: {contact_id}') + # else: + # log.warning(f'Contact not created. Person ID: {person_id}') + # log.debug(create_contact_obj_result) + # contact_id = None + # if fail_any: return False + # else: + # log.warning(f'Contact not created. Person ID: {person_id}') + # log.debug(create_contact_obj_result) + # contact_id = None + # if fail_any: return False + # person_outline['contact_id'] = contact_id + # else: + # log.info('Contact not found or not in a dict.') + # pass # NOTE: This is the older pre v3 version # if person_obj_new.contact: @@ -551,7 +580,7 @@ def create_person_obj_v3( log.debug(create_organization_obj_result) organization_id = None if fail_any: return False - return_dict['organization_id'] = organization_id + person_outline['organization_id'] = organization_id else: log.info('Organization not found or not in a dict.') pass @@ -573,6 +602,20 @@ def create_person_obj_v3( ): user_id = update_user_obj_result log.info(f'User updated. User ID: {user_id}') + + # NOTE: This should not be needed. Updating person just in case! + # Need to update the person with the current user_id + if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass + else: return False + log.info(f'Need to update the person with the current user_id.') + person_dict_up = {} + person_dict_up['id'] = person_id + person_dict_up['user_id'] = user_id + if person_dict_up_result := sql_update(data=person_dict_up, table_name='person'): pass + else: + log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}') + if fail_any: return False + log.debug(person_dict_up_result) else: log.warning(f'User not updated. Person ID: {person_id}') log.debug(update_user_obj_result) @@ -582,6 +625,19 @@ def create_person_obj_v3( if isinstance(update_user_obj_result, int): user_id = update_user_obj_result log.info(f'User updated. User ID: {user_id}') + + # Need to update the person with the current user_id + if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass + else: return False + log.info(f'Need to update the person with the current user_id.') + person_dict_up = {} + person_dict_up['id'] = person_id + person_dict_up['user_id'] = user_id + if person_dict_up_result := sql_update(data=person_dict_up, table_name='person'): pass + else: + log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}') + if fail_any: return False + log.debug(person_dict_up_result) else: log.warning(f'User not updated. Person ID: {person_id}') log.debug(update_user_obj_result) @@ -600,6 +656,17 @@ def create_person_obj_v3( if isinstance(create_user_obj_result, int): user_id = create_user_obj_result log.info(f'User created. User ID: {user_id}') + + # Need to update the person with the new user_id + log.info(f'Need to update the person with the new user_id.') + person_dict_up = {} + person_dict_up['id'] = person_id + person_dict_up['user_id'] = user_id + if person_dict_up_result := sql_update(data=person_dict_up, table_name='person'): pass + else: + log.error(f'Person not updated with new User ID. Person ID: {person_id} User ID: {user_id}') + if fail_any: return False + log.debug(person_dict_up_result) else: log.warning(f'User not created. Person ID: {person_id}') log.debug(create_user_obj_result) @@ -622,7 +689,7 @@ def create_person_obj_v3( if fail_any: return False log.debug(person_obj_up_result) - return_dict['user_id'] = user_id + person_outline['user_id'] = user_id else: log.info('User not found or not in a dict.') pass @@ -667,7 +734,7 @@ def update_person_obj_v3( person_obj_exist: Person_Base, create_sub_obj: bool = False, fail_any: bool = False, # Fail if any thing goes wrong for sub objects - return_dict: bool = False, + return_outline: bool = False, ) -> bool: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) @@ -703,83 +770,112 @@ def update_person_obj_v3( log.debug(person_obj_up_result) return False - return_dict = {} - return_dict['person_id'] = person_id - return_dict['contact_id'] = None - return_dict['contact'] = {} - return_dict['contact']['address_id'] = None - return_dict['organization_id'] = None - return_dict['user_id'] = None + person_outline = {} + person_outline['person_id'] = person_id + person_outline['contact_id'] = None + person_outline['contact'] = {} + person_outline['contact']['address_id'] = None + person_outline['organization_id'] = None + person_outline['user_id'] = None + # NOTE: Use object model version because of better type checking and validations if person_obj_exist.contact: - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - contact_obj_unknown = person_obj_exist.contact - log.debug(contact_obj_unknown) - if isinstance(person_obj_exist.contact, dict): - contact_id = contact_obj_unknown.get('contact_id_random', None) - # try: - # contact_obj_new = Contact_Base(**contact_obj_new) - # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - # log.debug(contact_obj_new) - # except ValidationError as e: - # log.error(e.json()) - # return False + person_outline['contact_id'] = None + contact_obj = person_obj_exist.contact + if contact_id := person_obj_exist.contact_id: pass + elif contact_id := contact_obj.id: pass + else: contact_id = None + contact_obj.id + contact_obj.for_type = 'person' + contact_obj.for_id = person_id + create_update_contact_obj_result = create_update_contact_obj_v4( + contact_dict_obj = contact_obj, + contact_id = contact_id, + account_id = account_id, + for_type = 'person', + for_id = person_id, + fail_any = fail_any, + return_outline = return_outline, + ) + if isinstance(create_update_contact_obj_result, int): + contact_id = create_update_contact_obj_result + elif create_update_contact_obj_result == True: pass else: - contact_id = contact_obj_unknown.id + log.warning(f'Create or Update failed while trying create_update_contact_obj_v4(): {create_update_contact_obj_result}') + contact_id = None - # if contact_id := contact_obj_unknown.get('contact_id_random', None): - if contact_id: - # from app.methods.contact_methods import update_contact_obj_v3 - if update_contact_obj_result := update_contact_obj( - contact_id = contact_id, - contact_obj_up = contact_obj_unknown, - create_sub_obj = create_sub_obj, - fail_any = fail_any, - ): - contact_id = update_contact_obj_result - log.info(f'Contact updated. Contact ID: {contact_id}') - else: - log.warning(f'Contact not updated. Person ID: {person_id}') - log.debug(update_contact_obj_result) - contact_id = None - if fail_any: return False + person_outline['contact_id'] = contact_id - if isinstance(update_contact_obj_result, int): - contact_id = update_contact_obj_result - log.info(f'Contact updated. Contact ID: {contact_id}') - else: - log.warning(f'Contact not updated. Person ID: {person_id}') - log.debug(update_contact_obj_result) - contact_id = None - if fail_any: return False - else: - log.info(f'No Contact ID found.') - # from app.methods.contact_methods import create_contact_obj_v3 - if create_contact_obj_result := create_contact_obj( - account_id = account_id, - for_type = 'person', - for_id = person_id, - contact_obj_new = contact_obj_unknown, - create_sub_obj = create_sub_obj, - fail_any = fail_any, - ): - if isinstance(create_contact_obj_result, int): - contact_id = create_contact_obj_result - log.info(f'Contact created. Contact ID: {contact_id}') - else: - log.warning(f'Contact not created. Person ID: {person_id}') - log.debug(create_contact_obj_result) - contact_id = None - if fail_any: return False - else: - log.warning(f'Contact not created. Person ID: {person_id}') - log.debug(create_contact_obj_result) - contact_id = None - if fail_any: return False - return_dict['contact_id'] = contact_id - else: - log.info('Contact not found or not in a dict.') - pass + # NOTE: This is the older pre v3 version + # if person_obj_exist.contact: + # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + # contact_obj_unknown = person_obj_exist.contact + # log.debug(contact_obj_unknown) + # if isinstance(person_obj_exist.contact, dict): + # contact_id = contact_obj_unknown.get('contact_id_random', None) + # # try: + # # contact_obj_new = Contact_Base(**contact_obj_new) + # # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + # # log.debug(contact_obj_new) + # # except ValidationError as e: + # # log.error(e.json()) + # # return False + # else: + # contact_id = contact_obj_unknown.id + + # # if contact_id := contact_obj_unknown.get('contact_id_random', None): + # if contact_id: + # # from app.methods.contact_methods import update_contact_obj_v3 + # if update_contact_obj_result := update_contact_obj( + # contact_id = contact_id, + # contact_obj_up = contact_obj_unknown, + # create_sub_obj = create_sub_obj, + # fail_any = fail_any, + # ): + # contact_id = update_contact_obj_result + # log.info(f'Contact updated. Contact ID: {contact_id}') + # else: + # log.warning(f'Contact not updated. Person ID: {person_id}') + # log.debug(update_contact_obj_result) + # contact_id = None + # if fail_any: return False + + # if isinstance(update_contact_obj_result, int): + # contact_id = update_contact_obj_result + # log.info(f'Contact updated. Contact ID: {contact_id}') + # else: + # log.warning(f'Contact not updated. Person ID: {person_id}') + # log.debug(update_contact_obj_result) + # contact_id = None + # if fail_any: return False + # else: + # log.info(f'No Contact ID found.') + # # from app.methods.contact_methods import create_contact_obj_v3 + # if create_contact_obj_result := create_contact_obj( + # account_id = account_id, + # for_type = 'person', + # for_id = person_id, + # contact_obj_new = contact_obj_unknown, + # create_sub_obj = create_sub_obj, + # fail_any = fail_any, + # ): + # if isinstance(create_contact_obj_result, int): + # contact_id = create_contact_obj_result + # log.info(f'Contact created. Contact ID: {contact_id}') + # else: + # log.warning(f'Contact not created. Person ID: {person_id}') + # log.debug(create_contact_obj_result) + # contact_id = None + # if fail_any: return False + # else: + # log.warning(f'Contact not created. Person ID: {person_id}') + # log.debug(create_contact_obj_result) + # contact_id = None + # if fail_any: return False + # person_outline['contact_id'] = contact_id + # else: + # log.info('Contact not found or not in a dict.') + # pass if person_obj_exist.organization and isinstance(person_obj_exist.organization, dict): log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL @@ -831,7 +927,7 @@ def update_person_obj_v3( log.debug(create_organization_obj_result) organization_id = None if fail_any: return False - return_dict['organization_id'] = organization_id + person_outline['organization_id'] = organization_id else: log.info('Organization not found or not in a dict.') pass @@ -849,8 +945,23 @@ def update_person_obj_v3( create_sub_obj = create_sub_obj, fail_any = fail_any, ): - user_id = update_user_obj_result + # user_id = update_user_obj_result # Result will hopefully always be 1 or True log.info(f'User updated. User ID: {user_id}') + log.debug(update_user_obj_result) + + # NOTE: This should not be needed. Updating person just in case! + # Need to update the person with the current user_id + if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass + else: return False + log.info(f'Need to update the person with the current user_id.') + person_dict_up = {} + person_dict_up['id'] = person_id + person_dict_up['user_id'] = user_id + if person_dict_up_result := sql_update(data=person_dict_up, table_name='person'): pass + else: + log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}') + if fail_any: return False + log.debug(person_dict_up_result) else: log.warning(f'User not updated. Person ID: {person_id}') log.debug(update_user_obj_result) @@ -858,8 +969,23 @@ def update_person_obj_v3( if fail_any: return False if isinstance(update_user_obj_result, int): - user_id = update_user_obj_result + # user_id = update_user_obj_result # Result will hopefully always be 1 or True log.info(f'User updated. User ID: {user_id}') + log.debug(update_user_obj_result) + + # NOTE: This should not be needed. Updating person just in case! + # Need to update the person with the current user_id + if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass + else: return False + log.info(f'Need to update the person with the current user_id.') + person_dict_up = {} + person_dict_up['id'] = person_id + person_dict_up['user_id'] = user_id + if person_dict_up_result := sql_update(data=person_dict_up, table_name='person'): pass + else: + log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}') + if fail_any: return False + log.debug(person_dict_up_result) else: log.warning(f'User not updated. Person ID: {person_id}') log.debug(update_user_obj_result) @@ -878,6 +1004,17 @@ def update_person_obj_v3( if isinstance(create_user_obj_result, int): user_id = create_user_obj_result log.info(f'User created. User ID: {user_id}') + + # Need to update the person with the new user_id + log.info(f'Need to update the person with the new user_id.') + person_dict_up = {} + person_dict_up['id'] = person_id + person_dict_up['user_id'] = user_id + if person_dict_up_result := sql_update(data=person_dict_up, table_name='person'): pass + else: + log.error(f'Person not updated with new User ID. Person ID: {person_id} User ID: {user_id}') + if fail_any: return False + log.debug(person_dict_up_result) else: log.warning(f'User not created. Person ID: {person_id}') log.debug(create_user_obj_result) @@ -888,7 +1025,7 @@ def update_person_obj_v3( log.debug(create_user_obj_result) user_id = None if fail_any: return False - return_dict['user_id'] = user_id + person_outline['user_id'] = user_id else: log.info('User not found or not in a dict.') pass diff --git a/app/methods/user_methods.py b/app/methods/user_methods.py index b980472..698606e 100644 --- a/app/methods/user_methods.py +++ b/app/methods/user_methods.py @@ -29,6 +29,8 @@ def create_user_obj( user_obj_new: User_New_Base, allow_update: bool = False, # Allow updating the user account if one is found avoid_dup_username: bool = False, # Avoid creating a duplicate by modifying the supplied username + create_sub_obj: bool = False, + fail_any: bool = True, # Fail if any thing goes wrong for sub objects return_dict: bool = False, ) -> bool|dict|int: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL diff --git a/app/routers/person.py b/app/routers/person.py index 9ebd049..7efd92f 100644 --- a/app/routers/person.py +++ b/app/routers/person.py @@ -105,7 +105,8 @@ async def post_person_obj_new_v3( account_id = x_account_id, person_obj_new = person_obj, create_sub_obj = create_sub_obj, - fail_any = fail_any + fail_any = fail_any, + return_outline = False, ): pass else: return mk_resp(data=False, status_code=400, response=response, status_message='The person was not created. Check the field names and data types.') @@ -160,8 +161,9 @@ async def patch_person_obj_exist_v3( person_obj_exist = person_obj, create_sub_obj = create_sub_obj, fail_any = fail_any, + return_outline = False, ): pass - else: return mk_resp(data=False, status_code=400, response=response, status_message='The event person was not created. Check the field names and data types.') + else: return mk_resp(data=False, status_code=400, response=response, status_message='The person was not created. Check the field names and data types.') if update_person_obj_result: if return_obj: @@ -174,9 +176,9 @@ async def patch_person_obj_exist_v3( data = {} data['person_id'] = person_id data['person_id_random'] = person_id_random - return mk_resp(data=data, response=response, status_message='The event person was created.') + return mk_resp(data=data, response=response, status_message='The person was created.') else: - return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to create an event person was unexpected.') + return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to create an person was unexpected.') # ### END ### API Person ### patch_person_obj_exist_v3() ###