From 2f037290d9199e99cef8eab1060a21a4497a0691 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Mon, 16 Aug 2021 18:15:44 -0400 Subject: [PATCH] Working on IDAA membership imports --- app/routers/importing.py | 109 +++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/app/routers/importing.py b/app/routers/importing.py index 018095a..1bf65ee 100644 --- a/app/routers/importing.py +++ b/app/routers/importing.py @@ -31,7 +31,18 @@ async def importing_person_data( log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) - account_id = 19 + allow_insert_person = True + allow_insert_contact = False + allow_insert_address = False + allow_insert_user = True + + allow_update_person = False + allow_update_contact = False + allow_update_address = False + allow_update_user = False + # allow_update_person_new_user = True + + account_id = 99 full_file_path = 'admin/temp/import_person_data.xlsx' df = pandas.read_excel(full_file_path, na_filter=False, dtype={'external_id':str, 'phone_home':str, 'phone_mobile':str, 'city':str, 'state_province':str, 'address_postal_code':str, 'country':str}) @@ -56,7 +67,10 @@ async def importing_person_data( user_id = None person_data = {} - person_data['external_id'] = record['external_id'] + if record['external_id']: + person_data['external_id'] = record['external_id'] + else: + person_data['external_id'] = record['email'] # person_data['informal_name'] = record['informal_name'] person_data['given_name'] = record['given_name'] if record['middle_name']: @@ -119,11 +133,12 @@ async def importing_person_data( address_id = person_rec.get('address_id', None) user_id = person_rec.get('user_id', None) person_data['id'] = person_id - if person_obj_up_result := sql_update(data=person_data, table_name='person'): - log.debug(person_obj_up_result) - else: - log.warning(person_obj_up_result) - continue # Something unexpected may have happened + if allow_update_person: + if person_obj_up_result := sql_update(data=person_data, table_name='person'): + log.debug(person_obj_up_result) + else: + log.warning(person_obj_up_result) + continue # Something unexpected may have happened else: log.warning('Found more than one record') log.warning(person_rec_li_result) @@ -134,13 +149,14 @@ async def importing_person_data( # INSERT new record log.debug('Found no records or something went wrong') person_data['account_id'] = account_id - if person_obj_in_result := sql_insert(data=person_data, table_name='person'): - log.debug(person_obj_in_result) - person_id = person_obj_in_result # Should be an int - person_new = True # Need to UPDATE this record after the contact, address, and user data is processed - else: - log.warning(person_obj_in_result) - continue # Something unexpected may have happened + if allow_insert_person: + if person_obj_in_result := sql_insert(data=person_data, table_name='person'): + log.debug(person_obj_in_result) + person_id = person_obj_in_result # Should be an int + person_new = True # Need to UPDATE this record after the contact, address, and user data is processed + else: + log.warning(person_obj_in_result) + continue # Something unexpected may have happened # Process the contact data log.debug('Process the contact data') @@ -178,11 +194,12 @@ async def importing_person_data( # UPDATE existing contact record log.info('UPDATE existing contact record') contact_data['id'] = contact_id - if contact_obj_up_result := sql_update(data=contact_data, table_name='contact'): - log.debug(contact_obj_up_result) - else: - log.warning(contact_obj_up_result) - continue # Something unexpected may have happened + if allow_update_contact: + if contact_obj_up_result := sql_update(data=contact_data, table_name='contact'): + log.debug(contact_obj_up_result) + else: + log.warning(contact_obj_up_result) + continue # Something unexpected may have happened elif person_id: # INSERT new contact record and link to person record log.info('INSERT new contact record and link to person record') @@ -234,11 +251,12 @@ async def importing_person_data( # UPDATE existing address record log.info('UPDATE existing address record') address_data['id'] = address_id - if address_obj_up_result := sql_update(data=address_data, table_name='address'): - log.debug(address_obj_up_result) - else: - log.warning(address_obj_up_result) - # continue # Something unexpected may have happened + if allow_update_address: + if address_obj_up_result := sql_update(data=address_data, table_name='address'): + log.debug(address_obj_up_result) + else: + log.warning(address_obj_up_result) + # continue # Something unexpected may have happened elif contact_id: # INSERT new address record and link to contact record log.info('INSERT new address record and link to contact record') @@ -282,11 +300,12 @@ async def importing_person_data( # UPDATE existing user record log.info('UPDATE existing user record') user_data['id'] = user_id - if user_obj_up_result := sql_update(data=user_data, table_name='user'): - log.debug(user_obj_up_result) - else: - log.warning(user_obj_up_result) - continue # Something unexpected may have happened + if allow_update_user: + if user_obj_up_result := sql_update(data=user_data, table_name='user'): + log.debug(user_obj_up_result) + else: + log.warning(user_obj_up_result) + continue # Something unexpected may have happened elif person_id: # INSERT new user record and link to person record log.info('INSERT new user record and link to person record') @@ -298,14 +317,15 @@ async def importing_person_data( other_data['temp_password'] = random_password_string user_data['other_json'] = json.dumps(other_data, indent=4) - if user_obj_in_result := sql_insert(data=user_data, table_name='user'): - log.debug(user_obj_in_result) - user_id = user_obj_in_result # Should be an int - person_new = True # Need to UPDATE this record after the contact, address, and user data is processed - else: - log.debug(user_obj_in_result) - # break - continue # Something unexpected may have happened + if allow_insert_user: + if user_obj_in_result := sql_insert(data=user_data, table_name='user'): + log.debug(user_obj_in_result) + user_id = user_obj_in_result # Should be an int + person_new = True # Need to UPDATE this record after the contact, address, and user data is processed + else: + log.debug(user_obj_in_result) + # break + continue # Something unexpected may have happened else: log.error('No user ID to update or person ID to create a user linked to the person.') return False @@ -315,15 +335,16 @@ async def importing_person_data( person_data_up = {} person_data_up['id'] = person_id person_data_up['user_id'] = user_id - random_password_string + # random_password_string # Don't need to update with the new contact or address IDs that were just created. - if person_obj_up_result := sql_update(data=person_data_up, table_name='person'): - log.debug(person_obj_up_result) - else: - log.warning(person_obj_up_result) - # break - continue # Something unexpected may have happened + if allow_insert_user: # Because this only matters on user INSERT + if person_obj_up_result := sql_update(data=person_data_up, table_name='person'): + log.debug(person_obj_up_result) + else: + log.warning(person_obj_up_result) + # break + continue # Something unexpected may have happened person_data_li.append(person_data) log.debug(f"Record processed: {person_id} {person_data['full_name']}")