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): 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: 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 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: 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={'product_type_id', 'product_type', 'created_on', 'updated_on'}) 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 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.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_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() ###