Working on the cart and other related things

This commit is contained in:
Scott Idem
2021-05-12 15:25:59 -04:00
parent 9a46b755bf
commit ee863face2
3 changed files with 58 additions and 34 deletions

View File

@@ -11,6 +11,9 @@ from .order_cart_model import Order_Cart_Base
def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_li:bool=False):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if order_cart_obj.account_id_random and (order_cart_obj.person_id_random or order_cart_obj.user_id_random):
log.info(f'An account.id_random {order_cart_obj.account_id_random} was passed. And either a person.id_random {order_cart_obj.person_id_random} or user.id_random {order_cart_obj.user_id_random} was passed. We can update the order cart.')
else:
@@ -21,27 +24,52 @@ def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_l
#else: return False
order_cart_id_random = order_cart_obj.id_random # id_random because can't ref the alias
log.setLevel(logging.DEBUG)
log.debug(order_cart_obj.id)
log.debug(order_cart_id_random)
if order_cart_id := redis_lookup_id_random(record_id_random=order_cart_id_random, table_name='order_cart'): pass
else: return False
log.setLevel(logging.DEBUG)
log.info('Loop through lines to update and calculate totals')
# Calculate totals
order_cart_total_amount:int = 0
order_cart_total_quantity:int = 0
for order_cart_line_obj in order_cart_obj.order_cart_line_li:
order_cart_total_amount += order_cart_line_obj.quantity * order_cart_line_obj.amount
order_cart_total_quantity += order_cart_line_obj.quantity
log.debug(order_cart_line_obj)
order_cart_line_obj.order_cart_id = order_cart_id
order_cart_line_obj.order_cart_id_random = order_cart_id_random
order_cart_line_obj_data = order_cart_line_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_cart_line_id_random', 'product_type_id', 'product_type', 'created_on', 'updated_on'})
order_cart_line_obj_data = order_cart_line_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'product_type_id', 'product_type', 'created_on', 'updated_on'})
#order_cart_line_obj_data['order_id'] = order_cart_id
log.setLevel(logging.DEBUG)
log.debug(order_cart_line_obj_data)
if order_cart_line_obj_resp := sql_insert_or_update(data=order_cart_line_obj_data, table_name='order_cart_line', rm_id_random=True, id_random_length=8): pass
data = {}
data['order_cart_id_random'] = order_cart_id_random
data['product_id_random'] = order_cart_line_obj.product_id_random
sql_select_result = sql_select(table_name='order_cart_line', data=data, rm_id_random=True)
log.setLevel(logging.DEBUG)
log.debug(sql_select_result)
if sql_select_result:
log.info('A matching order cart line was found. Update...')
if order_cart_line_obj_up_result := sql_update(data=order_cart_line_obj_data, table_name='order_cart_line', record_id=sql_select_result.get('id'), rm_id_random=True, id_random_length=8): pass
else:
log.error('Something went wrong while trying to update an order cart line record.')
return False
else:
log.error('Something went wrong while trying to insert or update an order cart line record.')
return False
log.info('A matching order cart line was not found. Insert...')
if order_cart_line_obj_in_result := sql_insert(data=order_cart_line_obj_data, table_name='order_cart_line', rm_id_random=True, id_random_length=8): pass
else:
log.error('Something went wrong while trying to insert an order cart line record.')
return False
#log.debug(order_cart_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_cart_line_li', 'cfg', 'created_on', 'updated_on'}))
#order_cart_obj_data = order_cart_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_cart_line_li', 'cfg', 'created_on', 'updated_on'})
order_cart_total_amount += order_cart_line_obj.quantity * order_cart_line_obj.amount
order_cart_total_quantity += order_cart_line_obj.quantity
order_cart_obj_new = {}
order_cart_obj_new['id_random'] = order_cart_id_random