From 29c6770581e6f9aa784eadebfb9ea63accd82b6f Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Thu, 6 Jan 2022 18:34:53 -0500 Subject: [PATCH] Code clean up. Bug fixes for person, user, contact, and address methods. There is a bug related get_account_id_w_for_type_id() --- app/methods/address_methods.py | 20 +++++++++++++- app/methods/contact_methods.py | 28 ++++++++++++++++--- app/methods/person_methods.py | 49 ++++++++++++++++++++++++++-------- app/methods/user_methods.py | 12 +++++++++ 4 files changed, 94 insertions(+), 15 deletions(-) diff --git a/app/methods/address_methods.py b/app/methods/address_methods.py index 5943967..c06eb22 100644 --- a/app/methods/address_methods.py +++ b/app/methods/address_methods.py @@ -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'}) # ### 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_dict['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 @@ -309,6 +314,8 @@ def create_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_dict_obj: Address_Base, + for_type: str = None, + for_id: int|str = None, create_sub_obj: bool = False, fail_any: bool = False, # Fail if any thing goes wrong for sub objects ) -> bool: @@ -339,6 +346,17 @@ def update_address_obj( address_obj.id = address_id # Is this needed? 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.dict(by_alias=True, exclude_unset=True)) log.debug(address_dict_obj.dict(by_alias=False, exclude_unset=True)) diff --git a/app/methods/contact_methods.py b/app/methods/contact_methods.py index dc0432d..ae7117a 100644 --- a/app/methods/contact_methods.py +++ b/app/methods/contact_methods.py @@ -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'}) # ### 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_dict['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 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 else: @@ -454,6 +461,8 @@ 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, + 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 @@ -490,7 +499,20 @@ def update_contact_obj( contact_obj.id = contact_id # Is this needed? 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 else: diff --git a/app/methods/person_methods.py b/app/methods/person_methods.py index ac1b78b..4058299 100644 --- a/app/methods/person_methods.py +++ b/app/methods/person_methods.py @@ -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'}) - # ### 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_dict['account_id'] = account_id - 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 + # Look for a contact_id in the contact_obj + if contact_id: pass + elif contact_id := person_obj.contact.id: pass + + # 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 else: @@ -517,6 +527,11 @@ def create_person_kiss( if user_id and person_obj.user: 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 if user_update_result := update_user_obj( user_id = user_id, @@ -575,11 +590,18 @@ def update_person_kiss( person_obj.id = person_id # Is this needed? person_dict['id'] = person_id - 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 + # Look for a contact_id in the contact_obj + if contact_id: pass + elif contact_id := person_obj.contact.id: pass + + # 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 else: @@ -611,6 +633,11 @@ def update_person_kiss( if user_id and person_obj.user: 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 if user_update_result := update_user_obj( user_id = user_id, diff --git a/app/methods/user_methods.py b/app/methods/user_methods.py index 68f777c..6cfefb5 100644 --- a/app/methods/user_methods.py +++ b/app/methods/user_methods.py @@ -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'}) # ### 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_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? + # Look for a person_id in the user_obj + if person_id: pass + elif person_id := user_obj.person.id: pass + if person_id: # Link to an existing person 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) + # Look for a person_id in the user_obj + if person_id: pass + elif person_id := user_obj.person.id: pass + if person_id: # Link to an existing person log.info(f'Adding person_id to person_dict. Person ID: {person_id}')