Code clean up. Simplifying person, user, contact, and address methods

This commit is contained in:
Scott Idem
2022-01-06 13:19:12 -05:00
parent c127e0822c
commit 567f6a6302
6 changed files with 350 additions and 204 deletions

View File

@@ -223,11 +223,10 @@ def create_update_address_obj_v4(
# ### BEGIN ### API Address Methods ### create_address_obj() ### # ### BEGIN ### API Address Methods ### create_address_obj() ###
# NOTE: This will create an address. # Updated 2022-01-06
# Reviewed and updated 2021-08-10
def create_address_obj( def create_address_obj(
address_obj_new: Address_Base, account_id: int|str,
account_id: int|str = None, address_dict_obj: Address_Base,
for_type: str = None, for_type: str = None,
for_id: int|str = None, for_id: int|str = None,
fail_any: bool = False, # Fail if any thing goes wrong for sub objects fail_any: bool = False, # Fail if any thing goes wrong for sub objects
@@ -235,80 +234,128 @@ def create_address_obj(
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())
# ### SECTION ### Secondary data validation
# NOTE: Remove at future date. Is this check needed if we trust that the ID is checked ahead of time?
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
else: return False else: return False
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
else: return False else: return False
log.debug(type(address_obj_new)) log.info('Create dictionary or Pydantic object')
if isinstance(address_obj_new, dict): log.debug(type(address_dict_obj))
if account_id: if isinstance(address_dict_obj, dict):
address_obj_new['account_id'] = account_id address_dict = address_dict_obj
if for_type:
address_obj_new['for_type'] = for_type
if for_id:
address_obj_new['for_id'] = for_id
try: try:
address_obj_new = Address_Base(**address_obj_new) address_obj = Contact_Base(**address_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(address_obj)
log.debug(address_obj_new)
except ValidationError as e: except ValidationError as e:
log.error(e.json()) log.error(e.json())
return False return False
else: else:
if account_id: address_obj = address_dict_obj
address_obj_new.account_id = account_id address_obj.account_id = account_id
if for_type:
address_obj_new.for_type = for_type
if for_id:
address_obj_new.for_id = for_id
address_obj_data = address_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'}) address_dict = address_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
if address_obj_in_result := sql_insert(data=address_obj_data, table_name='address', rm_id_random=True, id_random_length=8): pass # log.debug(type(address_dict_obj))
# if isinstance(address_dict_obj, dict):
# if account_id:
# address_obj_new['account_id'] = account_id
# if for_type:
# address_obj_new['for_type'] = for_type
# if for_id:
# address_obj_new['for_id'] = for_id
# try:
# address_obj_new = Address_Base(**address_obj_new)
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(address_obj_new)
# except ValidationError as e:
# log.error(e.json())
# return False
# else:
# if account_id:
# address_obj_new.account_id = account_id
# if for_type:
# address_obj_new.for_type = for_type
# if for_id:
# address_obj_new.for_id = for_id
# address_dict = address_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
# ### SECTION ### Process data
address_obj.account_id = account_id
address_obj.for_type = for_type
address_obj.for_id = for_id
address_dict['account_id'] = account_id
address_dict['for_type'] = for_type
address_dict['for_id'] = for_id
if address_dict_in_result := sql_insert(data=address_dict, table_name='address', rm_id_random=True, id_random_length=8): pass
else: else:
return False return False
log.debug(address_obj_in_result) log.debug(address_dict_in_result)
address_id = address_dict_in_result
address_id = address_obj_in_result log.inf(f'Returning the new address_id: {address_id}')
log.debug(f'Returning the new address_id: {address_id}')
return address_id return address_id
# ### END ### API Address Methods ### create_address_obj() ### # ### END ### API Address Methods ### create_address_obj() ###
# ### BEGIN ### API Address Methods ### update_address_obj() ### # ### BEGIN ### API Address Methods ### update_address_obj() ###
# NOTE: This will update an address. # Updated 2022-01-06
# Reviewed and updated 2021-08-10
def update_address_obj( def update_address_obj(
address_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value. address_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
address_obj_up: Address_Base, address_dict_obj: Address_Base,
create_sub_obj: bool = False, create_sub_obj: bool = False,
fail_any: bool = False, # Fail if any thing goes wrong for sub objects fail_any: bool = False, # Fail if any thing goes wrong for sub objects
) -> bool: ) -> bool:
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
# NOTE: Remove at future date. Is this check needed if we trust that the ID is checked ahead of time?
if address_id := redis_lookup_id_random(record_id_random=address_id, table_name='address'): pass if address_id := redis_lookup_id_random(record_id_random=address_id, table_name='address'): pass
else: return False else: return False
address_obj_up.id = address_id log.info('Create dictionary or Pydantic object')
log.debug(type(address_dict_obj))
log.debug(address_obj_up) if isinstance(address_dict_obj, dict):
# log.debug(address_obj_up.dict(by_alias=True, exclude_unset=True)) address_dict = address_dict_obj
log.debug(address_obj_up.dict(by_alias=False, exclude_unset=True)) try:
# log.debug(address_obj_up.dict(by_alias=False, exclude_unset=False)) address_obj = Contact_Base(**address_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
address_dict_up = address_obj_up.dict(by_alias=False, exclude_unset=True) log.debug(address_obj)
except ValidationError as e:
if address_obj_up_result := sql_update(data=address_dict_up, table_name='address', rm_id_random=True): log.error(e.json())
log.debug(address_obj_up_result) return False
return True
else: else:
log.debug(address_obj_up_result) address_obj = address_dict_obj
address_dict = address_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
# ### SECTION ### Process data
address_obj.id = address_id # Is this needed?
address_dict['address_id'] = address_id
log.debug(address_dict_obj)
# log.debug(address_dict_obj.dict(by_alias=True, exclude_unset=True))
log.debug(address_dict_obj.dict(by_alias=False, exclude_unset=True))
# log.debug(address_dict_obj.dict(by_alias=False, exclude_unset=False))
# address_dict = address_dict_obj.dict(by_alias=False, exclude_unset=True)
if address_dict_up_result := sql_update(data=address_dict, table_name='address', rm_id_random=True): pass
else:
log.warning(f'Address not updated.')
log.debug(address_dict_up_result)
return False return False
log.debug(address_dict_up_result)
return True
# ### END ### API Address Methods ### update_address_obj() ### # ### END ### API Address Methods ### update_address_obj() ###

View File

@@ -313,10 +313,10 @@ def create_update_contact_obj_v4(
# ### BEGIN ### API Contact Methods ### create_contact_obj() ### # ### BEGIN ### API Contact Methods ### create_contact_obj() ###
# NOTE: This will create a contact and then also create a linked address if contact_obj.address data is passed. # NOTE: This will create a contact and then also create a linked address if contact_obj.address data is passed.
# NOTE: In the future it should be required that account_id, for_type, and for_id should be passed separately. account_id might not be required *if* it can be looked up based on for_type and for_id. # NOTE: In the future it should be required that account_id, for_type, and for_id should be passed separately. account_id might not be required *if* it can be looked up based on for_type and for_id.
# Reviewed and updated 2021-08-24 # Updated 2022-01-06
def create_contact_obj( def create_contact_obj(
contact_obj_new: Contact_Base, account_id: int|str,
account_id: int|str = None, contact_dict_obj: Contact_Base,
for_type: str = None, for_type: str = None,
for_id: int|str = None, for_id: int|str = None,
create_sub_obj: bool = False, create_sub_obj: bool = False,
@@ -325,82 +325,128 @@ def create_contact_obj(
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())
# ### SECTION ### Secondary data validation
# NOTE: Remove at future date. Is this check needed if we trust that the ID is checked ahead of time?
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
else: return False else: return False
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
else: return False else: return False
log.debug(type(contact_obj_new)) log.info('Create dictionary or Pydantic object')
if isinstance(contact_obj_new, dict): log.debug(type(contact_dict_obj))
if account_id: if isinstance(contact_dict_obj, dict):
contact_obj_new['account_id'] = account_id contact_dict = contact_dict_obj
if for_type:
contact_obj_new['for_type'] = for_type
if for_id:
contact_obj_new['for_id'] = for_id
try: try:
contact_obj_new = Contact_Base(**contact_obj_new) contact_obj = Contact_Base(**contact_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(contact_obj)
log.debug(contact_obj_new)
except ValidationError as e: except ValidationError as e:
log.error(e.json()) log.error(e.json())
return False return False
else: else:
if account_id: contact_obj = contact_dict_obj
contact_obj_new.account_id = account_id contact_obj.account_id = account_id
if for_type:
contact_obj_new.for_type = for_type
if for_id:
contact_obj_new.for_id = for_id
contact_obj_data = contact_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'}) contact_dict = contact_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'address_id', 'address_id_random', 'created_on', 'updated_on'})
if contact_obj_in_result := sql_insert(data=contact_obj_data, table_name='contact', rm_id_random=True, id_random_length=8): pass # log.debug(type(contact_dict_obj))
# if isinstance(contact_dict_obj, dict):
# if account_id:
# contact_dict_obj['account_id'] = account_id
# if for_type:
# contact_dict_obj['for_type'] = for_type
# if for_id:
# contact_dict_obj['for_id'] = for_id
# try:
# contact_obj = Contact_Base(**contact_dict_obj)
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(contact_obj)
# except ValidationError as e:
# log.error(e.json())
# return False
# else:
# if account_id:
# contact_obj.account_id = account_id
# if for_type:
# contact_obj.for_type = for_type
# if for_id:
# contact_obj.for_id = for_id
# contact_dict = contact_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'})
# ### SECTION ### Process data
contact_obj.account_id = account_id
contact_obj.for_type = for_type
contact_obj.for_id = for_id
contact_dict['account_id'] = account_id
contact_dict['for_type'] = for_type
contact_dict['for_id'] = for_id
if contact_dict_in_result := sql_insert(data=contact_dict, table_name='contact', rm_id_random=True, id_random_length=8): pass
else: else:
return False return False
log.debug(contact_obj_in_result) log.debug(contact_dict_in_result)
contact_id = contact_dict_in_result
contact_id = contact_obj_in_result if address_id and contact.address:
log.info('Updating Address object')
if contact_obj_new.address: if address_update_result := update_address_obj(
address_obj_new = contact_obj_new.address address_id = address_id,
address_obj_new.for_type = 'contact' address_dict_obj = contact_obj.address,
address_obj_new.for_id = contact_id
create_address_obj_result = create_address_obj(
address_obj_new = address_obj_new,
account_id = account_id,
for_type = 'contact', for_type = 'contact',
for_id = contact_id, for_id = contact_id,
fail_any = fail_any, ): pass
) else: return False
if isinstance(create_address_obj_result, int): elif contact.address:
address_id = create_address_obj_result log.info('Creating Address object')
# NOTE: This last update should no longer be needed now that the contact.address_id is not supposed to be used. if address_create_result := create_address_obj(
# Need to update the contact with the new address_id account_id = account_id,
contact_obj_up = {} # REMOVE address_dict_obj = contact_obj.address,
contact_obj_up['id'] = contact_id # REMOVE for_type = 'contact',
contact_obj_up['address_id'] = address_id # REMOVE for_id = contact_id,
if contact_obj_up_result := sql_update(data=contact_obj_up, table_name='contact'): pass # REMOVE ): pass
else: # REMOVE else: return False
return False # REMOVE else: pass
log.debug(contact_obj_up_result) # REMOVE
else: # if contact_obj.address:
log.debug(f'No address_id was returned when tyring to create_address_obj(): {create_address_obj_result}') # address_obj_new = contact_obj.address
address_id = None # address_obj_new.for_type = 'contact'
# address_obj_new.for_id = contact_id
# create_address_obj_result = create_address_obj(
# address_obj_new = address_obj_new,
# account_id = account_id,
# for_type = 'contact',
# for_id = contact_id,
# fail_any = fail_any,
# )
# if isinstance(create_address_obj_result, int):
# address_id = create_address_obj_result
# # NOTE: This last update should no longer be needed now that the contact.address_id is not supposed to be used.
# # Need to update the contact with the new address_id
# contact_obj_up = {} # REMOVE
# contact_obj_up['id'] = contact_id # REMOVE
# contact_obj_up['address_id'] = address_id # REMOVE
# if contact_obj_up_result := sql_update(data=contact_obj_up, table_name='contact'): pass # REMOVE
# else: # REMOVE
# return False # REMOVE
# log.debug(contact_obj_up_result) # REMOVE
# else:
# log.debug(f'No address_id was returned when tyring to create_address_obj(): {create_address_obj_result}')
# address_id = None
log.info(f'Returning the new contact_id: {contact_id}')
log.debug(f'Returning the new contact_id: {contact_id}')
return contact_id return contact_id
# ### END ### API Contact Methods ### create_contact_obj() ### # ### END ### API Contact Methods ### create_contact_obj() ###
# ### BEGIN ### API Contact Methods ### update_contact_obj() ### # ### BEGIN ### API Contact Methods ### update_contact_obj() ###
# NOTE: This will update a contact and then also create or update a linked address if contact_obj.address data is passed. # NOTE: This will update a contact and then also create or update a linked address if contact_obj.address data is passed.
# Reviewed and updated 2021-08-10 # Updated 2022-01-06
def update_contact_obj( def update_contact_obj(
contact_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value. contact_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
contact_obj_up: Contact_Base, contact_dict_obj: Contact_Base,
create_sub_obj: bool = False, create_sub_obj: bool = False,
fail_any: bool = False, # Fail if any thing goes wrong for sub objects fail_any: bool = False, # Fail if any thing goes wrong for sub objects
return_dict: bool = False, return_dict: bool = False,
@@ -408,80 +454,128 @@ def update_contact_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
# NOTE: Remove at future date. Is this check needed if we trust that the ID is checked ahead of time?
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
else: return False else: return False
log.info('Create dictionary or Pydantic object')
log.debug(type(contact_dict_obj))
if isinstance(contact_dict_obj, dict):
contact_dict = contact_dict_obj
try:
contact_obj = Contact_Base(**contact_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(contact_obj)
except ValidationError as e:
log.error(e.json())
return False
else:
contact_obj = contact_dict_obj
contact_dict = contact_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'address_id', 'address_id_random', 'created_on', 'updated_on'})
# ### SECTION ### Process data
contact_obj.id = contact_id # Is this needed?
contact_dict['contact_id'] = contact_id
account_id = get_account_id_w_contact_id(contact_id) account_id = get_account_id_w_contact_id(contact_id)
contact_obj_up.id = contact_id if contact_dict_up_result := sql_update(data=contact_dict, table_name='contact', rm_id_random=True): pass
else:
log.warning(f'Contact not updated.')
log.debug(contact_dict_up_result)
return False
log.debug(contact_obj_up) log.debug(contact_dict_up_result)
if address_id and contact.address:
log.info('Updating Address object')
if address_update_result := update_address_obj(
address_id = address_id,
address_dict_obj = contact_obj.address,
for_type = 'contact',
for_id = contact_id,
): pass
else: return False
elif contact.address:
log.info('Creating Address object')
if address_create_result := create_address_obj(
account_id = account_id,
address_dict_obj = contact_obj.address,
for_type = 'contact',
for_id = contact_id,
): pass
else: return False
else: pass
# log.debug(contact_obj_up)
# log.debug(contact_obj_up.dict(by_alias=True, exclude_unset=True)) # log.debug(contact_obj_up.dict(by_alias=True, exclude_unset=True))
log.debug(contact_obj_up.dict(by_alias=False, exclude_unset=True)) log.debug(contact_obj.dict(by_alias=False, exclude_unset=True))
# log.debug(contact_obj_up.dict(by_alias=False, exclude_unset=False)) # log.debug(contact_obj_up.dict(by_alias=False, exclude_unset=False))
#contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True) #contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True)
if contact_obj_up.address: # if contact_obj_up.address:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
address_obj_unknown = contact_obj_up.address # address_obj_unknown = contact_obj_up.address
log.debug(address_obj_unknown) # log.debug(address_obj_unknown)
if isinstance(contact_obj_up.address, dict): # if isinstance(contact_obj_up.address, dict):
address_id = address_obj_unknown.get('address_id_random', None) # address_id = address_obj_unknown.get('address_id_random', None)
else: # else:
address_id = address_obj_unknown.id # address_id = address_obj_unknown.id
if address_id: # if address_id:
# from app.methods.address_methods import update_address_obj_v3 # # from app.methods.address_methods import update_address_obj_v3
if update_address_obj_result := update_address_obj( # if update_address_obj_result := update_address_obj(
address_id = address_id, # address_id = address_id,
address_obj_up = address_obj_unknown, # address_obj_up = address_obj_unknown,
create_sub_obj = create_sub_obj, # create_sub_obj = create_sub_obj,
fail_any = fail_any, # fail_any = fail_any,
): # ):
address_id = update_address_obj_result # address_id = update_address_obj_result
log.info(f'Address updated. Address ID: {address_id}') # log.info(f'Address updated. Address ID: {address_id}')
else: # else:
log.warning(f'Address not updated. Contact ID: {contact_id}') # log.warning(f'Address not updated. Contact ID: {contact_id}')
log.debug(update_address_obj_result) # log.debug(update_address_obj_result)
address_id = None # address_id = None
if fail_any: return False # if fail_any: return False
if isinstance(update_address_obj_result, int): # if isinstance(update_address_obj_result, int):
address_id = update_address_obj_result # address_id = update_address_obj_result
log.info(f'Address updated. Address ID: {address_id}') # log.info(f'Address updated. Address ID: {address_id}')
else: # else:
log.warning(f'Address not updated. Contact ID: {contact_id}') # log.warning(f'Address not updated. Contact ID: {contact_id}')
log.debug(update_address_obj_result) # log.debug(update_address_obj_result)
address_id = None # address_id = None
if fail_any: return False # if fail_any: return False
else: # else:
log.info(f'No Address ID found.') # log.info(f'No Address ID found.')
# from app.methods.address_methods import create_address_obj_v3 # # from app.methods.address_methods import create_address_obj_v3
if create_address_obj_result := create_address_obj( # if create_address_obj_result := create_address_obj(
account_id = account_id, # account_id = account_id,
for_type = 'contact', # for_type = 'contact',
for_id = contact_id, # for_id = contact_id,
address_obj_new = address_obj_unknown, # address_obj_new = address_obj_unknown,
fail_any = fail_any, # fail_any = fail_any,
): # ):
if isinstance(create_address_obj_result, int): # if isinstance(create_address_obj_result, int):
address_id = create_address_obj_result # address_id = create_address_obj_result
log.info(f'Address created. Address ID: {address_id}') # log.info(f'Address created. Address ID: {address_id}')
else: # else:
log.warning(f'Address not created. Contact ID: {contact_id}') # log.warning(f'Address not created. Contact ID: {contact_id}')
log.debug(create_address_obj_result) # log.debug(create_address_obj_result)
address_id = None # address_id = None
if fail_any: return False # if fail_any: return False
else: # else:
log.warning(f'Address not created. Contact ID: {contact_id}') # log.warning(f'Address not created. Contact ID: {contact_id}')
log.debug(create_address_obj_result) # log.debug(create_address_obj_result)
address_id = None # address_id = None
if fail_any: return False # if fail_any: return False
# return_dict['address_id'] = address_id # # return_dict['address_id'] = address_id
else: # else:
log.info('Address not found or not in a dict.') # log.info('Address not found or not in a dict.')
pass # pass
@@ -521,15 +615,17 @@ def update_contact_obj(
contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'address'}) # contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'address'})
log.debug(contact_dict_up) # log.debug(contact_dict_up)
if contact_obj_up_result := sql_update(data=contact_dict_up, table_name='contact', rm_id_random=True): # if contact_obj_up_result := sql_update(data=contact_dict_up, table_name='contact', rm_id_random=True):
log.debug(contact_obj_up_result) # log.debug(contact_obj_up_result)
return True # return True
else: # else:
log.debug(contact_obj_up_result) # log.debug(contact_obj_up_result)
return False # return False
return True
# ### END ### API Contact Methods ### update_contact_obj() ### # ### END ### API Contact Methods ### update_contact_obj() ###

View File

@@ -138,10 +138,10 @@ def update_organization_obj(
log.debug(contact_id) log.debug(contact_id)
log.debug(contact_obj_up) log.debug(contact_obj_up)
if contact_obj_up_result := update_contact_obj( if contact_obj_up_result := update_contact_obj(
contact_id=contact_id, contact_id = contact_id,
contact_obj_up=contact_obj_up, contact_dict_obj = contact_obj_up,
create_sub_obj=create_sub_obj, create_sub_obj = create_sub_obj,
): ):
log.debug(contact_obj_up_result) log.debug(contact_obj_up_result)
else: else:
log.debug(contact_obj_up_result) log.debug(contact_obj_up_result)
@@ -150,7 +150,10 @@ def update_organization_obj(
# NOTE: This will blindly create a new contact even if there was one associated but the organization.contact_id was not found. # NOTE: This will blindly create a new contact even if there was one associated but the organization.contact_id was not found.
contact_obj_in = organization_obj_up.contact contact_obj_in = organization_obj_up.contact
log.debug(contact_obj_in) log.debug(contact_obj_in)
if contact_obj_in_result := create_contact_obj(contact_obj_new=contact_obj_in): if contact_obj_in_result := create_contact_obj(
account_id = contact_obj_in.account_id,
contact_dict_obj=contact_obj_in,
):
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(contact_obj_in_result) log.debug(contact_obj_in_result)
organization_obj_up.contact_id = contact_obj_in_result organization_obj_up.contact_id = contact_obj_in_result

View File

@@ -448,7 +448,7 @@ def get_person_rec_list(
# ### BEGIN ### API Person Methods ### create_person_kiss() ### # ### BEGIN ### API Person Methods ### create_person_kiss() ###
# Updated 2022-01-05 # Updated 2022-01-06
def create_person_kiss( def create_person_kiss(
account_id: int, account_id: int,
person_dict_obj: Person_Base, person_dict_obj: Person_Base,
@@ -495,7 +495,7 @@ def create_person_kiss(
log.debug(person_dict_in_result) log.debug(person_dict_in_result)
person_id = person_dict_in_result person_id = person_dict_in_result
if contact_id and person.contact: if contact_id and person_obj.contact:
log.info('Updating Contact object') log.info('Updating Contact object')
if contact_update_result := update_contact_obj( if contact_update_result := update_contact_obj(
contact_id = contact_id, contact_id = contact_id,
@@ -504,7 +504,7 @@ def create_person_kiss(
for_id = person_id, for_id = person_id,
): pass ): pass
else: return False else: return False
elif person.contact: elif person_obj.contact:
log.info('Creating Contact object') log.info('Creating Contact object')
if contact_create_result := create_contact_obj( if contact_create_result := create_contact_obj(
account_id = account_id, account_id = account_id,
@@ -515,7 +515,7 @@ def create_person_kiss(
else: return False else: return False
else: pass else: pass
if user_id and person.user: if user_id and person_obj.user:
log.info('Updating User object') log.info('Updating User object')
if user_update_result := update_user_obj( if user_update_result := update_user_obj(
user_id = user_id, user_id = user_id,
@@ -523,7 +523,7 @@ def create_person_kiss(
person_id = person_id, person_id = person_id,
): pass # NOTE: There is a trigger that will update the person record with the new user 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_obj.user:
log.info('Creating User object') log.info('Creating User object')
if user_create_result := create_user_obj( if user_create_result := create_user_obj(
account_id = account_id, account_id = account_id,
@@ -540,7 +540,7 @@ def create_person_kiss(
# ### BEGIN ### API Person Methods ### update_person_kiss() ### # ### BEGIN ### API Person Methods ### update_person_kiss() ###
# Updated 2022-01-05 # Updated 2022-01-06
def update_person_kiss( def update_person_kiss(
person_id: int, person_id: int,
person_dict_obj: Person_Base, person_dict_obj: Person_Base,
@@ -569,7 +569,7 @@ def update_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 # ### SECTION ### Process data
person_obj.person_id = person_id # Is this needed? person_obj.id = person_id # Is this needed?
person_dict['person_id'] = person_id person_dict['person_id'] = person_id
if user_id: if user_id:
@@ -586,7 +586,7 @@ def update_person_kiss(
log.debug(person_dict_up_result) log.debug(person_dict_up_result)
if contact_id and person.contact: if contact_id and person_obj.contact:
log.info('Updating Contact object') log.info('Updating Contact object')
if contact_update_result := update_contact_obj( if contact_update_result := update_contact_obj(
contact_id = contact_id, contact_id = contact_id,
@@ -595,7 +595,7 @@ def update_person_kiss(
for_id = person_id, for_id = person_id,
): pass ): pass
else: return False else: return False
elif person.contact: elif person_obj.contact:
log.info('Creating Contact object') log.info('Creating Contact object')
if contact_create_result := create_contact_obj( if contact_create_result := create_contact_obj(
account_id = account_id, account_id = account_id,
@@ -606,7 +606,7 @@ def update_person_kiss(
else: return False else: return False
else: pass else: pass
if user_id and person.user: if user_id and person_obj.user:
log.info('Updating User object') log.info('Updating User object')
if user_update_result := update_user_obj( if user_update_result := update_user_obj(
user_id = user_id, user_id = user_id,
@@ -614,7 +614,7 @@ def update_person_kiss(
person_id = person_id, person_id = person_id,
): pass # NOTE: There is a trigger that will update the person record with the new user 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_obj.user:
log.info('Creating User object') log.info('Creating User object')
if user_create_result := create_user_obj( if user_create_result := create_user_obj(
account_id = account_id, account_id = account_id,
@@ -1495,10 +1495,10 @@ def update_person_obj(
log.debug(contact_id) log.debug(contact_id)
log.debug(contact_obj_up) log.debug(contact_obj_up)
if contact_obj_up_result := update_contact_obj( if contact_obj_up_result := update_contact_obj(
contact_id = contact_id, contact_id = contact_id,
contact_obj_up = contact_obj_up, contact_dict_obj = contact_obj_up,
create_sub_obj = create_sub_obj, create_sub_obj = create_sub_obj,
): ):
log.debug(contact_obj_up_result) log.debug(contact_obj_up_result)
else: else:
log.debug(contact_obj_up_result) log.debug(contact_obj_up_result)
@@ -1507,7 +1507,10 @@ def update_person_obj(
# NOTE: This will blindly create a new contact even if there was one associated but the person.contact_id was not found. # NOTE: This will blindly create a new contact even if there was one associated but the person.contact_id was not found.
contact_obj_in = person_obj_up.contact contact_obj_in = person_obj_up.contact
log.debug(contact_obj_in) log.debug(contact_obj_in)
if contact_obj_in_result := create_contact_obj(contact_obj_new=contact_obj_in): if contact_obj_in_result := create_contact_obj(
account_id = contact_obj_in.account_id,
contact_dict_obj = contact_obj_in,
):
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(contact_obj_in_result) log.debug(contact_obj_in_result)
# NOTE: This last update should no longer be needed now that the person.contact_id is not supposed to be used. # NOTE: This last update should no longer be needed now that the person.contact_id is not supposed to be used.

View File

@@ -23,9 +23,7 @@ from app.models.user_models import User_Base, User_New_Base, User_Out_Base
# ### BEGIN ### API User Methods ### create_user_obj() ### # ### BEGIN ### API User Methods ### create_user_obj() ###
# NOTE: This will create a new user and also hash a new password string if given. # NOTE: This will create a new user and also hash a new password string if given.
# NOTE: This uses the User_New_Base model, not User_Base or User_Out_Base # NOTE: This uses the User_New_Base model, not User_Base or User_Out_Base
# Updated 2021-08-25 # Updated 2022-01-06
# Reviewed and updated 2021-08-21
# Reviewed and updated 2021-08-10
def create_user_obj( def create_user_obj(
account_id: int|str, account_id: int|str,
user_dict_obj: User_New_Base, user_dict_obj: User_New_Base,
@@ -49,7 +47,7 @@ def create_user_obj(
if isinstance(user_dict_obj, dict): if isinstance(user_dict_obj, dict):
user_dict = user_dict_obj user_dict = user_dict_obj
try: try:
user_obj = Person_Base(**user_dict) user_obj = User_New_Base(**user_dict)
log.debug(user_obj) log.debug(user_obj)
except ValidationError as e: except ValidationError as e:
log.error(e.json()) log.error(e.json())
@@ -167,8 +165,7 @@ def update_user_obj(
if isinstance(user_dict_obj, dict): if isinstance(user_dict_obj, dict):
user_dict = user_dict_obj user_dict = user_dict_obj
try: try:
user_obj = Person_Base(**user_dict) user_obj = User_Base(**user_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(user_obj) log.debug(user_obj)
except ValidationError as e: except ValidationError as e:
log.error(e.json()) log.error(e.json())
@@ -180,19 +177,19 @@ def update_user_obj(
user_dict = user_obj.dict(by_alias=False, exclude_unset=True, exclude={'password', 'super', 'manager', 'contact', 'organization', 'person', 'created_on', 'updated_on'}) user_dict = user_obj.dict(by_alias=False, exclude_unset=True, exclude={'password', 'super', 'manager', 'contact', 'organization', 'person', 'created_on', 'updated_on'})
log.debug(type(user_dict_obj)) # log.debug(type(user_dict_obj))
if isinstance(user_dict_obj, dict): # if isinstance(user_dict_obj, dict):
try: # try:
user_obj = User_Base(**user_dict_obj) # user_obj = User_Base(**user_dict_obj)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(user_obj) # log.debug(user_obj)
except ValidationError as e: # except ValidationError as e:
log.error(e.json()) # log.error(e.json())
return False # return False
user_obj.id = user_id # user_obj.id = user_id
log.debug(user_obj) # log.debug(user_obj)
# log.debug(user_obj.dict(by_alias=True, exclude_unset=True)) # log.debug(user_obj.dict(by_alias=True, exclude_unset=True))
log.debug(user_obj.dict(by_alias=False, exclude_unset=True)) log.debug(user_obj.dict(by_alias=False, exclude_unset=True))
# log.debug(user_obj.dict(by_alias=False, exclude_unset=False)) # log.debug(user_obj.dict(by_alias=False, exclude_unset=False))
@@ -204,7 +201,7 @@ def update_user_obj(
# log.debug(contact_obj_up) # log.debug(contact_obj_up)
# if contact_obj_up_result := update_contact_obj( # if contact_obj_up_result := update_contact_obj(
# contact_id=contact_id, # contact_id=contact_id,
# contact_obj_up=contact_obj_up, # contact_dict_obj=contact_obj_up,
# create_sub_obj=create_sub_obj, # create_sub_obj=create_sub_obj,
# ): # ):
# log.debug(contact_obj_up_result) # log.debug(contact_obj_up_result)
@@ -215,7 +212,7 @@ def update_user_obj(
# # NOTE: This will blindly create a new contact even if there was one associated but the user.contact_id was not found. # # NOTE: This will blindly create a new contact even if there was one associated but the user.contact_id was not found.
# contact_obj_in = user_obj.contact # contact_obj_in = user_obj.contact
# log.debug(contact_obj_in) # log.debug(contact_obj_in)
# if contact_obj_in_result := create_contact_obj(contact_obj_new=contact_obj_in): # if contact_obj_in_result := create_contact_obj(contact_dict_obj=contact_obj_in):
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(contact_obj_in_result) # log.debug(contact_obj_in_result)
# user_obj.contact_id = contact_obj_in_result # user_obj.contact_id = contact_obj_in_result
@@ -276,7 +273,7 @@ def update_user_obj(
# log.debug(user_dict) # log.debug(user_dict)
# ### SECTION ### Process data # ### SECTION ### Process data
user_obj.user_id = user_id # Is this needed? user_obj.id = user_id # Is this needed?
user_dict['user_id'] = user_id user_dict['user_id'] = user_id
if person_id: if person_id:

View File

@@ -198,7 +198,7 @@ async def patch_contact_json(
if contact_obj_up_result := update_contact_obj( if contact_obj_up_result := update_contact_obj(
contact_id=contact_id, contact_id=contact_id,
contact_obj_up=contact_obj, contact_dict_obj=contact_obj,
create_sub_obj=create_sub_obj, create_sub_obj=create_sub_obj,
): ):