Code clean up. Bug fixes for person, user, contact, and address methods
This commit is contained in:
@@ -319,6 +319,7 @@ def create_contact_obj(
|
||||
contact_dict_obj: Contact_Base,
|
||||
for_type: str = None,
|
||||
for_id: int|str = None,
|
||||
address_id: int = None,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
) -> int|bool:
|
||||
@@ -333,6 +334,10 @@ def create_contact_obj(
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
|
||||
else: return False
|
||||
|
||||
if address_id := redis_lookup_id_random(record_id_random=address_id, table_name='address'): pass
|
||||
elif address_id is None: pass
|
||||
else: return False
|
||||
|
||||
log.info('Create dictionary or Pydantic object')
|
||||
log.debug(type(contact_dict_obj))
|
||||
if isinstance(contact_dict_obj, dict):
|
||||
@@ -382,6 +387,8 @@ def create_contact_obj(
|
||||
contact_dict['for_type'] = for_type
|
||||
contact_dict['for_id'] = for_id
|
||||
|
||||
# if not address_id and contact_obj.address_id_random:
|
||||
|
||||
if contact_dict_in_result := sql_insert(data=contact_dict, table_name='contact', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
return False
|
||||
@@ -389,7 +396,7 @@ def create_contact_obj(
|
||||
log.debug(contact_dict_in_result)
|
||||
contact_id = contact_dict_in_result
|
||||
|
||||
if address_id and contact.address:
|
||||
if address_id and contact_obj.address:
|
||||
log.info('Updating Address object')
|
||||
if address_update_result := update_address_obj(
|
||||
address_id = address_id,
|
||||
@@ -398,7 +405,7 @@ def create_contact_obj(
|
||||
for_id = contact_id,
|
||||
): pass
|
||||
else: return False
|
||||
elif contact.address:
|
||||
elif contact_obj.address:
|
||||
log.info('Creating Address object')
|
||||
if address_create_result := create_address_obj(
|
||||
account_id = account_id,
|
||||
@@ -422,7 +429,7 @@ def create_contact_obj(
|
||||
# )
|
||||
# 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.
|
||||
# # NOTE: This last update should no longer be needed now that the contact_obj.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
|
||||
@@ -447,6 +454,7 @@ def create_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_dict_obj: Contact_Base,
|
||||
address_id: int = None,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
return_dict: bool = False,
|
||||
@@ -459,6 +467,10 @@ def update_contact_obj(
|
||||
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||
else: return False
|
||||
|
||||
if address_id := redis_lookup_id_random(record_id_random=address_id, table_name='address'): pass
|
||||
elif address_id is None: pass
|
||||
else: return False
|
||||
|
||||
log.info('Create dictionary or Pydantic object')
|
||||
log.debug(type(contact_dict_obj))
|
||||
if isinstance(contact_dict_obj, dict):
|
||||
@@ -488,7 +500,7 @@ def update_contact_obj(
|
||||
|
||||
log.debug(contact_dict_up_result)
|
||||
|
||||
if address_id and contact.address:
|
||||
if address_id and contact_obj.address:
|
||||
log.info('Updating Address object')
|
||||
if address_update_result := update_address_obj(
|
||||
address_id = address_id,
|
||||
@@ -497,7 +509,7 @@ def update_contact_obj(
|
||||
for_id = contact_id,
|
||||
): pass
|
||||
else: return False
|
||||
elif contact.address:
|
||||
elif contact_obj.address:
|
||||
log.info('Creating Address object')
|
||||
if address_create_result := create_address_obj(
|
||||
account_id = account_id,
|
||||
@@ -593,7 +605,7 @@ def update_contact_obj(
|
||||
# log.debug(address_obj_up_result)
|
||||
# return False
|
||||
# elif contact_obj_up.address and not contact_obj_up.address.id:
|
||||
# # NOTE: This will blindly create a new address even if there was one associated but the contact.address_id was not found.
|
||||
# # NOTE: This will blindly create a new address even if there was one associated but the contact_obj.address_id was not found.
|
||||
# address_obj_in = contact_obj_up.address
|
||||
# log.debug(address_obj_in)
|
||||
# if address_obj_in_result := create_address_obj(
|
||||
@@ -604,7 +616,7 @@ def update_contact_obj(
|
||||
# ):
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(address_obj_in_result)
|
||||
# # NOTE: This last update should no longer be needed now that the contact.address_id is not supposed to be used.
|
||||
# # NOTE: This last update should no longer be needed now that the contact_obj.address_id is not supposed to be used.
|
||||
# # Need to update the contact with the new address_id
|
||||
# contact_obj_up.address_id = address_obj_in_result # REMOVE
|
||||
# else:
|
||||
|
||||
Reference in New Issue
Block a user