Files
OSIT-AE-API-FastAPI/app/models/order_cart_methods.py
2021-05-10 18:29:53 -04:00

104 lines
4.8 KiB
Python

from __future__ import annotations
import datetime
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from ..lib_general import *
from ..db_sql import redis_lookup_id_random, sql_insert_or_update, sql_update, sql_select
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):
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:
log.error('An account ID along with a person ID or user ID is required to create an order.')
return False
#if order_cart_id := redis_lookup_id_random(record_id_random=order_cart_id_random, table_name='order_cart'): pass
#else: return False
order_cart_id_random = order_cart_obj.id_random # id_random because can't ref the alias
# 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
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_id'] = order_cart_id
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
else:
log.error('Something went wrong while trying to insert or update 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_obj_new = {}
order_cart_obj_new['id_random'] = order_cart_id_random
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['order_id_random'] = order_cart_obj.order_id_random
order_cart_obj_new['total_amount'] = order_cart_total_amount
order_cart_obj_new['total_quantity'] = order_cart_total_quantity
order_cart_obj_new['notes'] = order_cart_obj.notes
if order_cart_obj_resp := sql_update(data=order_cart_obj_new, table_name='order_cart', rm_id_random=True): pass
else:
log.error('Something went wrong while trying to update an order cart record.')
return False
return False
# ### 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:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if order_cart_id := redis_lookup_id_random(record_id_random=order_cart_id, table_name='order_cart'): pass
else: return False
if order_cart_rec := sql_select(table_name='v_order_cart', record_id=order_cart_id):
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(order_cart_rec)
if inc_order_cart_line_li:
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
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
log.debug(order_cart_rec)
else:
return False
try:
order_cart_obj = Order_Cart_Base(**order_cart_rec)
log.debug(order_cart_obj)
except ValidationError as e:
log.error(e.json())
return False
return order_cart_obj
# ### END ### API Order Cart Methods ### load_order_cart_obj() ###