A lot of code clean up
This commit is contained in:
@@ -283,7 +283,12 @@ def create_address_obj(
|
||||
return False
|
||||
else: log.info(f'No existing address found with: For Type: {for_type}; For ID: {for_id}')
|
||||
|
||||
if address_dict_in_result := sql_insert(data=address_dict, table_name='address', rm_id_random=True, id_random_length=8): pass
|
||||
if address_dict_in_result := sql_insert(
|
||||
data = address_dict,
|
||||
table_name = 'address',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
return False
|
||||
|
||||
@@ -352,7 +357,11 @@ def update_address_obj(
|
||||
|
||||
# address_dict = address_dict_obj.dict(by_alias=False, exclude_unset=True)
|
||||
|
||||
if address_dict_up_result := sql_update(data=address_dict, table_name='address', rm_id_random=True): pass
|
||||
if address_dict_up_result := sql_update(
|
||||
data = address_dict,
|
||||
table_name = 'address',
|
||||
rm_id_random = True
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Address not updated.')
|
||||
log.debug(address_dict_up_result)
|
||||
|
||||
@@ -366,7 +366,12 @@ def create_contact_obj(
|
||||
if address_id: pass
|
||||
# elif address_id := contact_obj.address.id: pass
|
||||
|
||||
if contact_dict_in_result := sql_insert(data=contact_dict, table_name='contact', rm_id_random=True, id_random_length=8): pass
|
||||
if contact_dict_in_result := sql_insert(
|
||||
data = contact_dict,
|
||||
table_name = 'contact',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
return False
|
||||
|
||||
@@ -489,7 +494,11 @@ def update_contact_obj(
|
||||
log.debug(contact_obj.address.id)
|
||||
log.debug(contact_obj.address.id_random)
|
||||
|
||||
if contact_dict_up_result := sql_update(data=contact_dict, table_name='contact', rm_id_random=True): pass
|
||||
if contact_dict_up_result := sql_update(
|
||||
data=contact_dict,
|
||||
table_name='contact',
|
||||
rm_id_random=True
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Contact not updated.')
|
||||
log.debug(contact_dict_up_result)
|
||||
|
||||
@@ -456,8 +456,8 @@ def create_membership_person_obj(
|
||||
|
||||
if membership_person_obj.membership_person_group_list:
|
||||
log.info('Looping through Membership Person Group object list')
|
||||
for membership_person_group_obj in membership_person_group_obj_list_result:
|
||||
if membership_person_group_obj.id:
|
||||
for membership_person_group_obj in membership_person_obj.membership_person_group_list:
|
||||
if membership_person_group_id := membership_person_group_obj.id:
|
||||
log.info('Updating Membership Person Group object')
|
||||
if membership_person_group_update_result := update_membership_person_group_obj(
|
||||
membership_person_group_id = membership_person_group_id,
|
||||
@@ -474,6 +474,7 @@ def create_membership_person_obj(
|
||||
|
||||
if membership_person_obj.membership_person_type and membership_person_obj.membership_person_type.id:
|
||||
log.info('Updating Membership Person Type object')
|
||||
membership_person_type_id = membership_person_obj.membership_person_type.id
|
||||
if membership_person_type_update_result := update_membership_person_type_obj(
|
||||
membership_person_type_id = membership_person_type_id,
|
||||
membership_person_type_dict_obj = membership_person_obj.membership_person_type,
|
||||
@@ -555,26 +556,27 @@ def update_membership_person_obj(
|
||||
membership_person_outline['membership_person_type'] = {}
|
||||
membership_person_outline['membership_person_type_id'] = None
|
||||
|
||||
if membership_person_obj.membership_person_type_list:
|
||||
if membership_person_obj.membership_person_group_list:
|
||||
log.info('Looping through Membership Person Group object list')
|
||||
for membership_person_type_obj in membership_person_type_obj_list_result:
|
||||
if membership_person_type_obj.id:
|
||||
for membership_person_group_obj in membership_person_obj.membership_person_group_list:
|
||||
if membership_person_group_id := membership_person_group_obj.id:
|
||||
log.info('Updating Membership Person Group object')
|
||||
if membership_person_type_update_result := update_membership_person_type_obj(
|
||||
membership_person_type_id = membership_person_type_id,
|
||||
membership_person_type_dict_obj = membership_person_type_obj,
|
||||
if membership_person_group_update_result := update_membership_person_group_obj(
|
||||
membership_person_group_id = membership_person_group_id,
|
||||
membership_person_group_dict_obj = membership_person_group_obj,
|
||||
): pass
|
||||
else: return False
|
||||
else:
|
||||
log.info('Creating Membership Person Group object')
|
||||
if membership_person_type_create_result := create_membership_person_type_obj(
|
||||
if membership_person_group_create_result := create_membership_person_group_obj(
|
||||
membership_person_id = membership_person_id,
|
||||
membership_person_type_dict_obj = membership_person_type_obj,
|
||||
membership_person_group_dict_obj = membership_person_group_obj,
|
||||
): pass
|
||||
else: pass
|
||||
|
||||
if membership_person_obj.membership_person_type and membership_person_obj.membership_person_type.id:
|
||||
log.info('Updating Membership Person Type object')
|
||||
membership_person_type_id = membership_person_obj.membership_person_type.id
|
||||
if membership_person_type_update_result := update_membership_person_type_obj(
|
||||
membership_person_type_id = membership_person_type_id,
|
||||
membership_person_type_dict_obj = membership_person_obj.membership_person_type,
|
||||
|
||||
@@ -11,34 +11,194 @@ from app.lib_general import log, logging, logger_reset
|
||||
from app.models.order_line_models import Order_Line_Base, Order_Line_Full_Detail_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Line Methods ### create_order_line_obj() ###
|
||||
@logger_reset
|
||||
def create_order_line_obj(order_line_obj_new:Order_Line_Base):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# ### 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())
|
||||
|
||||
if not order_line_obj_new:
|
||||
return False
|
||||
|
||||
order_line_obj_data = order_line_obj_new.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
|
||||
# ### 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', '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_obj_in_result)
|
||||
log.debug(order_line_dict_in_result)
|
||||
order_line_id = order_line_dict_in_result
|
||||
|
||||
order_line_id = order_line_obj_in_result
|
||||
log.info(f'Returning the Order Line ID: {order_line_id}')
|
||||
|
||||
log.debug(f'Returning the new order_line_id: {order_line_id}')
|
||||
return order_line_id
|
||||
# ### END ### API Order Line Methods ### create_order_line_obj() ###
|
||||
# ### END ### API Order Methods ### create_order_obj_line() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Line Methods ### load_order_line_obj() ###
|
||||
# # ### 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
|
||||
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', 'updated_on', 'updated_on'})
|
||||
log.debug(order_line_obj)
|
||||
|
||||
# ### SECTION ### Process data
|
||||
order_line_obj.order_line_id = order_line_id # Is this needed?
|
||||
order_line_dict['order_line_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_line_obj(
|
||||
def load_order_obj_line(
|
||||
order_line_id: int|str,
|
||||
# limit: int = 500,
|
||||
# offset: int = 0,
|
||||
@@ -67,13 +227,13 @@ def load_order_line_obj(
|
||||
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_line_obj() ###
|
||||
# ### END ### API Order Line Methods ### load_order_obj_line() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Line Methods ### load_order_line_obj_full_detail() ###
|
||||
# ### BEGIN ### API Order Line Methods ### load_order_obj_line_full_detail() ###
|
||||
# Updated 2021-11-22
|
||||
@logger_reset
|
||||
def load_order_line_obj_full_detail(
|
||||
def load_order_obj_line_full_detail(
|
||||
order_line_rec: dict,
|
||||
# limit: int = 500,
|
||||
# offset: int = 0,
|
||||
@@ -95,71 +255,7 @@ def load_order_line_obj_full_detail(
|
||||
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_line_obj_full_detail() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Line Methods ### update_order_line_obj() ###
|
||||
@logger_reset
|
||||
def update_order_line_obj(
|
||||
order_line_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
order_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_line_obj_up.id = order_line_id
|
||||
|
||||
log.debug(order_line_obj_up)
|
||||
# log.debug(order_line_obj_up.dict(by_alias=True, exclude_unset=True))
|
||||
log.debug(order_line_obj_up.dict(by_alias=False, exclude_unset=True))
|
||||
# log.debug(order_line_obj_up.dict(by_alias=False, exclude_unset=False))
|
||||
|
||||
#order_line_dict_up = order_line_obj_up.dict(by_alias=False, exclude_unset=True)
|
||||
|
||||
# if order_line_obj_up.person_id and order_line_obj_up.person:
|
||||
# person_id = order_line_obj_up.person_id
|
||||
# person_obj_up = order_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_line_obj_up.user_id and order_line_obj_up.user:
|
||||
# user_id = order_line_obj_up.user_id
|
||||
# user_obj_up = order_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_line_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'user'})
|
||||
log.debug(order_line_dict_up)
|
||||
|
||||
if order_line_obj_up_result := sql_update(data=order_line_dict_up, table_name='order_line', rm_id_random=True):
|
||||
log.debug(order_line_obj_up_result)
|
||||
return True
|
||||
else:
|
||||
log.debug(order_line_obj_up_result)
|
||||
return False
|
||||
# ### END ### API Order Line Methods ### update_order_line_obj() ###
|
||||
# ### END ### API Order Line Methods ### load_order_obj_line_full_detail() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Line Methods ### get_order_line_rec_list() ###
|
||||
|
||||
@@ -8,7 +8,7 @@ from app.db_sql import redis_lookup_id_random, sql_delete, sql_enable_part, sql_
|
||||
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 get_order_line_rec_list, load_order_line_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.person_methods import load_person_obj
|
||||
# from app.methods.user_methods import load_user_obj
|
||||
|
||||
@@ -18,6 +18,268 @@ from app.models.order_line_models import Order_Line_Base, Order_Line_DB_Base # T
|
||||
# from app.models.user_models import User_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### create_order_obj() ###
|
||||
# Updated 2022-01-18
|
||||
def create_order_obj(
|
||||
account_id: int,
|
||||
order_dict_obj: Order_Base,
|
||||
person_id: int|None = None,
|
||||
) -> 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_dict_obj))
|
||||
if isinstance(order_dict_obj, dict):
|
||||
order_dict = order_dict_obj
|
||||
try:
|
||||
order_obj = Order_Base(**order_dict)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
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'})
|
||||
log.debug(order_obj)
|
||||
|
||||
# ### SECTION ### Process data
|
||||
# Look for an account_id in the order_obj
|
||||
# if account_id: pass
|
||||
# elif account_id := order_obj.account_id: pass
|
||||
|
||||
order_obj.account_id = account_id # Is this needed?
|
||||
order_dict['account_id'] = account_id
|
||||
|
||||
# Look for a person_id in the contact_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_in_result := sql_insert(
|
||||
data = order_dict,
|
||||
table_name = 'order',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Order not created.')
|
||||
log.debug(order_dict_in_result)
|
||||
return False
|
||||
|
||||
log.debug(order_dict_in_result)
|
||||
order_id = order_dict_in_result
|
||||
|
||||
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:
|
||||
log.info('Updating Order Line')
|
||||
if update_order_obj_result := update_order_obj_line(
|
||||
order_line_id = order_line_id,
|
||||
order_line_dict_obj = order_line,
|
||||
): pass
|
||||
else: return False
|
||||
else:
|
||||
log.info('Creating Order Line')
|
||||
if create_order_obj_line_result := create_order_obj_line(
|
||||
order_id = order_id,
|
||||
order_line_dict_obj = order_line,
|
||||
): pass
|
||||
else: pass
|
||||
|
||||
log.info(f'Returning the Order ID: {order_id}')
|
||||
|
||||
return order_id
|
||||
# ### END ### API Order Methods ### create_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### update_order_obj() ###
|
||||
# Updated 2022-01-18
|
||||
def update_order_obj(
|
||||
order_id: int,
|
||||
order_dict_obj: Order_Base,
|
||||
person_id: int|None = None,
|
||||
) -> 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_dict_obj))
|
||||
if isinstance(order_dict_obj, dict):
|
||||
order_dict = order_dict_obj
|
||||
try:
|
||||
order_obj = Order_Base(**order_dict)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
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'})
|
||||
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
|
||||
|
||||
if order_dict_up_result := sql_update(
|
||||
data = order_dict,
|
||||
table_name = 'order',
|
||||
rm_id_random = True,
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Person not updated.')
|
||||
log.debug(order_dict_up_result)
|
||||
return False
|
||||
|
||||
log.debug(order_dict_up_result)
|
||||
|
||||
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:
|
||||
log.info('Updating Order Line')
|
||||
if update_order_obj_result := update_order_obj_line(
|
||||
order_line_id = order_line_id,
|
||||
order_line_dict_obj = order_line,
|
||||
): pass
|
||||
else: return False
|
||||
else:
|
||||
log.info('Creating Order Line')
|
||||
if create_order_obj_line_result := create_order_obj_line(
|
||||
order_id = order_id,
|
||||
order_line_dict_obj = order_line,
|
||||
): pass
|
||||
else: pass
|
||||
|
||||
return True
|
||||
# ### END ### API Order Methods ### update_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### load_order_obj() ###
|
||||
# Updated 2021-11-19
|
||||
# @logger_reset
|
||||
def load_order_obj(
|
||||
order_id: int|str,
|
||||
inc_address: bool = False,
|
||||
inc_contact: bool = False,
|
||||
inc_order_cfg: bool = False,
|
||||
inc_order_line_list: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_user: bool = False,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
limit: int = 500,
|
||||
offset: int = 0,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
model_as_dict: bool = False,
|
||||
) -> None|bool|dict|list:
|
||||
log.setLevel(logging.WARNING) # 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 # None, false bool
|
||||
|
||||
if order_rec := sql_select(table_name='v_order', record_id=order_id): pass
|
||||
else: return order_rec # None, empty dict, empty list, false bool
|
||||
log.debug(order_rec)
|
||||
|
||||
try:
|
||||
order_obj = Order_Base(**order_rec)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
log.debug(order_obj)
|
||||
|
||||
# Updated 2022-01-18
|
||||
if inc_order_cfg:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
if order_cfg_result := load_order_cfg_obj(
|
||||
account_id = order_rec.get('account_id'),
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = True,
|
||||
):
|
||||
order_obj.cfg = order_cfg_result
|
||||
else: order_obj.cfg = {} # None
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_cfg_result)
|
||||
|
||||
# Updated 2021-06-18
|
||||
if inc_order_line_list:
|
||||
if order_line_rec_list_result := get_order_line_rec_list(
|
||||
for_obj_type = 'order',
|
||||
for_obj_id = order_id,
|
||||
limit = limit,
|
||||
):
|
||||
order_line_result_list = []
|
||||
for order_line_rec in order_line_rec_list_result:
|
||||
order_line_result_list.append(
|
||||
load_order_obj_line(
|
||||
order_line_id = order_line_rec.get('order_line_id'),
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = model_as_dict,
|
||||
)
|
||||
)
|
||||
order_obj.order_line_list = order_line_result_list
|
||||
else: order_obj.order_line_list = [] # None
|
||||
|
||||
# Updated 2022-01-18
|
||||
if inc_person:
|
||||
from app.methods.person_methods import load_person_obj
|
||||
if person_result := load_person_obj(
|
||||
person_id = order_rec.get('person_id'),
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_user = inc_user, # NOTE:
|
||||
enabled = enabled,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = model_as_dict,
|
||||
):
|
||||
order_obj.person = person_result
|
||||
else: order_obj.person = {} # None
|
||||
pass
|
||||
|
||||
# Updated 2021-06-22
|
||||
# NOTE: Phasing out! Use *inc_user* under load_person_obj() instead.
|
||||
# if inc_user:
|
||||
# log.warning(f'This is being deprecated? load_order_obj() inc_user')
|
||||
# from app.methods.user_methods import load_user_obj
|
||||
# if user_result := load_user_obj(
|
||||
# user_id = order_rec.get('user_id', None),
|
||||
# limit = limit,
|
||||
# by_alias = by_alias,
|
||||
# exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
# enabled = enabled,
|
||||
# ):
|
||||
# order_obj.user = user_result
|
||||
# else: order_obj.user = None
|
||||
# pass
|
||||
|
||||
if model_as_dict:
|
||||
return order_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
else:
|
||||
return order_obj
|
||||
# ### END ### API Order Methods ### load_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### save_order_obj() ###
|
||||
# @logger_reset
|
||||
def save_order_obj(order_obj_new:Order_Base, repl_order_line_li:bool=False):
|
||||
@@ -173,116 +435,7 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_li:bool=False):
|
||||
# ### END ### API Order Methods ### save_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### load_order_obj() ###
|
||||
# Updated 2021-11-19
|
||||
# @logger_reset
|
||||
def load_order_obj(
|
||||
order_id: int|str,
|
||||
inc_address: bool = False,
|
||||
inc_contact: bool = False,
|
||||
inc_order_cfg: bool = False,
|
||||
inc_order_line_list: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_user: bool = False,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
limit: int = 500,
|
||||
offset: int = 0,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
model_as_dict: bool = False,
|
||||
) -> None|bool|dict|list:
|
||||
log.setLevel(logging.WARNING) # 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 # None, false bool
|
||||
|
||||
if order_rec := sql_select(table_name='v_order', record_id=order_id): pass
|
||||
else: return order_rec # None, empty dict, empty list, false bool
|
||||
log.debug(order_rec)
|
||||
|
||||
try:
|
||||
order_obj = Order_Base(**order_rec)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
log.debug(order_obj)
|
||||
|
||||
# Updated 2022-01-18
|
||||
if inc_order_cfg:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
if order_cfg_result := load_order_cfg_obj(
|
||||
account_id = order_rec.get('account_id'),
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = True,
|
||||
):
|
||||
order_obj.cfg = order_cfg_result
|
||||
else: order_obj.cfg = {} # None
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_cfg_result)
|
||||
|
||||
# Updated 2021-06-18
|
||||
if inc_order_line_list:
|
||||
if order_line_rec_list_result := get_order_line_rec_list(
|
||||
for_obj_type = 'order',
|
||||
for_obj_id = order_id,
|
||||
limit = limit,
|
||||
):
|
||||
order_line_result_list = []
|
||||
for order_line_rec in order_line_rec_list_result:
|
||||
order_line_result_list.append(
|
||||
load_order_line_obj(
|
||||
order_line_id = order_line_rec.get('order_line_id'),
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = model_as_dict,
|
||||
)
|
||||
)
|
||||
order_obj.order_line_list = order_line_result_list
|
||||
else: order_obj.order_line_list = [] # None
|
||||
|
||||
# Updated 2022-01-18
|
||||
if inc_person:
|
||||
from app.methods.person_methods import load_person_obj
|
||||
if person_result := load_person_obj(
|
||||
person_id = order_rec.get('person_id'),
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_user = inc_user, # NOTE:
|
||||
enabled = enabled,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = model_as_dict,
|
||||
):
|
||||
order_obj.person = person_result
|
||||
else: order_obj.person = {} # None
|
||||
pass
|
||||
|
||||
# Updated 2021-06-22
|
||||
# NOTE: Phasing out! Use *inc_user* under load_person_obj() instead.
|
||||
# if inc_user:
|
||||
# log.warning(f'This is being deprecated? load_order_obj() inc_user')
|
||||
# from app.methods.user_methods import load_user_obj
|
||||
# if user_result := load_user_obj(
|
||||
# user_id = order_rec.get('user_id', None),
|
||||
# limit = limit,
|
||||
# by_alias = by_alias,
|
||||
# exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
# enabled = enabled,
|
||||
# ):
|
||||
# order_obj.user = user_result
|
||||
# else: order_obj.user = None
|
||||
# pass
|
||||
|
||||
if model_as_dict:
|
||||
return order_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
else:
|
||||
return order_obj
|
||||
# ### END ### API Order Methods ### load_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Methods ### get_order_rec_list() ###
|
||||
|
||||
@@ -19,6 +19,229 @@ from app.models.contact_models import Contact_Base
|
||||
from app.models.person_models import Person_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### create_person_kiss() ###
|
||||
# Updated 2022-01-06
|
||||
def create_person_kiss(
|
||||
account_id: int,
|
||||
person_dict_obj: Person_Base,
|
||||
contact_id: int|None = None,
|
||||
organization_id: int|None = None,
|
||||
user_id: int|None = None,
|
||||
) -> 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(person_dict_obj))
|
||||
if isinstance(person_dict_obj, dict):
|
||||
person_dict = person_dict_obj
|
||||
try:
|
||||
person_obj = Person_Base(**person_dict)
|
||||
log.debug(person_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
person_obj = person_dict_obj
|
||||
# person_obj.account_id = account_id
|
||||
|
||||
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
|
||||
|
||||
# ### SECTION ### Process data
|
||||
# Look for an account_id in the person_obj
|
||||
# if account_id: pass
|
||||
# elif account_id := person_obj.account_id: pass
|
||||
|
||||
person_obj.account_id = account_id # Is this needed?
|
||||
person_dict['account_id'] = account_id
|
||||
|
||||
# Look for a contact_id in the contact_obj
|
||||
if contact_id: pass
|
||||
elif contact_id := person_obj.contact.id: pass
|
||||
|
||||
# Look for a user_id in the person_obj
|
||||
if user_id: pass
|
||||
elif user_id := person_obj.user.id: pass
|
||||
# if user_id:
|
||||
# # Link to an existing user
|
||||
# log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
# person_obj.user_id = user_id # Is this needed?
|
||||
# person_dict['user_id'] = user_id
|
||||
|
||||
if person_dict_in_result := sql_insert(
|
||||
data = person_dict,
|
||||
table_name = 'person',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Person not created.')
|
||||
log.debug(person_dict_in_result)
|
||||
return False
|
||||
|
||||
log.debug(person_dict_in_result)
|
||||
person_id = person_dict_in_result
|
||||
|
||||
if contact_id and person_obj.contact:
|
||||
log.info('Updating Contact object')
|
||||
if contact_update_result := update_contact_obj(
|
||||
contact_id = contact_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
elif person_obj.contact:
|
||||
log.info('Creating Contact object')
|
||||
if contact_create_result := create_contact_obj(
|
||||
account_id = account_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
if user_id and person_obj.user:
|
||||
log.info('Updating User object')
|
||||
# Link to an existing user
|
||||
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
person_obj.user_id = user_id # Is this needed?
|
||||
person_dict['user_id'] = user_id
|
||||
|
||||
from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_update_result := update_user_obj(
|
||||
user_id = user_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
elif person_obj.user:
|
||||
log.info('Creating User object')
|
||||
from app.methods.user_methods import create_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_create_result := create_user_obj(
|
||||
account_id = account_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
allow_update = True, # WARNING NOTE: This will allow an existing user record to be updated.
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
log.info(f'Returning the Person ID: {person_id}')
|
||||
|
||||
return person_id
|
||||
# ### END ### API Person Methods ### create_person_kiss() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### update_person_kiss() ###
|
||||
# Updated 2022-01-06
|
||||
def update_person_kiss(
|
||||
person_id: int,
|
||||
person_dict_obj: Person_Base,
|
||||
contact_id: int|None = None,
|
||||
organization_id: int|None = None,
|
||||
user_id: int|None = None,
|
||||
):
|
||||
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(person_dict_obj))
|
||||
if isinstance(person_dict_obj, dict):
|
||||
person_dict = person_dict_obj
|
||||
try:
|
||||
person_obj = Person_Base(**person_dict)
|
||||
log.debug(person_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
person_obj = person_dict_obj
|
||||
|
||||
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
|
||||
|
||||
# ### SECTION ### Process data
|
||||
person_obj.id = person_id # Is this needed?
|
||||
person_dict['id'] = person_id
|
||||
|
||||
# Look for a contact_id in the contact_obj
|
||||
if contact_id: pass
|
||||
elif contact_id := person_obj.contact.id: pass
|
||||
|
||||
# Look for a user_id in the person_obj
|
||||
if user_id: pass
|
||||
elif user_id := person_obj.user.id: pass
|
||||
# if user_id:
|
||||
# # Link to an existing user
|
||||
# log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
# person_obj.user_id = user_id # Is this needed?
|
||||
# person_dict['user_id'] = user_id
|
||||
|
||||
if person_dict_up_result := sql_update(
|
||||
data = person_dict,
|
||||
table_name = 'person',
|
||||
rm_id_random = True,
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Person not updated.')
|
||||
log.debug(person_dict_up_result)
|
||||
return False
|
||||
|
||||
log.debug(person_dict_up_result)
|
||||
|
||||
if contact_id and person_obj.contact:
|
||||
log.info('Updating Contact object')
|
||||
if contact_update_result := update_contact_obj(
|
||||
contact_id = contact_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
elif person_obj.contact:
|
||||
log.info('Creating Contact object')
|
||||
if contact_create_result := create_contact_obj(
|
||||
account_id = account_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
if user_id and person_obj.user:
|
||||
log.info('Updating User object')
|
||||
# Link to an existing user
|
||||
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
person_obj.user_id = user_id # Is this needed?
|
||||
person_dict['user_id'] = user_id
|
||||
|
||||
from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_update_result := update_user_obj(
|
||||
user_id = user_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
elif person_obj.user:
|
||||
log.info('Creating User object')
|
||||
from app.methods.user_methods import create_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_create_result := create_user_obj(
|
||||
account_id = account_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
allow_update = True, # WARNING NOTE: This will allow an existing user record to be updated.
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
return True
|
||||
# ### END ### API Person Methods ### update_person_kiss() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### load_person_obj() ###
|
||||
# Updated 2021-12-15
|
||||
def load_person_obj(
|
||||
@@ -430,228 +653,6 @@ def get_person_rec_list(
|
||||
# data_dict = data_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude=['contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'])
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### create_person_kiss() ###
|
||||
# Updated 2022-01-06
|
||||
def create_person_kiss(
|
||||
account_id: int,
|
||||
person_dict_obj: Person_Base,
|
||||
contact_id: int|None = None,
|
||||
organization_id: int|None = None,
|
||||
user_id: int|None = None,
|
||||
) -> 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(person_dict_obj))
|
||||
if isinstance(person_dict_obj, dict):
|
||||
person_dict = person_dict_obj
|
||||
try:
|
||||
person_obj = Person_Base(**person_dict)
|
||||
log.debug(person_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
person_obj = person_dict_obj
|
||||
# person_obj.account_id = account_id
|
||||
|
||||
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
|
||||
|
||||
# ### SECTION ### Process data
|
||||
# Look for an account_id in the person_obj
|
||||
# if account_id: pass
|
||||
# elif account_id := person_obj.account_id: pass
|
||||
|
||||
person_obj.account_id = account_id # Is this needed?
|
||||
person_dict['account_id'] = account_id
|
||||
|
||||
# Look for a contact_id in the contact_obj
|
||||
if contact_id: pass
|
||||
elif contact_id := person_obj.contact.id: pass
|
||||
|
||||
# Look for a user_id in the person_obj
|
||||
if user_id: pass
|
||||
elif user_id := person_obj.user.id: pass
|
||||
# if user_id:
|
||||
# # Link to an existing user
|
||||
# log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
# person_obj.user_id = user_id # Is this needed?
|
||||
# person_dict['user_id'] = user_id
|
||||
|
||||
if person_dict_in_result := sql_insert(
|
||||
data = person_dict,
|
||||
table_name = 'person',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Person not created.')
|
||||
log.debug(person_dict_in_result)
|
||||
return False
|
||||
|
||||
log.debug(person_dict_in_result)
|
||||
person_id = person_dict_in_result
|
||||
|
||||
if contact_id and person_obj.contact:
|
||||
log.info('Updating Contact object')
|
||||
if contact_update_result := update_contact_obj(
|
||||
contact_id = contact_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
elif person_obj.contact:
|
||||
log.info('Creating Contact object')
|
||||
if contact_create_result := create_contact_obj(
|
||||
account_id = account_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
if user_id and person_obj.user:
|
||||
log.info('Updating User object')
|
||||
# Link to an existing user
|
||||
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
person_obj.user_id = user_id # Is this needed?
|
||||
person_dict['user_id'] = user_id
|
||||
|
||||
from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_update_result := update_user_obj(
|
||||
user_id = user_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
elif person_obj.user:
|
||||
log.info('Creating User object')
|
||||
from app.methods.user_methods import create_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_create_result := create_user_obj(
|
||||
account_id = account_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
allow_update = True, # WARNING NOTE: This will allow an existing user record to be updated.
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
log.info(f'Returning the Person ID: {person_id}')
|
||||
|
||||
return person_id
|
||||
# ### END ### API Person Methods ### create_person_kiss() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### update_person_kiss() ###
|
||||
# Updated 2022-01-06
|
||||
def update_person_kiss(
|
||||
person_id: int,
|
||||
person_dict_obj: Person_Base,
|
||||
contact_id: int|None = None,
|
||||
organization_id: int|None = None,
|
||||
user_id: int|None = None,
|
||||
):
|
||||
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(person_dict_obj))
|
||||
if isinstance(person_dict_obj, dict):
|
||||
person_dict = person_dict_obj
|
||||
try:
|
||||
person_obj = Person_Base(**person_dict)
|
||||
log.debug(person_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
person_obj = person_dict_obj
|
||||
|
||||
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
|
||||
|
||||
# ### SECTION ### Process data
|
||||
person_obj.id = person_id # Is this needed?
|
||||
person_dict['id'] = person_id
|
||||
|
||||
# Look for a contact_id in the contact_obj
|
||||
if contact_id: pass
|
||||
elif contact_id := person_obj.contact.id: pass
|
||||
|
||||
# Look for a user_id in the person_obj
|
||||
if user_id: pass
|
||||
elif user_id := person_obj.user.id: pass
|
||||
# if user_id:
|
||||
# # Link to an existing user
|
||||
# log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
# person_obj.user_id = user_id # Is this needed?
|
||||
# person_dict['user_id'] = user_id
|
||||
|
||||
if person_dict_up_result := sql_update(
|
||||
data = person_dict,
|
||||
table_name = 'person',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'Person not updated.')
|
||||
log.debug(person_dict_up_result)
|
||||
return False
|
||||
|
||||
log.debug(person_dict_up_result)
|
||||
|
||||
if contact_id and person_obj.contact:
|
||||
log.info('Updating Contact object')
|
||||
if contact_update_result := update_contact_obj(
|
||||
contact_id = contact_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
elif person_obj.contact:
|
||||
log.info('Creating Contact object')
|
||||
if contact_create_result := create_contact_obj(
|
||||
account_id = account_id,
|
||||
contact_dict_obj = person_obj.contact,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
): pass
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
if user_id and person_obj.user:
|
||||
log.info('Updating User object')
|
||||
# Link to an existing user
|
||||
log.info(f'Adding user_id to person_dict. User ID: {user_id}')
|
||||
person_obj.user_id = user_id # Is this needed?
|
||||
person_dict['user_id'] = user_id
|
||||
|
||||
from app.methods.user_methods import update_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_update_result := update_user_obj(
|
||||
user_id = user_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
elif person_obj.user:
|
||||
log.info('Creating User object')
|
||||
from app.methods.user_methods import create_user_obj # NOTE: This creates a loop if outside function
|
||||
if user_create_result := create_user_obj(
|
||||
account_id = account_id,
|
||||
user_dict_obj = person_obj.user,
|
||||
person_id = person_id,
|
||||
allow_update = True, # WARNING NOTE: This will allow an existing user record to be updated.
|
||||
): pass # NOTE: There is a trigger that will update the person record with the new user ID.
|
||||
else: return False
|
||||
else: pass
|
||||
|
||||
return True
|
||||
# ### END ### API Person Methods ### update_person_kiss() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### create_update_person_obj_v4b() ###
|
||||
|
||||
@@ -134,7 +134,12 @@ def create_user_obj(
|
||||
log.info(f'Avoiding duplicate username is now allowed. Suggested Username: {username}')
|
||||
new_username = username+'-'+str(random.randint(10, 99))
|
||||
user_dict['username'] = new_username
|
||||
if user_dict_in_result := sql_insert(data=user_dict, table_name='user', rm_id_random=True, id_random_length=8): pass
|
||||
if user_dict_in_result := sql_insert(
|
||||
data = user_dict,
|
||||
table_name = 'user',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'User not created.')
|
||||
log.debug(user_dict_in_result)
|
||||
@@ -148,7 +153,12 @@ def create_user_obj(
|
||||
|
||||
log.debug(f'Returning the existing user_id: {user_id}')
|
||||
else:
|
||||
if user_dict_in_result := sql_insert(data=user_dict, table_name='user', rm_id_random=True, id_random_length=8): pass
|
||||
if user_dict_in_result := sql_insert(
|
||||
data = user_dict,
|
||||
table_name = 'user',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'User not created.')
|
||||
log.debug(user_dict_in_result)
|
||||
@@ -322,7 +332,11 @@ def update_user_obj(
|
||||
log.debug(user_obj)
|
||||
log.debug(user_dict)
|
||||
|
||||
if user_dict_up_result := sql_update(data=user_dict, table_name='user', rm_id_random=True): pass
|
||||
if user_dict_up_result := sql_update(
|
||||
data = user_dict,
|
||||
table_name = 'user',
|
||||
rm_id_random = True
|
||||
): pass
|
||||
else:
|
||||
log.warning(f'User not updated.')
|
||||
log.debug(user_dict_up_result)
|
||||
|
||||
Reference in New Issue
Block a user