Code clean up. Bug fixes for person, user, contact, and address methods. There is a bug related get_account_id_w_for_type_id()

This commit is contained in:
Scott Idem
2022-01-06 18:34:53 -05:00
parent 597c765673
commit 29c6770581
4 changed files with 94 additions and 15 deletions

View File

@@ -284,10 +284,15 @@ def create_address_obj(
# address_dict = address_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'}) # address_dict = address_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
# ### SECTION ### Process data # ### SECTION ### Process data
# Look for an account_id in the address_obj
if account_id: pass
elif account_id := address_obj.account_id: pass
address_obj.account_id = account_id address_obj.account_id = account_id
address_dict['account_id'] = account_id
address_obj.for_type = for_type address_obj.for_type = for_type
address_obj.for_id = for_id address_obj.for_id = for_id
address_dict['account_id'] = account_id
address_dict['for_type'] = for_type address_dict['for_type'] = for_type
address_dict['for_id'] = for_id address_dict['for_id'] = for_id
@@ -309,6 +314,8 @@ def create_address_obj(
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_dict_obj: Address_Base, address_dict_obj: Address_Base,
for_type: str = None,
for_id: int|str = None,
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:
@@ -339,6 +346,17 @@ def update_address_obj(
address_obj.id = address_id # Is this needed? address_obj.id = address_id # Is this needed?
address_dict['id'] = address_id address_dict['id'] = address_id
# Look for an account_id in the address_obj
if account_id := address_obj.account_id: pass
elif account_id := get_account_id_w_for_type_id(for_type='address', for_id=address_id): pass
# If for_type and for_id are passed then the address needs to be updated
if for_type and for_id:
address_obj.for_type = for_type
address_obj.for_id = for_id
address_dict['for_type'] = for_type
address_dict['for_id'] = for_id
log.debug(address_dict_obj) 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=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=True))

View File

@@ -380,14 +380,21 @@ def create_contact_obj(
# contact_dict = contact_obj.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', 'created_on', 'updated_on'})
# ### SECTION ### Process data # ### SECTION ### Process data
# Look for an account_id in the contact_obj
if account_id: pass
elif account_id := contact_obj.account_id: pass
contact_obj.account_id = account_id contact_obj.account_id = account_id
contact_dict['account_id'] = account_id
contact_obj.for_type = for_type contact_obj.for_type = for_type
contact_obj.for_id = for_id contact_obj.for_id = for_id
contact_dict['account_id'] = account_id
contact_dict['for_type'] = for_type contact_dict['for_type'] = for_type
contact_dict['for_id'] = for_id contact_dict['for_id'] = for_id
# if not address_id and contact_obj.address_id_random: # Look for an address_id in the contact_obj if one was not passed separately
if address_id: pass
# elif address_id := contact_obj.address.id: pass
if contact_dict_in_result := sql_insert(data=contact_dict, table_name='contact', rm_id_random=True, id_random_length=8): pass if contact_dict_in_result := sql_insert(data=contact_dict, table_name='contact', rm_id_random=True, id_random_length=8): pass
else: else:
@@ -454,6 +461,8 @@ def create_contact_obj(
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_dict_obj: Contact_Base, contact_dict_obj: Contact_Base,
for_type: str = None,
for_id: int|str = None,
address_id: int = None, address_id: int = None,
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
@@ -490,7 +499,20 @@ def update_contact_obj(
contact_obj.id = contact_id # Is this needed? contact_obj.id = contact_id # Is this needed?
contact_dict['id'] = contact_id contact_dict['id'] = contact_id
account_id = get_account_id_w_contact_id(contact_id) # Look for an account_id in the contact_obj
if account_id := contact_obj.account_id: pass
elif account_id := get_account_id_w_for_type_id(for_type='contact', for_id=contact_id): pass
# If for_type and for_id are passed then the contact needs to be updated
if for_type and for_id:
contact_obj.for_type = for_type
contact_obj.for_id = for_id
contact_dict['for_type'] = for_type
contact_dict['for_id'] = for_id
# Look for an address_id in the contact_obj if one was not passed separately
if address_id: pass
elif address_id := contact_obj.address.id: pass
if contact_dict_up_result := sql_update(data=contact_dict, table_name='contact', rm_id_random=True): pass if contact_dict_up_result := sql_update(data=contact_dict, table_name='contact', rm_id_random=True): pass
else: else:

View File

@@ -476,15 +476,25 @@ def create_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# Look for an account_id in the user_obj
if account_id: pass
elif account_id := person_obj.account_id: pass
person_obj.account_id = account_id # Is this needed? person_obj.account_id = account_id # Is this needed?
person_dict['account_id'] = account_id person_dict['account_id'] = account_id
if user_id: # Look for a contact_id in the contact_obj
# Link to an existing user if contact_id: pass
log.info(f'Adding user_id to person_dict. User ID: {user_id}') elif contact_id := person_obj.contact.id: pass
person_obj.user_id = user_id # Is this needed?
person_dict['user_id'] = user_id # Look for a user_id in the person_obj
if user_id: pass
elif user_id := person_obj.user.id: pass
# if user_id:
# # Link to an existing user
# log.info(f'Adding user_id to person_dict. User ID: {user_id}')
# person_obj.user_id = user_id # Is this needed?
# person_dict['user_id'] = user_id
if person_dict_in_result := sql_insert(data=person_dict, table_name='person', rm_id_random=True, id_random_length=default_num_bytes): pass 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: else:
@@ -517,6 +527,11 @@ def create_person_kiss(
if user_id and person_obj.user: if user_id and person_obj.user:
log.info('Updating User object') log.info('Updating User object')
# Link to an existing user
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
person_obj.user_id = user_id # Is this needed?
person_dict['user_id'] = user_id
from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function
if user_update_result := update_user_obj( if user_update_result := update_user_obj(
user_id = user_id, user_id = user_id,
@@ -575,11 +590,18 @@ def update_person_kiss(
person_obj.id = person_id # Is this needed? person_obj.id = person_id # Is this needed?
person_dict['id'] = person_id person_dict['id'] = person_id
if user_id: # Look for a contact_id in the contact_obj
# Link to an existing user if contact_id: pass
log.info(f'Adding user_id to person_dict. User ID: {user_id}') elif contact_id := person_obj.contact.id: pass
person_obj.user_id = user_id # Is this needed?
person_dict['user_id'] = user_id # Look for a user_id in the person_obj
if user_id: pass
elif user_id := person_obj.user.id: pass
# if user_id:
# # Link to an existing user
# log.info(f'Adding user_id to person_dict. User ID: {user_id}')
# person_obj.user_id = user_id # Is this needed?
# person_dict['user_id'] = user_id
if person_dict_up_result := sql_update(data=person_dict, table_name='person', rm_id_random=True, id_random_length=default_num_bytes): pass if person_dict_up_result := sql_update(data=person_dict, table_name='person', rm_id_random=True, id_random_length=default_num_bytes): pass
else: else:
@@ -611,6 +633,11 @@ def update_person_kiss(
if user_id and person_obj.user: if user_id and person_obj.user:
log.info('Updating User object') log.info('Updating User object')
# Link to an existing user
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
person_obj.user_id = user_id # Is this needed?
person_dict['user_id'] = user_id
from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function
if user_update_result := update_user_obj( if user_update_result := update_user_obj(
user_id = user_id, user_id = user_id,

View File

@@ -63,6 +63,10 @@ def create_user_obj(
user_dict = user_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id_random', 'new_password', 'organization', 'person', 'person_id_random', 'created_on', 'updated_on'}) user_dict = user_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id_random', 'new_password', 'organization', 'person', 'person_id_random', 'created_on', 'updated_on'})
# ### SECTION ### Process data # ### SECTION ### Process data
# Look for an account_id in the user_obj
if account_id: pass
elif account_id := user_obj.account_id: pass
user_obj.account_id = account_id # Is this needed? user_obj.account_id = account_id # Is this needed?
user_dict['account_id'] = account_id user_dict['account_id'] = account_id
@@ -78,6 +82,10 @@ def create_user_obj(
# user_dict['password'] = user_obj.password # There has to be a better way to do this??? It thinks "password" is unset and so is excluded? # user_dict['password'] = user_obj.password # There has to be a better way to do this??? It thinks "password" is unset and so is excluded?
# Look for a person_id in the user_obj
if person_id: pass
elif person_id := user_obj.person.id: pass
if person_id: if person_id:
# Link to an existing person # Link to an existing person
log.info(f'Adding person_id to user_dict. User ID: {person_id}') log.info(f'Adding person_id to user_dict. User ID: {person_id}')
@@ -301,6 +309,10 @@ def update_user_obj(
log.debug(user_obj.new_password) log.debug(user_obj.new_password)
# Look for a person_id in the user_obj
if person_id: pass
elif person_id := user_obj.person.id: pass
if person_id: if person_id:
# Link to an existing person # Link to an existing person
log.info(f'Adding person_id to person_dict. Person ID: {person_id}') log.info(f'Adding person_id to person_dict. Person ID: {person_id}')