Working on products and carts

This commit is contained in:
Scott Idem
2021-08-02 17:25:48 -04:00
parent c639c2b0a0
commit ac21a67c8a
4 changed files with 114 additions and 42 deletions

View File

@@ -4,13 +4,16 @@ import datetime
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from app.lib_general import *
from app.lib_general import log, logging
from app.db_sql import redis_lookup_id_random, sql_insert_or_update, sql_insert, sql_update, sql_select
from app.models.order_cart_models import Order_Cart_Base
def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_li:bool=False):
def update_order_cart_obj(
order_cart_obj: Order_Cart_Base,
repl_order_cart_line_list: bool = False,
) -> bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -32,13 +35,13 @@ def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_l
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.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:
for order_cart_line_obj in order_cart_obj.order_cart_line_list:
log.debug(order_cart_line_obj)
order_cart_line_obj.order_cart_id = order_cart_id
@@ -46,38 +49,62 @@ def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_l
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'})
#log.setLevel(logging.DEBUG)
# log.setLevel(logging.DEBUG)
log.debug(order_cart_line_obj_data)
data = {}
data['order_cart_id_random'] = order_cart_id_random
data['product_id_random'] = order_cart_line_obj.product_id_random
data['order_cart_id'] = order_cart_id
data['product_id'] = order_cart_line_obj.product_id
sql_select_result = sql_select(table_name='order_cart_line', data=data, rm_id_random=True)
#log.setLevel(logging.DEBUG)
# log.setLevel(logging.DEBUG)
log.debug(sql_select_result)
if sql_select_result:
# log.setLevel(logging.DEBUG)
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
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.setLevel(logging.DEBUG)
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
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_total_amount += order_cart_line_obj.quantity * order_cart_line_obj.amount
# order_cart_total_quantity += order_cart_line_obj.quantity
order_cart_line_data = {}
order_cart_line_data['order_cart_id'] = order_cart_id
if order_cart_line_sql_select_result := sql_select(table_name='order_cart_line', data=order_cart_line_data, rm_id_random=True):
# log.setLevel(logging.DEBUG)
log.debug(sql_select_result)
for order_cart_line_rec in order_cart_line_sql_select_result:
log.debug(order_cart_line_rec)
quantity = order_cart_line_rec.get('quantity', 0)
amount = order_cart_line_rec.get('amount', 0)
order_cart_total_amount += quantity * amount
order_cart_total_quantity += quantity
order_cart_obj_new = {}
order_cart_obj_new['id_random'] = order_cart_id_random
order_cart_obj_new['id'] = order_cart_id
order_cart_obj_new['account_id_random'] = order_cart_obj.account_id_random
order_cart_obj_new['person_id_random'] = order_cart_obj.person_id_random
order_cart_obj_new['user_id_random'] = order_cart_obj.person_id_random
order_cart_obj_new['user_id_random'] = order_cart_obj.user_id_random
order_cart_obj_new['order_id_random'] = order_cart_obj.order_id_random
@@ -95,7 +122,12 @@ 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() ###
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:
# Update 2021-08-02 (partially)
def load_order_cart_obj(
order_cart_id: int|str,
inc_order_cart_line_list: bool = False,
inc_order_cart_cfg: bool = False,
) -> Order_Cart_Base|dict|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -106,15 +138,17 @@ def load_order_cart_obj(order_cart_id:int|str, inc_order_cart_line_li:bool=False
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(order_cart_rec)
if inc_order_cart_line_li:
if inc_order_cart_line_list:
order_cart_line_data = {}
order_cart_line_data['order_cart_id'] = order_cart_id
if order_cart_line_rec_li := sql_select(table_name='v_order_cart_line', data=order_cart_line_data, as_list=True):
order_cart_rec['order_cart_line_li'] = order_cart_line_rec_li
order_cart_rec['order_cart_line_list'] = order_cart_line_rec_li
else: order_cart_rec['order_cart_line_list'] = []
if inc_order_cart_cfg:
if order_cart_cfg_rec := sql_select(table_name='v_account_cfg_detail', field_name='account_id', field_value=order_cart_rec.get('account_id', None)):
order_cart_rec['cfg'] = order_cart_cfg_rec
else: order_cart_rec['cfg'] = None
log.debug(order_cart_rec)
else:
@@ -169,7 +203,7 @@ def old_save_order_cart_obj(order_cart_obj_new=None):
# Loop through the line list that was sent and compare with what was pulled from the DB
# Only insert if a product ID does not match
# Only update if a product ID does match
for order_cart_line_obj_new in order_cart_obj_new.order_cart_line_li:
for order_cart_line_obj_new in order_cart_obj_new.order_cart_line_list:
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if not any(order_cart_line_obj_curr.product_id_random == order_cart_line_obj_new.product_id_random for order_cart_line_obj_curr in order_cart_line_obj_li_curr):
# Need to append to current list
@@ -188,7 +222,7 @@ def old_save_order_cart_obj(order_cart_obj_new=None):
log.info(f'Not a match: {order_cart_line_obj_curr.product_id_random}')
# Save merged current and new list to the new order cart object
order_cart_obj_new.order_cart_line_li = order_cart_line_obj_li_curr
order_cart_obj_new.order_cart_line_list = order_cart_line_obj_li_curr
log.debug(order_cart_obj_new)
# Final loop through to get the new order totals
@@ -202,8 +236,8 @@ def old_save_order_cart_obj(order_cart_obj_new=None):
order_cart_obj_new.total_amount = order_cart_total_amount
order_cart_obj_new.total_quantity = order_cart_total_quantity
log.debug(order_cart_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_cart_id_random', '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_id_random', 'order_cart_line_li', 'cfg', 'created_on', 'updated_on'})
log.debug(order_cart_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_cart_id_random', 'order_cart_line_list', '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_id_random', 'order_cart_line_list', 'cfg', 'created_on', 'updated_on'})
# SQL INSERT or UPDATE the order_cart record
log.info('SQL INSERT or UPDATE the order_cart record')
@@ -222,7 +256,7 @@ def old_save_order_cart_obj(order_cart_obj_new=None):
# Loop through the order_cart_line list to SQL INSERT or UPDATE the records
log.info('Loop through the order_cart_line list to SQL INSERT or UPDATE the records')
for order_cart_line_obj in order_cart_obj_new.order_cart_line_li:
for order_cart_line_obj in order_cart_obj_new.order_cart_line_list:
log.debug(f"--- {order_cart_line_obj}")
log.debug(order_cart_line_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=False, exclude={}))
@@ -239,7 +273,7 @@ def old_save_order_cart_obj(order_cart_obj_new=None):
# IS THIS STILL NEEDED?
# ### BEGIN ### API Order Cart Model ### get_order_cart_obj() ###
def old_get_order_cart_obj(order_cart_id=None, inc_order_cart_line_li=None, inc_order_cart_cfg=None):
def old_get_order_cart_obj(order_cart_id=None, inc_order_cart_line_list=None, inc_order_cart_cfg=None):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -251,11 +285,11 @@ def old_get_order_cart_obj(order_cart_id=None, inc_order_cart_line_li=None, inc_
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(order_cart_rec)
if inc_order_cart_line_li:
if inc_order_cart_line_list:
order_cart_line_data = {}
order_cart_line_data['order_cart_id'] = order_cart_id
if order_cart_line_rec_li := sql_select(table_name='v_order_cart_line', data=order_cart_line_data, as_list=True):
order_cart_rec['order_cart_line_li'] = order_cart_line_rec_li
order_cart_rec['order_cart_line_list'] = order_cart_line_rec_li
if inc_order_cart_cfg:
if order_cart_cfg_rec := sql_select(table_name='v_account_cfg_detail', field_name='account_id', field_value=order_cart_rec.get('account_id', None)):