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() ###
# NOTE: This will create an address.
# Reviewed and updated 2021-08-10
# Updated 2022-01-06
def create_address_obj(
address_obj_new: Address_Base,
account_id: int|str = None,
account_id: int|str,
address_dict_obj: Address_Base,
for_type: str = None,
for_id: int|str = None,
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.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
else: return False
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
else: return False
log.debug(type(address_obj_new))
if isinstance(address_obj_new, 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
log.info('Create dictionary or Pydantic object')
log.debug(type(address_dict_obj))
if isinstance(address_dict_obj, dict):
address_dict = address_dict_obj
try:
address_obj_new = Address_Base(**address_obj_new)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(address_obj_new)
address_obj = Contact_Base(**address_dict)
log.debug(address_obj)
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_obj = address_dict_obj
address_obj.account_id = account_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:
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
# ### END ### API Address Methods ### create_address_obj() ###
# ### BEGIN ### API Address Methods ### update_address_obj() ###
# NOTE: This will update an address.
# Reviewed and updated 2021-08-10
# Updated 2022-01-06
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_obj_up: Address_Base,
address_dict_obj: Address_Base,
create_sub_obj: bool = False,
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
) -> bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
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
else: return False
address_obj_up.id = address_id
log.debug(address_obj_up)
# log.debug(address_obj_up.dict(by_alias=True, exclude_unset=True))
log.debug(address_obj_up.dict(by_alias=False, exclude_unset=True))
# log.debug(address_obj_up.dict(by_alias=False, exclude_unset=False))
address_dict_up = address_obj_up.dict(by_alias=False, exclude_unset=True)
if address_obj_up_result := sql_update(data=address_dict_up, table_name='address', rm_id_random=True):
log.debug(address_obj_up_result)
return True
log.info('Create dictionary or Pydantic object')
log.debug(type(address_dict_obj))
if isinstance(address_dict_obj, dict):
address_dict = address_dict_obj
try:
address_obj = Contact_Base(**address_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(address_obj)
except ValidationError as e:
log.error(e.json())
return False
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
log.debug(address_dict_up_result)
return True
# ### END ### API Address Methods ### update_address_obj() ###