Working on the cart and other related things
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import redis, secrets
|
import datetime, pytz, redis, secrets
|
||||||
from timeit import default_timer as timer
|
from timeit import default_timer as timer
|
||||||
|
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
@@ -27,7 +27,7 @@ db = engine.connect()
|
|||||||
|
|
||||||
# ### BEGIN ### Core Help CRUD ### sql_insert() ###
|
# ### BEGIN ### Core Help CRUD ### sql_insert() ###
|
||||||
def sql_insert(sql:str=None, data:dict=None, table_name:str=None, id_random_length:int=8):
|
def sql_insert(sql:str=None, data:dict=None, table_name:str=None, id_random_length:int=8):
|
||||||
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())
|
||||||
|
|
||||||
if sql:
|
if sql:
|
||||||
@@ -109,7 +109,7 @@ def sql_insert(sql:str=None, data:dict=None, table_name:str=None, id_random_leng
|
|||||||
|
|
||||||
# ### BEGIN ### Core Help CRUD ### sql_update() ###
|
# ### BEGIN ### Core Help CRUD ### sql_update() ###
|
||||||
def sql_update(sql:str=None, data:dict=None, table_name:str=None, record_id:int=None, record_id_random:str=None, rm_id_random=None, id_random_length:None|int=8):
|
def sql_update(sql:str=None, data:dict=None, table_name:str=None, record_id:int=None, record_id_random:str=None, rm_id_random=None, id_random_length:None|int=8):
|
||||||
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())
|
||||||
|
|
||||||
if sql:
|
if sql:
|
||||||
@@ -146,11 +146,15 @@ def sql_update(sql:str=None, data:dict=None, table_name:str=None, record_id:int=
|
|||||||
log.info('Update record with ID random')
|
log.info('Update record with 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.')
|
||||||
return False
|
return False
|
||||||
sql_update = text(sql)
|
sql_update = text(sql)
|
||||||
|
|
||||||
|
log.debug(sql_update)
|
||||||
|
|
||||||
trans = db.begin()
|
trans = db.begin()
|
||||||
try:
|
try:
|
||||||
|
log.info('Trying to execute the SQL UPDATE query...')
|
||||||
result_update = db.execute(sql_update, data)
|
result_update = db.execute(sql_update, data)
|
||||||
trans.commit()
|
trans.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -187,7 +191,7 @@ def sql_update(sql:str=None, data:dict=None, table_name:str=None, record_id:int=
|
|||||||
# The catch all SQL INSERT or UPDATE function - STI 2021-02-17
|
# The catch all SQL INSERT or UPDATE function - STI 2021-02-17
|
||||||
# This one does it all for SQL INSERT and UPDATE queries
|
# This one does it all for SQL INSERT and UPDATE queries
|
||||||
def sql_insert_or_update(sql:str=None, data:dict=None, table_name:str=None, rm_id_random:bool=None, id_random_length:int=None):
|
def sql_insert_or_update(sql:str=None, data:dict=None, table_name:str=None, rm_id_random:bool=None, id_random_length:int=None):
|
||||||
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())
|
||||||
|
|
||||||
#if sql: pass
|
#if sql: pass
|
||||||
@@ -226,6 +230,7 @@ def sql_insert_or_update(sql:str=None, data:dict=None, table_name:str=None, rm_i
|
|||||||
;
|
;
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
log.debug(f"""
|
log.debug(f"""
|
||||||
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string})
|
INSERT INTO `{table_name}` ({fields_string}) VALUES ({values_string})
|
||||||
ON DUPLICATE KEY UPDATE
|
ON DUPLICATE KEY UPDATE
|
||||||
|
|||||||
@@ -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):
|
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):
|
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.')
|
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:
|
else:
|
||||||
@@ -21,27 +24,52 @@ def update_order_cart_obj(order_cart_obj:Order_Cart_Base, repl_order_cart_line_l
|
|||||||
#else: return False
|
#else: return False
|
||||||
|
|
||||||
order_cart_id_random = order_cart_obj.id_random # id_random because can't ref the alias
|
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
|
# Calculate totals
|
||||||
order_cart_total_amount:int = 0
|
order_cart_total_amount:int = 0
|
||||||
order_cart_total_quantity: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_li:
|
||||||
order_cart_total_amount += order_cart_line_obj.quantity * order_cart_line_obj.amount
|
log.debug(order_cart_line_obj)
|
||||||
order_cart_total_quantity += order_cart_line_obj.quantity
|
|
||||||
|
|
||||||
|
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.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:
|
else:
|
||||||
log.error('Something went wrong while trying to insert or update an order cart line record.')
|
log.info('A matching order cart line was not found. Insert...')
|
||||||
return False
|
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_total_amount += order_cart_line_obj.quantity * order_cart_line_obj.amount
|
||||||
#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_quantity += order_cart_line_obj.quantity
|
||||||
|
|
||||||
order_cart_obj_new = {}
|
order_cart_obj_new = {}
|
||||||
order_cart_obj_new['id_random'] = order_cart_id_random
|
order_cart_obj_new['id_random'] = order_cart_id_random
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ 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,
|
||||||
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,
|
||||||
@@ -53,10 +54,12 @@ async def patch_order_cart_obj(
|
|||||||
by_alias: Optional[bool] = True,
|
by_alias: Optional[bool] = True,
|
||||||
exclude_unset: Optional[bool] = True,
|
exclude_unset: Optional[bool] = True,
|
||||||
):
|
):
|
||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
order_cart_obj_up_result = update_order_cart_obj(order_cart_obj)
|
log.debug(order_cart_obj)
|
||||||
|
|
||||||
|
order_cart_obj_up_result = update_order_cart_obj(order_cart_obj=order_cart_obj, repl_order_cart_line_li=repl_order_cart_line_li)
|
||||||
if isinstance(order_cart_obj_up_result, int):
|
if isinstance(order_cart_obj_up_result, int):
|
||||||
log.info(f'Order cart update and the result was an int: {order_cart_obj_up_result}')
|
log.info(f'Order cart update and the result was an int: {order_cart_obj_up_result}')
|
||||||
pass
|
pass
|
||||||
@@ -67,26 +70,14 @@ async def patch_order_cart_obj(
|
|||||||
log.error(f'Order cart update and the result was an bool: {order_cart_obj_up_result}')
|
log.error(f'Order cart update and the result was an bool: {order_cart_obj_up_result}')
|
||||||
return mk_resp(data=False, status_code=500) # Internal Server Error
|
return mk_resp(data=False, status_code=500) # Internal Server Error
|
||||||
|
|
||||||
if order_cart_obj := load_order_cart_obj(order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg):
|
if return_obj:
|
||||||
data = order_cart_obj.dict(by_alias=True, exclude_unset=False)
|
if order_cart_obj := load_order_cart_obj(order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg):
|
||||||
return mk_resp(data=data)
|
data = order_cart_obj.dict(by_alias=True, exclude_unset=False)
|
||||||
|
return mk_resp(data=data)
|
||||||
|
else:
|
||||||
|
return mk_resp(data=False, status_code=404) # Not Found
|
||||||
else:
|
else:
|
||||||
return mk_resp(data=False, status_code=404) # Not Found
|
return mk_resp(data=True)
|
||||||
|
|
||||||
|
|
||||||
# obj_type = 'order_cart'
|
|
||||||
# obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
||||||
# obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type)
|
|
||||||
# obj_data_dict['id_random'] = obj_id
|
|
||||||
# result = patch_obj_template(
|
|
||||||
# obj_type=obj_type,
|
|
||||||
# data=obj_data_dict,
|
|
||||||
# obj_id=order_cart_id,
|
|
||||||
# return_obj=True,
|
|
||||||
# by_alias=True,
|
|
||||||
# exclude_unset=True,
|
|
||||||
# )
|
|
||||||
# return result
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/list', response_model=Resp_Body_Base)
|
@router.get('/list', response_model=Resp_Body_Base)
|
||||||
|
|||||||
Reference in New Issue
Block a user