from __future__ import annotations 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.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): log.setLevel(logging.WARNING) # 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.') elif order_cart_obj.account_id_random and not (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. A person.id_random {order_cart_obj.person_id_random} or user.id_random {order_cart_obj.user_id_random} was not passed. We can update the order cart without one of those.') else: log.error('An account ID. A person ID or user ID is not required until starting to process 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.WARNING) # 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() ### # IS THIS STILL NEEDED? # ### BEGIN ### API Order Cart Model ### save_order_cart_obj() ### def old_save_order_cart_obj(order_cart_obj_new=None): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if not order_cart_obj_new: return False #log.debug(order_cart_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True)) # 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): #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL #log.debug(order_cart_line_rec_li_curr) order_cart_line_obj_li_curr = [] for order_cart_line_rec in order_cart_line_rec_li_curr: try: order_cart_line_obj = Order_Cart_Line_Base(**order_cart_line_rec) log.debug(order_cart_line_obj) except ValidationError as e: log.error(e.json()) order_cart_line_obj_li_curr.append(order_cart_line_obj) #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(order_cart_line_obj_li_curr) else: #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(order_cart_line_rec_li_curr) order_cart_line_obj_li_curr = [] # 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: #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 log.info('Need to append to current list') order_cart_line_obj_li_curr.append(order_cart_line_obj_new) else: # Need to update a current list item ... loop through to find log.info('Need to update a current list item ... loop through to find') for index, order_cart_line_obj_curr in enumerate(order_cart_line_obj_li_curr): log.info(index) if order_cart_line_obj_new.product_id_random == order_cart_line_obj_curr.product_id_random: log.info(f'Match: {order_cart_line_obj_curr.product_id_random}') order_cart_line_obj_new.id_random = order_cart_line_obj_curr.id_random order_cart_line_obj_li_curr[index] = order_cart_line_obj_new else: 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 log.debug(order_cart_obj_new) # Final loop through to get the new order totals # Calculate totals order_cart_total_amount = 0 order_cart_total_quantity = 0 for order_cart_line_obj_curr in order_cart_line_obj_li_curr: order_cart_total_amount += order_cart_line_obj_curr.quantity * order_cart_line_obj_curr.amount order_cart_total_quantity += order_cart_line_obj_curr.quantity 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'}) # SQL INSERT or UPDATE the order_cart record log.info('SQL INSERT or UPDATE the order_cart record') if order_cart_obj_resp := sql_insert_or_update(sql=None, data=order_cart_obj_data, table_name='order_cart', rm_id_random=True, id_random_length=8): pass else: return False #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(order_cart_obj_resp) if isinstance(order_cart_obj_resp, bool) and order_cart_obj_resp: if order_cart_id := order_cart_obj_new.id: pass elif order_cart_id := order_cart_obj_new.id_random: pass elif isinstance(order_cart_obj_resp, int): order_cart_id = order_cart_obj_resp else: return False # 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: log.debug(f"--- {order_cart_line_obj}") log.debug(order_cart_line_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=False, exclude={})) 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', 'product_name', 'product_description', 'product_unit_price', 'product_max_quantity', 'order_cart_line_id_random', 'created_on', 'updated_on'}) order_cart_line_obj_data['order_cart_id'] = order_cart_id if order_cart_line_obj_resp := sql_insert_or_update(sql=None, data=order_cart_line_obj_data, table_name='order_cart_line', rm_id_random=True, id_random_length=8): pass else: return False log.debug(order_cart_line_obj_resp) return order_cart_id # ### END ### API Order Cart Model ### save_order_cart_obj() ### # 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): log.setLevel(logging.WARNING) # 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 order_cart_obj # ### END ### API Order Cart Model ### get_order_cart_obj() ###