Working on the cart and other related things

This commit is contained in:
Scott Idem
2021-05-10 18:29:53 -04:00
parent 8cbabf48fe
commit 9a46b755bf
36 changed files with 444 additions and 263 deletions

View File

@@ -4,8 +4,8 @@ import datetime, hashlib, logging, os, pytz, redis, secrets
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
from ..db_sql import redis_lookup_id_random
from ..lib_general import *
from ..log import *
from .common_field_schema import base_fields, default_num_bytes
@@ -95,6 +95,92 @@ class Order_Cart_Line_Base(BaseModel):
fields = base_fields
class Order_Cart_DB_Base(BaseModel):
log.setLevel(logging.WARNING)
log.debug(locals())
id_random: Optional[str] = Field(
**base_fields['order_cart_id_random'],
alias='order_cart_id_random',
default_factory=lambda:secrets.token_urlsafe(default_num_bytes),
)
id: Optional[int] = Field(
#alias='order_cart_id'
)
account_id_random: Optional[str]
account_id: Optional[int] # NOTE: This is not really optional
person_id_random: Optional[str]
person_id: Optional[int]
user_id_random: Optional[str]
user_id: Optional[int]
order_id_random: Optional[str]
order_id: Optional[int]
total_quantity: Optional[int] = Field(0, ge=0, lt=150)
total_amount: Optional[int] = Field(0, ge=0, lt=1500000)
notes: Optional[str]
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
#@validator('order_cart_id_random', always=True)
def order_cart_id_random_copy(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['id_random']:
return values['id_random']
return None
@validator('id', always=True)
def order_cart_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['id_random']:
return redis_lookup_id_random(record_id_random=values['id_random'], table_name='order_cart')
return None
@validator('account_id', always=True)
def account_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['account_id_random']:
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
return None
@validator('person_id', always=True)
def person_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['person_id_random']:
return redis_lookup_id_random(record_id_random=values['person_id_random'], table_name='person')
return None
@validator('user_id', always=True)
def user_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['user_id_random']:
return redis_lookup_id_random(record_id_random=values['user_id_random'], table_name='user')
return None
@validator('order_id', always=True)
def order_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if 'order_id_random' in values and values['order_id_random']:
return redis_lookup_id_random(record_id_random=values['order_id_random'], table_name='order')
return None
class Config:
underscore_attrs_are_private = True
fields = base_fields
class Order_Cart_Base(BaseModel):
log.setLevel(logging.WARNING)
log.debug(locals())
@@ -124,7 +210,7 @@ class Order_Cart_Base(BaseModel):
created_on: Optional[datetime.datetime] = None
updated_on: Optional[datetime.datetime] = None
order_cart_line_list: List[Order_Cart_Line_Base] = []
order_cart_line_li: List[Order_Cart_Line_Base] = []
cfg: Optional[Order_Cart_Cfg_Base] = Order_Cart_Cfg_Base()
@@ -199,7 +285,7 @@ def save_order_cart_obj(order_cart_obj_new=None):
#log.debug(order_cart_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True))
# Get the current order_cart_line list to compare with what was sent
# Get the current order_cart_line li to compare with what was sent
data = {}
data['order_cart_id_random'] = order_cart_obj_new.id_random
if order_cart_line_rec_li_curr := sql_select(table_name='v_order_cart_line', data=data, rm_id_random=True, as_list=True):
@@ -225,7 +311,7 @@ def 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_list:
for order_cart_line_obj_new in order_cart_obj_new.order_cart_line_li:
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
@@ -244,7 +330,7 @@ def 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_list = order_cart_line_obj_li_curr
order_cart_obj_new.order_cart_line_li = order_cart_line_obj_li_curr
log.debug(order_cart_obj_new)
# Final loop through to get the new order totals
@@ -258,8 +344,8 @@ def 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_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'})
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'})
# SQL INSERT or UPDATE the order_cart record
log.info('SQL INSERT or UPDATE the order_cart record')
@@ -278,7 +364,7 @@ def 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_list:
for order_cart_line_obj in order_cart_obj_new.order_cart_line_li:
log.debug(f"--- {order_cart_line_obj}")
log.debug(order_cart_line_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=False, exclude={}))
@@ -310,7 +396,7 @@ def get_order_cart_obj(order_cart_id=None, inc_order_cart_line_li=None, inc_orde
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_list'] = order_cart_line_rec_li
order_cart_rec['order_cart_line_li'] = 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)):