Work on person methods and related

This commit is contained in:
Scott Idem
2021-09-08 12:22:22 -04:00
parent 84aecddc7c
commit d0d8392817
12 changed files with 533 additions and 277 deletions

View File

@@ -448,87 +448,6 @@ def create_person_obj_v3(
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:
# 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}')
# contact_obj_new = person_obj_new.contact
# contact_obj_new.for_type = 'person'
# contact_obj_new.for_id = person_id
# create_contact_obj_result = create_contact_obj(contact_obj_new=contact_obj_new)
# 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
if person_obj_new.organization and isinstance(person_obj_new.organization, dict):
log.info(f'Organization was found. Create a new Organization and link it to the new Person or update existing Organization. Person ID: {person_id}')
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -806,77 +725,6 @@ def update_person_obj_v3(
person_outline['contact_id'] = contact_id
# 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
organization_obj_unknown = person_obj_exist.organization
@@ -1158,6 +1006,264 @@ def update_person_obj(
# ### END ### API Person Methods ### update_person_obj() ###
# ### BEGIN ### API Person Methods ### create_update_person_obj_v4b() ###
def create_update_person_obj_v4b(
account_id: int|str,
person_dict_obj: Person_Base,
person_id: int|str|None = None,
process_contact: bool = True, # For future v5
process_organization: bool = True, # For future v5
process_user: bool = True, # For future v5
create_sub_obj: bool = True, # For future v5
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
return_outline: bool = False, # For future v5
) -> bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('Checking requirements...')
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): log.info(f'Account ID: {account_id}')
else:
log.error('Missing or invalid Account ID passed. Failed requirement.')
log.error(f'Account ID: {account_id}')
return False
if person_id:
log.info(f'Person ID passed. Update existing Person below. Person ID: {person_id}')
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else:
log.error('Person ID passed but is invalid. Failed requirement.')
return False
# person_obj.id = person_id
else:
log.info('No Person ID passed. Create new Person below. Required: Account ID')
log.debug(type(person_dict_obj))
log.info('Create dictionary or Pydantic object variables...')
if isinstance(person_dict_obj, dict):
person_dict = person_dict_obj
person_dict['account_id'] = account_id
if person_id:
person_dict['person_id'] = person_id
try:
person_obj = Person_Base(**person_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
except ValidationError as e:
log.error(e.json())
return False
else:
person_obj = person_dict_obj
person_obj.account_id = account_id
if person_id:
# NOTE: Can't update the ID alias if it was never set.
person_obj.id = person_id
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'organization', 'user', 'created_on', 'updated_on'})
log.info(f'SQL INSERT or UPDATE record... Person ID: {person_id}')
if person_id:
if person_dict_up_result := sql_update(data=person_dict, table_name='person', rm_id_random=True): pass
else:
log.warning(f'Person not updated. Person ID: {person_id}')
log.debug(person_dict_up_result)
return False
log.debug(person_dict_up_result)
else:
if person_dict_in_result := sql_insert(data=person_dict, table_name='person', rm_id_random=True, id_random_length=default_num_bytes): pass
else:
log.warning(f'Person not created.')
log.debug(person_dict_in_result)
return False
log.debug(person_dict_in_result)
person_id = person_dict_in_result
person_outline = {}
person_outline['person_id'] = person_id
# Updated 2021-09-08
log.info(f'Check if processing Contact data...')
# NOTE: Use object model version because of better type checking and validations
if process_contact and person_obj.contact:
person_outline['contact_id'] = None
contact_obj = person_obj.contact
if contact_id := person_obj.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(
account_id = account_id,
contact_dict_obj = contact_obj,
contact_id = contact_id,
for_type = 'person',
for_id = person_id,
# process_address = process_address,
# create_sub_obj = create_sub_obj,
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
# Updated 2021-09-08
log.info(f'Check if processing Organization data...')
if process_organization and person_obj.organization:
organization_obj = person_obj.organization
organization_id = person_obj.organization_id_random
organization_result = create_update_organization_obj(
organization_id = organization_id,
organization_obj = organization_obj,
process_contact = True, # Setting to True under the assumption that if there is organization information then there is probably a contact (and address).
)
log.debug(organization_result)
if isinstance(organization_result, bool) and organization_result is True:
pass # Do not need to update person object.
elif isinstance(organization_result, bool) and organization_result is False:
pass # Do not need to update person object.
elif isinstance(organization_result, int):
person_obj.organization_id = organization_result
organization_id = organization_result
else:
log.warning('Something may have gone wrong while trying to create or update a organization.')
log.info(f'Check if need to update the Person with the Organization ID... Update Person Obj: {update_person_obj} Organization ID: {organization_id}')
if update_person_obj and isinstance(organization_id, int):
log.info(f'Updating the Person with the new/current Organization ID. Organization ID: {organization_id}')
person_data_up = {}
person_data_up['id'] = person_id
person_data_up['organization_id'] = organization_id
if person_data_up_result := sql_update(data=person_data_up, table_name='person'):
log.info(f'Person updated with Organization ID. Person ID: {person_id} Organization ID: {organization_id}')
else:
log.error(f'Person not updated with current Organization ID. Person ID: {person_id} Organization ID: {organization_id}')
if fail_any: return False
log.debug(person_dict_up_result)
person_outline['organization_id'] = organization_id
# Updated 2021-09-08
log.info(f'Check if processing User data... Process User: {process_user}')
if process_user and person_obj.user:
log.info(f'User data was found. Create a new User and link it to the new Person or update existing User. Person ID: {person_id}')
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
user_obj = person_obj.user
user_id = person_obj.user_id
update_person_obj = False
log.debug(user_obj)
if user_id:
log.warning('User ID found. This is not expected, but should be ok.')
# from app.methods.user_methods import update_user_obj_v3
from app.methods.user_methods import update_user_obj
if update_user_obj_result := update_user_obj(
user_id = user_id,
user_obj_up = user_obj,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
):
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!
update_person_obj = True
else:
log.warning(f'User not updated. Person ID: {person_id}')
log.debug(update_user_obj_result)
user_id = None
if fail_any: return False
# 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
# log.debug(person_dict_up_result)
# else:
# log.warning(f'User not updated. Person ID: {person_id}')
# log.debug(update_user_obj_result)
# user_id = None
# if fail_any: return False
else:
log.info(f'No User ID found.')
# from app.methods.user_methods import create_user_obj_v3
from app.methods.user_methods import create_user_obj
if create_user_obj_result := create_user_obj(
account_id = account_id,
user_obj_new = user_obj,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
):
if isinstance(create_user_obj_result, int):
log.info(f'User created. User ID: {user_id}')
log.debug(update_user_obj_result)
user_id = create_user_obj_result
# Need to update the person with the new user_id
update_person_obj = True
else:
log.warning(f'User not created. Updated? Person ID: {person_id}')
log.debug(create_user_obj_result)
user_id = None
if fail_any: return False
else:
log.warning(f'User not created. Person ID: {person_id}')
log.debug(create_user_obj_result)
user_id = None
if fail_any: return False
log.info(f'Check if need to update the Person with the User ID... Update Person Obj: {update_person_obj} User ID: {user_id}')
if update_person_obj and isinstance(user_id, int):
log.info(f'Updating the Person with the new/current User ID. User ID: {user_id}')
person_data_up = {}
person_data_up['id'] = person_id
person_data_up['user_id'] = user_id
if person_data_up_result := sql_update(data=person_data_up, table_name='person'):
log.info(f'Person updated with User ID. Person ID: {person_id} User ID: {user_id}')
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)
person_outline['user_id'] = user_id
else:
log.info('User data not found.')
pass
return person_id
# Process person data
person_dict_up = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
log.debug(person_dict_up)
# Update record
person_up_result = sql_update(
data = person_dict_up,
table_name = 'person',
rm_id_random = True,
)
log.debug(person_up_result)
if isinstance(person_up_result, bool) and person_up_result is True:
return person_id
elif isinstance(person_up_result, bool) and person_up_result is False:
return False
elif isinstance(person_up_result, int):
return person_up_result
else:
return False
# ### END ### API Person Methods ### create_update_person_obj_v4b() ###
# ### BEGIN ### API Person Methods ### create_update_person_obj() ###
def create_update_person_obj(
person_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value.
@@ -1169,11 +1275,15 @@ def create_update_person_obj(
log.debug(locals())
if person_id:
log.info(f'Person ID passed. Update existing Person. Person ID: {person_id}')
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else: return False
person_obj.id = person_id
else:
# Insert record now and update later
log.info('No Person ID passed. Create new Person. Required: Account ID')
person_dict_in = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
log.debug(person_dict_in)
person_in_result = sql_insert(