Work on redoing orders and related. A lot of code clean up. I think it works pretty well.
This commit is contained in:
@@ -4,18 +4,17 @@ import datetime
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import redis_lookup_id_random, sql_delete, sql_enable_part, sql_insert_or_update, sql_limit_offset_part, sql_select
|
||||
from app.db_sql import redis_lookup_id_random, sql_delete, sql_enable_part, sql_insert, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_update
|
||||
from app.lib_general import log, logging, logger_reset
|
||||
|
||||
from app.methods.order_cfg_methods import load_order_cfg_obj
|
||||
from app.methods.order_line_methods import create_order_obj_line, get_order_line_rec_list, load_order_obj_line, update_order_obj_line
|
||||
from app.methods.order_line_methods import check_order_obj_line_list, create_order_obj_line, get_order_line_rec_list, load_order_obj_line, remove_order_obj_line, update_order_obj_line
|
||||
# from app.methods.person_methods import load_person_obj
|
||||
# from app.methods.user_methods import load_user_obj
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.order_models import Order_Base
|
||||
from app.models.order_line_models import Order_Line_Base, Order_Line_DB_Base # This should go away later.
|
||||
# from app.models.person_models import Person_Base
|
||||
# from app.models.user_models import User_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### create_order_obj() ###
|
||||
@@ -42,7 +41,7 @@ def create_order_obj(
|
||||
order_obj = order_dict_obj
|
||||
# order_obj.account_id = account_id
|
||||
|
||||
order_dict = order_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'cfg', 'person', 'user', 'created_on', 'updated_on'})
|
||||
order_dict = order_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'cfg', 'order_line_list', 'person', 'user', 'created_on', 'updated_on'})
|
||||
log.debug(order_obj)
|
||||
|
||||
# ### SECTION ### Process data
|
||||
@@ -76,7 +75,11 @@ def create_order_obj(
|
||||
if order_obj.order_line_list:
|
||||
log.info('Looping through Order Line list')
|
||||
for order_line in order_obj.order_line_list:
|
||||
if order_line.id:
|
||||
# NOTE: Should there be a check here using check_order_obj_line_list()???
|
||||
# In theory there should not already be any order_line records associated with the new order_id.
|
||||
order_line_id = order_line.id
|
||||
|
||||
if order_line_id:
|
||||
log.info('Updating Order Line')
|
||||
if update_order_obj_result := update_order_obj_line(
|
||||
order_line_id = order_line_id,
|
||||
@@ -102,7 +105,7 @@ def create_order_obj(
|
||||
def update_order_obj(
|
||||
order_id: int,
|
||||
order_dict_obj: Order_Base,
|
||||
person_id: int|None = None,
|
||||
# person_id: int|None = None,
|
||||
) -> int|bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
@@ -121,24 +124,25 @@ def update_order_obj(
|
||||
order_obj = order_dict_obj
|
||||
# order_obj.account_id = account_id
|
||||
|
||||
order_dict = order_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'cfg', 'person', 'user', 'created_on', 'updated_on'})
|
||||
order_dict = order_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'cfg', 'order_line_list', 'person', 'user', 'created_on', 'updated_on'})
|
||||
log.debug(order_obj)
|
||||
|
||||
# ### SECTION ### Process data
|
||||
order_obj.id = order_id # Is this needed?
|
||||
order_dict['id'] = order_id
|
||||
|
||||
# Look for a person_id in the order_obj
|
||||
if person_id:
|
||||
order_obj.person_id = person_id # Is this needed?
|
||||
order_dict['person_id'] = person_id
|
||||
elif person_id := order_obj.person.id: pass
|
||||
# # Look for a person_id in the order_obj
|
||||
# if person_id:
|
||||
# order_obj.person_id = person_id # Is this needed?
|
||||
# order_dict['person_id'] = person_id
|
||||
# elif person_id := order_obj.person.id: pass
|
||||
|
||||
if order_dict_up_result := sql_update(
|
||||
data = order_dict,
|
||||
table_name = 'order',
|
||||
rm_id_random = True,
|
||||
): pass
|
||||
elif order_dict_up_result is None: pass # More than likely no order specific data was passed. There might be order lines though.
|
||||
else:
|
||||
log.warning(f'Person not updated.')
|
||||
log.debug(order_dict_up_result)
|
||||
@@ -149,7 +153,35 @@ def update_order_obj(
|
||||
if order_obj.order_line_list:
|
||||
log.info('Looping through Order Line list')
|
||||
for order_line in order_obj.order_line_list:
|
||||
if order_line.id:
|
||||
order_line_id = order_line.id
|
||||
product_id = order_line.product_id
|
||||
|
||||
if order_line.remove_line:
|
||||
remove_order_obj_line(
|
||||
order_id = order_id,
|
||||
order_line_id = order_line_id,
|
||||
product_id = product_id,
|
||||
)
|
||||
continue # Do not need to check for anything else on this loop
|
||||
|
||||
order_line_id = order_line.id
|
||||
product_id = order_line.product_id
|
||||
if for_person_id := order_line.for_person_id: pass
|
||||
log.info(f'Checking for matching order lines. Order ID: {order_id}; Product ID: {product_id}; For Person ID: {for_person_id}')
|
||||
|
||||
order_line_id_found = None
|
||||
if check_order_obj_line_list_result := check_order_obj_line_list(
|
||||
order_id = order_id,
|
||||
product_id = product_id,
|
||||
for_person_id = for_person_id,
|
||||
):
|
||||
for order_line_rec in check_order_obj_line_list_result:
|
||||
order_line_id_found = order_line_rec.get('order_line_id')
|
||||
|
||||
if order_line_id == order_line_id_found: pass
|
||||
elif order_line_id := order_line_id_found: pass
|
||||
|
||||
if order_line_id:
|
||||
log.info('Updating Order Line')
|
||||
if update_order_obj_result := update_order_obj_line(
|
||||
order_line_id = order_line_id,
|
||||
@@ -222,6 +254,8 @@ def load_order_obj(
|
||||
if order_line_rec_list_result := get_order_line_rec_list(
|
||||
for_obj_type = 'order',
|
||||
for_obj_id = order_id,
|
||||
# product_for_type = 'all',
|
||||
# status = 'all',
|
||||
limit = limit,
|
||||
):
|
||||
order_line_result_list = []
|
||||
|
||||
Reference in New Issue
Block a user