510 lines
20 KiB
Python
510 lines
20 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 app.db_sql import redis_lookup_id_random, sql_delete, sql_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update
|
|
from app.lib_general import log, logging, logger_reset
|
|
|
|
from app.models.common_field_schema import default_num_bytes
|
|
# from app.models.order_line_models import Order_Line_Base, Order_Line_Full_Detail_Base
|
|
from app.models.order_line_models_v3 import Order_Line_Base, Order_Line_Full_Detail_Base
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### create_order_obj_line() ###
|
|
# Updated 2022-01-18
|
|
def create_order_obj_line(
|
|
order_id: int,
|
|
order_line_dict_obj: Order_Line_Base,
|
|
) -> int|bool:
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
log.info('Create dictionary or Pydantic object')
|
|
log.debug(type(order_line_dict_obj))
|
|
if isinstance(order_line_dict_obj, dict):
|
|
order_line_dict = order_line_dict_obj
|
|
try:
|
|
order_obj = Order_Line_Base(**order_line_dict)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
else:
|
|
order_obj = order_line_dict_obj
|
|
# order_obj.order_id = order_id
|
|
|
|
order_line_dict = order_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'product', 'remove_line', 'created_on', 'updated_on'})
|
|
log.debug(order_obj)
|
|
|
|
# ### SECTION ### Process data
|
|
order_obj.order_id = order_id # Is this needed?
|
|
order_line_dict['order_id'] = order_id
|
|
|
|
if order_line_dict_in_result := sql_insert(
|
|
data = order_line_dict,
|
|
table_name = 'order_line',
|
|
rm_id_random = True,
|
|
id_random_length = default_num_bytes
|
|
): pass
|
|
else:
|
|
log.warning(f'Order Line not created.')
|
|
log.debug(order_line_dict_in_result)
|
|
return False
|
|
|
|
log.debug(order_line_dict_in_result)
|
|
order_line_id = order_line_dict_in_result
|
|
|
|
log.info(f'Returning the Order Line ID: {order_line_id}')
|
|
|
|
return order_line_id
|
|
# ### END ### API Order Methods ### create_order_obj_line() ###
|
|
|
|
|
|
# # ### BEGIN ### API Order Line Methods ### create_order_obj_line() ###
|
|
# @logger_reset
|
|
# def create_order_obj_line(order_line_dict_obj:Order_Line_Base):
|
|
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
# log.debug(locals())
|
|
|
|
# if not order_line_dict_obj:
|
|
# return False
|
|
|
|
# order_line_obj_data = order_line_dict_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'user', 'created_on', 'updated_on'})
|
|
|
|
# if order_line_obj_in_result := sql_insert(data=order_line_obj_data, table_name='order_line', rm_id_random=True, id_random_length=8): pass
|
|
# else:
|
|
# return False
|
|
|
|
# log.debug(order_line_obj_in_result)
|
|
|
|
# order_line_id = order_line_obj_in_result
|
|
|
|
# log.debug(f'Returning the new order_line_id: {order_line_id}')
|
|
# return order_line_id
|
|
# # ### END ### API Order Line Methods ### create_order_obj_line() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### update_order_obj_line() ###
|
|
# Updated 2022-01-18
|
|
def update_order_obj_line(
|
|
order_line_id: int,
|
|
order_line_dict_obj: Order_Line_Base,
|
|
) -> int|bool:
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
|
else: return False
|
|
|
|
log.info('Create dictionary or Pydantic object')
|
|
log.debug(type(order_line_dict_obj))
|
|
if isinstance(order_line_dict_obj, dict):
|
|
order_line_dict = order_line_dict_obj
|
|
try:
|
|
order_line_obj = Order_Line_Base(**order_line_dict)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
else:
|
|
order_line_obj = order_line_dict_obj
|
|
# order_line_obj.order_line_id = order_line_id
|
|
|
|
order_line_dict = order_line_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'product', 'remove_line', 'updated_on', 'updated_on'})
|
|
log.debug(order_line_obj)
|
|
|
|
# ### SECTION ### Process data
|
|
order_line_obj.id = order_line_id # Is this needed?
|
|
order_line_dict['id'] = order_line_id
|
|
|
|
if order_line_dict_up_result := sql_update(
|
|
data = order_line_dict,
|
|
table_name = 'order_line',
|
|
rm_id_random = True,
|
|
): pass
|
|
else:
|
|
log.warning(f'Order Line not updated.')
|
|
log.debug(order_line_dict_up_result)
|
|
return False
|
|
|
|
log.debug(order_line_dict_up_result)
|
|
|
|
return True
|
|
# ### END ### API Order Methods ### update_order_obj_line() ###
|
|
|
|
|
|
# # ### BEGIN ### API Order Line Methods ### update_order_obj_line() ###
|
|
# @logger_reset
|
|
# def update_order_obj_line(
|
|
# order_line_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
|
# order_obj_line_obj_up: Order_Line_Base,
|
|
# create_sub_obj: bool = False,
|
|
# ) -> bool:
|
|
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
# log.debug(locals())
|
|
|
|
# if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
|
# else: return False
|
|
|
|
# order_obj_line_obj_up.id = order_line_id
|
|
|
|
# log.debug(order_obj_line_obj_up)
|
|
# # log.debug(order_obj_line_obj_up.dict(by_alias=True, exclude_unset=True))
|
|
# log.debug(order_obj_line_obj_up.dict(by_alias=False, exclude_unset=True))
|
|
# # log.debug(order_obj_line_obj_up.dict(by_alias=False, exclude_unset=False))
|
|
|
|
# #order_line_dict_up = order_obj_line_obj_up.dict(by_alias=False, exclude_unset=True)
|
|
|
|
# # if order_obj_line_obj_up.person_id and order_obj_line_obj_up.person:
|
|
# # person_id = order_obj_line_obj_up.person_id
|
|
# # person_obj_up = order_obj_line_obj_up.person
|
|
# # log.debug(person_id)
|
|
# # log.debug(person_obj_up)
|
|
# # if person_obj_up_result := update_person_obj(
|
|
# # person_id = person_id,
|
|
# # person_obj_up = person_obj_up,
|
|
# # create_sub_obj = create_sub_obj,
|
|
# # ):
|
|
# # log.debug(person_obj_up_result)
|
|
# # else:
|
|
# # log.debug(person_obj_up_result)
|
|
# # return False
|
|
|
|
# # if order_obj_line_obj_up.user_id and order_obj_line_obj_up.user:
|
|
# # user_id = order_obj_line_obj_up.user_id
|
|
# # user_obj_up = order_obj_line_obj_up.user
|
|
# # log.debug(user_id)
|
|
# # log.debug(user_obj_up)
|
|
# # if user_obj_up_result := update_user_obj(
|
|
# # user_id = user_id,
|
|
# # user_dict_obj = user_obj_up,
|
|
# # create_sub_obj = create_sub_obj,
|
|
# # ):
|
|
# # log.debug(user_obj_up_result)
|
|
# # else:
|
|
# # log.debug(user_obj_up_result)
|
|
# # return False
|
|
|
|
# order_line_dict_up = order_obj_line_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'user'})
|
|
# log.debug(order_line_dict_up)
|
|
|
|
# if order_obj_line_obj_up_result := sql_update(data=order_line_dict_up, table_name='order_line', rm_id_random=True):
|
|
# log.debug(order_obj_line_obj_up_result)
|
|
# return True
|
|
# else:
|
|
# log.debug(order_obj_line_obj_up_result)
|
|
# return False
|
|
# # ### END ### API Order Line Methods ### update_order_obj_line() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### load_order_obj_line() ###
|
|
# Updated 2021-11-19
|
|
@logger_reset
|
|
def load_order_obj_line(
|
|
order_line_id: int|str,
|
|
# limit: int = 500,
|
|
# offset: int = 0,
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True, # NOTE: Normally this is True
|
|
model_as_dict: bool = False, # NOTE: Normally this is False
|
|
) -> Order_Line_Base|dict|bool:
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
|
else: return False
|
|
|
|
if order_line_rec := sql_select(table_name='v_order_line', record_id=order_line_id): pass
|
|
else: return False
|
|
log.debug(order_line_rec)
|
|
|
|
try:
|
|
order_line_obj = Order_Line_Base(**order_line_rec)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
log.debug(order_line_obj)
|
|
|
|
if model_as_dict:
|
|
return order_line_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
|
else:
|
|
return order_line_obj
|
|
# ### END ### API Order Line Methods ### load_order_obj_line() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### load_order_obj_line_full_detail() ###
|
|
# Updated 2021-11-22
|
|
@logger_reset
|
|
def load_order_obj_line_full_detail(
|
|
order_line_rec: dict,
|
|
# limit: int = 500,
|
|
# offset: int = 0,
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True, # NOTE: Normally this is True
|
|
model_as_dict: bool = False, # NOTE: Normally this is False
|
|
) -> Order_Line_Full_Detail_Base|dict|bool:
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
try:
|
|
order_line_obj_full_detail = Order_Line_Full_Detail_Base(**order_line_rec)
|
|
log.debug(order_line_obj_full_detail)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
|
|
if model_as_dict:
|
|
return order_line_obj_full_detail.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
|
else:
|
|
return order_line_obj_full_detail
|
|
# ### END ### API Order Line Methods ### load_order_obj_line_full_detail() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### get_order_line_rec_list() ###
|
|
@logger_reset
|
|
def get_order_line_rec_list(
|
|
for_obj_type: str,
|
|
for_obj_id: str,
|
|
from_datetime: datetime.datetime = None, # For the order, not order_line
|
|
to_datetime: datetime.datetime = None, # For the order, not order_line
|
|
product_for_type: str = 'all', # all, cont_edu_cert, event, fundraising, membership, etc
|
|
status: str = 'all', # started, in progress, complete, all
|
|
full_detail: bool = False,
|
|
# enabled: str = 'enabled', # enabled, disabled, all
|
|
limit: int = 500,
|
|
offset: int = 0,
|
|
) -> list|bool:
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
|
else: return False
|
|
|
|
data = {}
|
|
data[f'{for_obj_type}_id'] = for_obj_id
|
|
# data['for_obj_type'] = for_obj_type
|
|
sql_obj_type_id = f'`order_line`.{for_obj_type}_id = :{for_obj_type}_id'
|
|
|
|
allowed_prod_type_li = ['cont_edu_cert', 'event', 'fundraising', 'membership', 'other'] # TEMPORARY list...
|
|
sql_product_for_type = ''
|
|
if product_for_type in allowed_prod_type_li:
|
|
if product_for_type == 'closed' or product_for_type == 'complete':
|
|
data['product_for_type'] = ['closed', 'complete']
|
|
sql_product_for_type = f'AND `order_line`.product_for_type IN :product_for_type'
|
|
elif product_for_type == 'locked' or product_for_type == 'in progress':
|
|
data['product_for_type'] = ['locked', 'in progress']
|
|
sql_product_for_type = f'AND `order_line`.product_for_type IN :product_for_type'
|
|
else:
|
|
data['product_for_type'] = product_for_type
|
|
sql_product_for_type = f'AND `order_line`.product_for_type = :product_for_type'
|
|
elif product_for_type == 'all':
|
|
sql_product_for_type = f'AND (`order_line`.product_for_type IS NULL OR `order_line`.product_for_type IS NOT NULL)'
|
|
else:
|
|
log.warning('The product_for_type value passed is not allowed. Returning None')
|
|
return False
|
|
|
|
allowed_status_li = ['open', 'locked', 'in progress', 'reopened', 'closed', 'complete', 'canceled', 'other'] # TEMPORARY list...
|
|
sql_status = ''
|
|
if status in allowed_status_li:
|
|
if status == 'closed' or status == 'complete':
|
|
data['status'] = ['closed', 'complete']
|
|
sql_status = f'AND `order_line`.order_status IN :status'
|
|
elif status == 'locked' or status == 'in progress':
|
|
data['status'] = ['locked', 'in progress']
|
|
sql_status = f'AND `order_line`.order_status IN :status'
|
|
else:
|
|
data['status'] = status
|
|
sql_status = f'AND `order_line`.order_status = :status'
|
|
elif status == 'all':
|
|
sql_status = f'AND `order_line`.order_status IS NOT NULL'
|
|
else:
|
|
log.warning('The status value passed is not allowed. Returning None')
|
|
return False
|
|
|
|
if from_datetime and to_datetime:
|
|
data['from_datetime'] = from_datetime
|
|
data['to_datetime'] = to_datetime
|
|
sql_from_to_datetime = f'AND `order_line`.order_created_on >= :from_datetime AND `order_line`.order_created_on <= :to_datetime'
|
|
elif from_datetime:
|
|
data['from_datetime'] = from_datetime
|
|
sql_from_to_datetime = f'AND `order_line`.order_created_on >= :from_datetime'
|
|
elif to_datetime:
|
|
data['to_datetime'] = to_datetime
|
|
sql_from_to_datetime = f'AND `order_line`.order_created_on <= :to_datetime'
|
|
else:
|
|
sql_from_to_datetime = ''
|
|
|
|
# sql_enabled, data['enable'] = sql_enable_part(table_name='order', enabled=enabled) # Reasonably safe return str and bool
|
|
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
|
|
|
|
log.debug(data)
|
|
|
|
if not full_detail:
|
|
sql = f"""
|
|
SELECT `order_line`.id AS 'order_line_id', `order_line`.id_random AS 'order_line_id_random'
|
|
FROM `v_order_line` AS `order_line`
|
|
WHERE
|
|
{sql_obj_type_id}
|
|
{sql_product_for_type}
|
|
{sql_status}
|
|
{sql_from_to_datetime}
|
|
ORDER BY order_line.name, `order_line`.created_on DESC, `order_line`.updated_on DESC
|
|
{sql_limit};
|
|
"""
|
|
else:
|
|
sql = f"""
|
|
SELECT *
|
|
FROM `v_order_line_full_detail` AS `order_line`
|
|
WHERE
|
|
{sql_obj_type_id}
|
|
{sql_product_for_type}
|
|
{sql_status}
|
|
{sql_from_to_datetime}
|
|
ORDER BY `order_line`.created_on DESC, `order_line`.updated_on DESC
|
|
{sql_limit};
|
|
"""
|
|
log.debug(sql)
|
|
|
|
if order_line_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
|
order_line_rec_li = order_line_rec_li_result
|
|
else: # [] or False
|
|
order_line_rec_li = order_line_rec_li_result
|
|
|
|
log.debug(order_line_rec_li_result)
|
|
|
|
return order_line_rec_li
|
|
# ### END ### API Order Line Methods ### get_order_line_rec_list() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### check_order_obj_line_list() ###
|
|
@logger_reset
|
|
def check_order_obj_line_list(
|
|
order_id: int|str,
|
|
product_id: int|str = None,
|
|
# product_for_id: int|str = 'all',
|
|
product_for_type: str = 'all', # all, cont_edu_cert, event, fundraising, membership, etc
|
|
for_person_id: int|str = None,
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
) -> list|bool:
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return False
|
|
|
|
if product_id := redis_lookup_id_random(record_id_random=product_id, table_name='product'): pass
|
|
elif product_id is None: pass
|
|
else: return False
|
|
|
|
if for_person_id := redis_lookup_id_random(record_id_random=for_person_id, table_name='person'): pass
|
|
elif for_person_id is None: pass
|
|
else: return False
|
|
|
|
data = {}
|
|
data['order_id'] = order_id
|
|
sql_order_id = f'`order_line`.order_id = :order_id'
|
|
|
|
if product_id:
|
|
data['product_id'] = product_id
|
|
sql_product_id = f'AND `order_line`.product_id = :product_id'
|
|
|
|
allowed_prod_type_li = ['cont_edu_cert', 'event', 'fundraising', 'membership', 'other'] # TEMPORARY list...
|
|
sql_product_for_type = ''
|
|
if product_for_type in allowed_prod_type_li:
|
|
if product_for_type == 'closed' or product_for_type == 'complete':
|
|
data['product_for_type'] = ['closed', 'complete']
|
|
sql_product_for_type = f'AND `order_line`.product_for_type IN :prod_type'
|
|
elif product_for_type == 'locked' or product_for_type == 'in progress':
|
|
data['product_for_type'] = ['locked', 'in progress']
|
|
sql_product_for_type = f'AND `order_line`.product_for_type IN :prod_type'
|
|
else:
|
|
data['product_for_type'] = prod_type
|
|
sql_product_for_type = f'AND `order_line`.product_for_type = :prod_type'
|
|
elif product_for_type == 'all':
|
|
sql_product_for_type = f'AND (`order_line`.product_for_type IS NULL OR `order_line`.product_for_type IS NOT NULL)'
|
|
else:
|
|
log.warning('The product_for_type value passed is not allowed. Returning None')
|
|
return False
|
|
|
|
sql_for_person_id = ''
|
|
if for_person_id:
|
|
data['for_person_id'] = for_person_id
|
|
sql_for_person_id = f'AND `order_line`.for_person_id = :for_person_id'
|
|
|
|
# sql_enabled, data['enable'] = sql_enable_part(table_name='order', enabled=enabled) # Reasonably safe return str and bool
|
|
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
|
|
|
|
log.debug(data)
|
|
|
|
sql = f"""
|
|
SELECT `order_line`.id AS 'order_line_id', `order_line`.id_random AS 'order_line_id_random'
|
|
FROM `order_line` AS `order_line`
|
|
WHERE
|
|
{sql_order_id}
|
|
{sql_product_id}
|
|
{sql_product_for_type}
|
|
{sql_for_person_id}
|
|
ORDER BY order_line.name, `order_line`.created_on DESC, `order_line`.updated_on DESC
|
|
{sql_limit};
|
|
"""
|
|
log.debug(sql)
|
|
|
|
if order_line_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
|
order_line_rec_li = order_line_rec_li_result
|
|
else: # [] or False
|
|
order_line_rec_li = order_line_rec_li_result
|
|
|
|
log.debug(order_line_rec_li_result)
|
|
|
|
return order_line_rec_li
|
|
# ### END ### API Order Line Methods ### check_order_obj_line_list() ###
|
|
|
|
|
|
|
|
# ### BEGIN ### API Order Line Methods ### remove_order_obj_line() ###
|
|
# Updated 2022-01-19
|
|
def remove_order_obj_line(
|
|
order_id: int,
|
|
order_line_id: int,
|
|
product_id: int,
|
|
) -> bool|None:
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
elif order_id is None: pass
|
|
else: return False
|
|
|
|
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
|
elif order_line_id is None: pass
|
|
else: return False
|
|
|
|
if product_id := redis_lookup_id_random(record_id_random=product_id, table_name='product'): pass
|
|
elif product_id is None: pass
|
|
else: return False
|
|
|
|
if order_line_id:
|
|
log.info(f'Deleting Order Line ID: {order_line_id}')
|
|
if order_line_del_result := sql_delete(
|
|
table_name = 'order_line',
|
|
record_id = order_line_id
|
|
):
|
|
log.debug(order_line_del_result)
|
|
return True
|
|
else: return False
|
|
elif order_id and product_id:
|
|
log.info(f'Deleting Order Line with Order ID: {order_id} and Product ID {product_id}')
|
|
data = { 'order_id': order_id, 'product_id': product_id}
|
|
if order_line_del_result := sql_delete(
|
|
table_name = 'order_line',
|
|
data = data,
|
|
):
|
|
log.debug(order_line_del_result)
|
|
return True
|
|
else: return False
|
|
# ### END ### API Order Line Methods ### remove_order_obj_line() ###
|