Working on products and carts

This commit is contained in:
Scott Idem
2021-08-01 19:46:14 -04:00
parent 1a493b85f8
commit c639c2b0a0
4 changed files with 74 additions and 26 deletions

View File

@@ -132,20 +132,20 @@ def sql_update(sql:str|None=None, data:dict|None=None, table_name:str|None=None,
sql_set = ', '.join(fields_string) sql_set = ', '.join(fields_string)
if record_id: if record_id:
log.info('Update record with ID') log.info(f'Update record with ID: {record_id}')
data['id'] = record_id data['id'] = record_id
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id = :id' sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id = :id'
elif record_id_random: elif record_id_random:
log.info('Update record with ID random') log.info(f'Update record with ID random: {record_id_random}')
data['id_random'] = record_id_random data['id_random'] = record_id_random
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id_random = :id_random' sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id_random = :id_random'
elif 'id' in data: elif 'id' in data:
log.info('Update record with ID') log.info(f"Update record with ID in data dict: {data['id']}")
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id = :id' sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id = :id'
elif 'id_random' in data: elif 'id_random' in data:
# NOTE: For now it is not possible to update the id_random when supplying the id_random as the primary key for a record. # NOTE: For now it is not possible to update the id_random when supplying the id_random as the primary key for a record.
# NOTE: In the future I can use record_id_random=True as a special case SQL UPDATE. # NOTE: In the future I can use record_id_random=True as a special case SQL UPDATE.
log.info('Update record with ID random') log.info(f"Update record with ID in data dict: {data['id_random']}")
sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id_random = :id_random' sql = 'UPDATE `'+table_name+'` SET '+ sql_set + ' WHERE id_random = :id_random'
else: else:
log.warning('Something was missing from the sql_update function call.') log.warning('Something was missing from the sql_update function call.')

View File

@@ -95,7 +95,7 @@ def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_l
# ### BEGIN ### API Order Cart Methods ### load_order_cart_obj() ### # ### BEGIN ### API Order Cart Methods ### load_order_cart_obj() ###
def load_order_cart_obj(order_cart_id:int|str, inc_order_cart_line_li:bool=False, inc_order_cart_cfg:bool=False) -> Order_Cart_Base: def load_order_cart_obj(order_cart_id:int|str, inc_order_cart_line_li:bool=False, inc_order_cart_cfg:bool=False) -> Order_Cart_Base|dict|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals()) log.debug(locals())

View File

@@ -27,7 +27,7 @@ router = APIRouter()
@router.post('/person_data', response_model=Resp_Body_Base) @router.post('/person_data', response_model=Resp_Body_Base)
async def importing_person_data( 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()) log.debug(locals())
account_id = 19 account_id = 19
@@ -125,6 +125,7 @@ async def importing_person_data(
continue # Something unexpected may have happened continue # Something unexpected may have happened
else: else:
log.warning('Found more than one record') log.warning('Found more than one record')
log.warning(person_rec_li_result)
# Do nothing # Do nothing
continue # Something unexpected may have happened continue # Something unexpected may have happened
person_rec_li = person_rec_li_result person_rec_li = person_rec_li_result
@@ -149,18 +150,32 @@ async def importing_person_data(
else: else:
contact_data['email_active'] = False contact_data['email_active'] = False
contact_data['email_status'] = record['email_status'] contact_data['email_status'] = record['email_status']
contact_data['phone_mobile'] = record['phone_mobile'] if record['email_status']:
contact_data['phone_home'] = record['phone_home'] 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']: if record['phone_fax']:
contact_data['phone_fax'] = record['phone_fax'] contact_data['phone_fax'] = record['phone_fax']
elif record['phone_fax'] and record['phone_work_fax']: elif record['phone_fax'] and record['phone_work_fax']:
contact_data['phone_fax'] = record['phone_fax'] contact_data['phone_fax'] = record['phone_fax']
contact_data['phone_other'] = record['phone_work_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) log.debug(contact_data)
if contact_id: if contact_id:
# UPDATE existing contact record # UPDATE existing contact record
log.info('UPDATE existing contact record')
contact_data['id'] = contact_id contact_data['id'] = contact_id
if contact_obj_up_result := sql_update(data=contact_data, table_name='contact'): if contact_obj_up_result := sql_update(data=contact_data, table_name='contact'):
log.debug(contact_obj_up_result) log.debug(contact_obj_up_result)
@@ -169,12 +184,14 @@ async def importing_person_data(
continue # Something unexpected may have happened continue # Something unexpected may have happened
elif person_id: elif person_id:
# INSERT new contact record and link to person record # 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['account_id'] = account_id
contact_data['for_type'] = 'person' contact_data['for_type'] = 'person'
contact_data['for_id'] = person_id contact_data['for_id'] = person_id
if contact_obj_in_result := sql_insert(data=contact_data, table_name='contact'): if contact_obj_in_result := sql_insert(data=contact_data, table_name='contact'):
log.debug(contact_obj_in_result) log.debug(contact_obj_in_result)
contact_id = contact_obj_in_result # Should be an int 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: else:
log.debug(contact_obj_in_result) log.debug(contact_obj_in_result)
continue # Something unexpected may have happened continue # Something unexpected may have happened
@@ -182,18 +199,39 @@ async def importing_person_data(
# Process the contact address data # Process the contact address data
log.debug('Process the contact address data') log.debug('Process the contact address data')
address_data = {} address_data = {}
address_data['line_1'] = record['address_line_1'] if record['address_line_1']:
address_data['line_2'] = record['address_line_2'] address_data['line_1'] = record['address_line_1']
address_data['line_3'] = record['address_line_3'] else:
address_data['city'] = record['address_city'] address_data['line_1'] = None
address_data['country_subdivision_code'] = record['address_country_code']+'-'+record['address_state_province_code'] if record['address_line_2']:
address_data['postal_code'] = record['address_postal_code'] address_data['line_2'] = record['address_line_2']
address_data['country_alpha_2_code'] = record['address_country_code'] 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) log.debug(address_data)
if address_id: if address_id:
# UPDATE existing address record # UPDATE existing address record
log.debug('UPDATE existing address record') log.info('UPDATE existing address record')
address_data['id'] = address_id address_data['id'] = address_id
if address_obj_up_result := sql_update(data=address_data, table_name='address'): if address_obj_up_result := sql_update(data=address_data, table_name='address'):
log.debug(address_obj_up_result) log.debug(address_obj_up_result)
@@ -202,17 +240,21 @@ async def importing_person_data(
# continue # Something unexpected may have happened # continue # Something unexpected may have happened
elif contact_id: elif contact_id:
# INSERT new address record and link to contact record # 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['account_id'] = account_id
address_data['for_type'] = 'contact' address_data['for_type'] = 'contact'
address_data['for_id'] = contact_id address_data['for_id'] = contact_id
if address_obj_in_result := sql_insert(data=address_data, table_name='address'): if address_obj_in_result := sql_insert(data=address_data, table_name='address'):
log.debug(address_obj_in_result) log.debug(address_obj_in_result)
address_id = address_obj_in_result # Should be an int 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: else:
log.debug(address_obj_in_result) log.debug(address_obj_in_result)
# break # break
continue # Something unexpected may have happened 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 # Process the user data
log.debug('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['username'] = record['email']
user_data['email'] = record['email'] user_data['email'] = record['email']
user_data['email_verified'] = contact_data['email_active'] # Not perfect, but a good start 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'] = False
user_data['enable_from'] = datetime.datetime.now(datetime.timezone.utc) 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['verified'] = True
user_data['notes'] = 'Created by importing list' 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) log.debug(user_data)
if user_id: if user_id:
# UPDATE existing user record # UPDATE existing user record
log.info('UPDATE existing user record')
user_data['id'] = user_id user_data['id'] = user_id
if user_obj_up_result := sql_update(data=user_data, table_name='user'): if user_obj_up_result := sql_update(data=user_data, table_name='user'):
log.debug(user_obj_up_result) log.debug(user_obj_up_result)
@@ -252,15 +288,26 @@ async def importing_person_data(
continue # Something unexpected may have happened continue # Something unexpected may have happened
elif person_id: elif person_id:
# INSERT new user record and link to person record # 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['account_id'] = account_id
user_data['person_id'] = person_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'): if user_obj_in_result := sql_insert(data=user_data, table_name='user'):
log.debug(user_obj_in_result) log.debug(user_obj_in_result)
user_id = user_obj_in_result # Should be an int 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: else:
log.debug(user_obj_in_result) log.debug(user_obj_in_result)
# break # break
continue # Something unexpected may have happened 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: if person_new:
log.debug('Updating person record one more time since this is a new person') log.debug('Updating person record one more time since this is a new person')

View File

@@ -47,7 +47,8 @@ async def post_order_cart_obj(
async def patch_order_cart_obj( async def patch_order_cart_obj(
order_cart_id: str = Query(..., min_length=1, max_length=22), order_cart_id: str = Query(..., min_length=1, max_length=22),
order_cart_obj: Order_Cart_Base = None, order_cart_obj: Order_Cart_Base = None,
repl_order_cart_line_li: Optional[bool] = False, repl_order_cart_line_list: Optional[bool] = False, # Replace all the lines instead of trying to update
# was repl_order_cart_line_li # NOTE: It was with out _list, just _li
x_account_id: Optional[str] = Header(..., ), x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True, return_obj: Optional[bool] = True,
inc_order_cart_line_li: Optional[bool] = True, inc_order_cart_line_li: Optional[bool] = True,