|
|
|
|
@@ -27,7 +27,7 @@ router = APIRouter()
|
|
|
|
|
@router.post('/person_data', response_model=Resp_Body_Base)
|
|
|
|
|
async def importing_person_data(
|
|
|
|
|
):
|
|
|
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
|
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
|
|
|
log.debug(locals())
|
|
|
|
|
|
|
|
|
|
account_id = 19
|
|
|
|
|
@@ -125,6 +125,7 @@ async def importing_person_data(
|
|
|
|
|
continue # Something unexpected may have happened
|
|
|
|
|
else:
|
|
|
|
|
log.warning('Found more than one record')
|
|
|
|
|
log.warning(person_rec_li_result)
|
|
|
|
|
# Do nothing
|
|
|
|
|
continue # Something unexpected may have happened
|
|
|
|
|
person_rec_li = person_rec_li_result
|
|
|
|
|
@@ -149,18 +150,32 @@ async def importing_person_data(
|
|
|
|
|
else:
|
|
|
|
|
contact_data['email_active'] = False
|
|
|
|
|
contact_data['email_status'] = record['email_status']
|
|
|
|
|
contact_data['phone_mobile'] = record['phone_mobile']
|
|
|
|
|
contact_data['phone_home'] = record['phone_home']
|
|
|
|
|
if record['email_status']:
|
|
|
|
|
contact_data['email_status'] = record['email_status']
|
|
|
|
|
else:
|
|
|
|
|
contact_data['email_status'] = None
|
|
|
|
|
if record['phone_mobile']:
|
|
|
|
|
contact_data['phone_mobile'] = record['phone_mobile']
|
|
|
|
|
else:
|
|
|
|
|
contact_data['phone_mobile'] = None
|
|
|
|
|
if record['phone_home']:
|
|
|
|
|
contact_data['phone_home'] = record['phone_home']
|
|
|
|
|
else:
|
|
|
|
|
contact_data['phone_home'] = None
|
|
|
|
|
if record['phone_fax']:
|
|
|
|
|
contact_data['phone_fax'] = record['phone_fax']
|
|
|
|
|
elif record['phone_fax'] and record['phone_work_fax']:
|
|
|
|
|
contact_data['phone_fax'] = record['phone_fax']
|
|
|
|
|
contact_data['phone_other'] = record['phone_work_fax']
|
|
|
|
|
contact_data['phone_office'] = record['phone_work']
|
|
|
|
|
if record['phone_work']:
|
|
|
|
|
contact_data['phone_office'] = record['phone_work']
|
|
|
|
|
else:
|
|
|
|
|
contact_data['phone_office'] = None
|
|
|
|
|
|
|
|
|
|
log.debug(contact_data)
|
|
|
|
|
if contact_id:
|
|
|
|
|
# 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)
|
|
|
|
|
@@ -169,12 +184,14 @@ async def importing_person_data(
|
|
|
|
|
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')
|
|
|
|
|
contact_data['account_id'] = account_id
|
|
|
|
|
contact_data['for_type'] = 'person'
|
|
|
|
|
contact_data['for_id'] = person_id
|
|
|
|
|
if contact_obj_in_result := sql_insert(data=contact_data, table_name='contact'):
|
|
|
|
|
log.debug(contact_obj_in_result)
|
|
|
|
|
contact_id = contact_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(contact_obj_in_result)
|
|
|
|
|
continue # Something unexpected may have happened
|
|
|
|
|
@@ -182,18 +199,39 @@ async def importing_person_data(
|
|
|
|
|
# Process the contact address data
|
|
|
|
|
log.debug('Process the contact address data')
|
|
|
|
|
address_data = {}
|
|
|
|
|
address_data['line_1'] = record['address_line_1']
|
|
|
|
|
address_data['line_2'] = record['address_line_2']
|
|
|
|
|
address_data['line_3'] = record['address_line_3']
|
|
|
|
|
address_data['city'] = record['address_city']
|
|
|
|
|
address_data['country_subdivision_code'] = record['address_country_code']+'-'+record['address_state_province_code']
|
|
|
|
|
address_data['postal_code'] = record['address_postal_code']
|
|
|
|
|
address_data['country_alpha_2_code'] = record['address_country_code']
|
|
|
|
|
if record['address_line_1']:
|
|
|
|
|
address_data['line_1'] = record['address_line_1']
|
|
|
|
|
else:
|
|
|
|
|
address_data['line_1'] = None
|
|
|
|
|
if record['address_line_2']:
|
|
|
|
|
address_data['line_2'] = record['address_line_2']
|
|
|
|
|
else:
|
|
|
|
|
address_data['line_2'] = None
|
|
|
|
|
if record['address_line_3']:
|
|
|
|
|
address_data['line_3'] = record['address_line_3']
|
|
|
|
|
else:
|
|
|
|
|
address_data['line_3'] = None
|
|
|
|
|
if record['address_city']:
|
|
|
|
|
address_data['city'] = record['address_city']
|
|
|
|
|
else:
|
|
|
|
|
address_data['city'] = None
|
|
|
|
|
if record['address_state_province_code']:
|
|
|
|
|
address_data['country_subdivision_code'] = record['address_country_code']+'-'+record['address_state_province_code']
|
|
|
|
|
else:
|
|
|
|
|
address_data['country_subdivision_code'] = None
|
|
|
|
|
if record['address_postal_code']:
|
|
|
|
|
address_data['postal_code'] = record['address_postal_code']
|
|
|
|
|
else:
|
|
|
|
|
address_data['postal_code'] = None
|
|
|
|
|
if record['address_country_code']:
|
|
|
|
|
address_data['country_alpha_2_code'] = record['address_country_code']
|
|
|
|
|
else:
|
|
|
|
|
address_data['country_alpha_2_code'] = None
|
|
|
|
|
|
|
|
|
|
log.debug(address_data)
|
|
|
|
|
if address_id:
|
|
|
|
|
# UPDATE existing address record
|
|
|
|
|
log.debug('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)
|
|
|
|
|
@@ -202,17 +240,21 @@ async def importing_person_data(
|
|
|
|
|
# continue # Something unexpected may have happened
|
|
|
|
|
elif contact_id:
|
|
|
|
|
# INSERT new address record and link to contact record
|
|
|
|
|
log.debug('INSERT new address record and link to contact record')
|
|
|
|
|
log.info('INSERT new address record and link to contact record')
|
|
|
|
|
address_data['account_id'] = account_id
|
|
|
|
|
address_data['for_type'] = 'contact'
|
|
|
|
|
address_data['for_id'] = contact_id
|
|
|
|
|
if address_obj_in_result := sql_insert(data=address_data, table_name='address'):
|
|
|
|
|
log.debug(address_obj_in_result)
|
|
|
|
|
address_id = address_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(address_obj_in_result)
|
|
|
|
|
# break
|
|
|
|
|
continue # Something unexpected may have happened
|
|
|
|
|
else:
|
|
|
|
|
log.error('No address ID to update or contact ID to create an address linked to the contact.')
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# Process the user data
|
|
|
|
|
log.debug('Process the user data')
|
|
|
|
|
@@ -221,8 +263,6 @@ async def importing_person_data(
|
|
|
|
|
user_data['username'] = record['email']
|
|
|
|
|
user_data['email'] = record['email']
|
|
|
|
|
user_data['email_verified'] = contact_data['email_active'] # Not perfect, but a good start
|
|
|
|
|
random_password_string = secrets.token_urlsafe(8)
|
|
|
|
|
user_data['password'] = secure_hash_string(string=random_password_string)
|
|
|
|
|
|
|
|
|
|
user_data['enable'] = False
|
|
|
|
|
user_data['enable_from'] = datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
|
@@ -236,14 +276,10 @@ async def importing_person_data(
|
|
|
|
|
user_data['verified'] = True
|
|
|
|
|
user_data['notes'] = 'Created by importing list'
|
|
|
|
|
|
|
|
|
|
other_data = {}
|
|
|
|
|
other_data['temp_password'] = random_password_string
|
|
|
|
|
|
|
|
|
|
user_data['other_json'] = json.dumps(other_data, indent=4)
|
|
|
|
|
|
|
|
|
|
log.debug(user_data)
|
|
|
|
|
if user_id:
|
|
|
|
|
# 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)
|
|
|
|
|
@@ -252,15 +288,26 @@ async def importing_person_data(
|
|
|
|
|
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')
|
|
|
|
|
user_data['account_id'] = account_id
|
|
|
|
|
user_data['person_id'] = person_id
|
|
|
|
|
random_password_string = secrets.token_urlsafe(8)
|
|
|
|
|
user_data['password'] = secure_hash_string(string=random_password_string)
|
|
|
|
|
other_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
|
|
|
|
|
else:
|
|
|
|
|
log.error('No user ID to update or person ID to create a user linked to the person.')
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if person_new:
|
|
|
|
|
log.debug('Updating person record one more time since this is a new person')
|
|
|
|
|
|