Moving things to use the common_route_params. Rewriting most of things order and order_cart related! Updating address and contact related. General code clean up.

This commit is contained in:
Scott Idem
2022-01-18 18:49:23 -05:00
parent 49d8a7f0dc
commit 24807db81c
10 changed files with 216 additions and 502 deletions

View File

@@ -12,6 +12,7 @@ from app.models.common_field_schema import default_num_bytes
# ### BEGIN ### API Address Methods ### load_address_obj() ###
@logger_reset
def load_address_obj(
address_id:int|str,
enabled: str = 'enabled', # enabled, disabled, all # Probably not needed for the address
@@ -54,7 +55,7 @@ def get_address_rec_list(
limit: int = 500,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
@@ -98,6 +99,7 @@ def get_address_rec_list(
# ### BEGIN ### API Address Methods ### create_update_address_obj_v4() ###
# NOTE: This will create or update an address.
# Rewrite and updated 2021-08-25
@logger_reset
def create_update_address_obj_v4(
address_dict_obj: Address_Base|dict,
address_id: int|str = None,
@@ -221,6 +223,7 @@ def create_update_address_obj_v4(
# ### BEGIN ### API Address Methods ### create_address_obj() ###
# Updated 2022-01-06
@logger_reset
def create_address_obj(
account_id: int|str,
address_dict_obj: Address_Base,
@@ -295,6 +298,7 @@ def create_address_obj(
# ### BEGIN ### API Address Methods ### update_address_obj() ###
# Updated 2022-01-06
@logger_reset
def update_address_obj(
address_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
address_dict_obj: Address_Base,
@@ -361,6 +365,7 @@ def update_address_obj(
# ### BEGIN ### API Address Methods ### create_update_address_obj() ###
@logger_reset
def create_update_address_obj(
address_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value.
address_obj: Address_Base,

View File

@@ -14,6 +14,7 @@ from app.models.contact_models import Contact_Base
# ### BEGIN ### API Contact Methods ### load_contact_obj() ###
@logger_reset
def load_contact_obj(
contact_id: int|str,
enabled: str = 'enabled', # enabled, disabled, all
@@ -24,7 +25,7 @@ def load_contact_obj(
model_as_dict: bool = False,
inc_address: bool = False
) -> Contact_Base|dict|bool:
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
@@ -76,7 +77,7 @@ def get_contact_rec_list(
limit: int = 500,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
@@ -118,6 +119,7 @@ def get_contact_rec_list(
# ### BEGIN ### API Contact Methods ### get_account_id_w_contact_id() ###
# Updated 2021-08-24
@logger_reset
def get_account_id_w_contact_id(
contact_id: int|str,
) -> bool|int|None:
@@ -137,9 +139,7 @@ def get_account_id_w_contact_id(
LIMIT 1;
"""
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if contact_data_result := sql_select(data=data, sql=sql):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(contact_data_result)
if account_id := contact_data_result.get('account_id', None): return account_id
else: return False
@@ -151,6 +151,7 @@ def get_account_id_w_contact_id(
# ### BEGIN ### API Contact Methods ### create_update_contact_obj_v4() ###
# NOTE: This will create or update a contact.
# Rewrite and updated 2021-08-25
@logger_reset
def create_update_contact_obj_v4(
contact_dict_obj: Contact_Base|dict,
contact_id: int|str = None,
@@ -161,7 +162,7 @@ def create_update_contact_obj_v4(
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
return_outline: bool = False,
) -> int|bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('Checking requirements...')
@@ -307,6 +308,7 @@ def create_update_contact_obj_v4(
# NOTE: This will create a contact and then also create a linked address if contact_obj.address data is passed.
# NOTE: In the future it should be required that account_id, for_type, and for_id should be passed separately. account_id might not be required *if* it can be looked up based on for_type and for_id.
# Updated 2022-01-06
@logger_reset
def create_contact_obj(
account_id: int|str,
contact_dict_obj: Contact_Base,
@@ -316,7 +318,7 @@ def create_contact_obj(
create_sub_obj: bool = False,
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
) -> int|bool:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# ### SECTION ### Secondary data validation
@@ -347,31 +349,6 @@ def create_contact_obj(
contact_dict = contact_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'address_id', 'address_id_random', 'created_on', 'updated_on'})
# log.debug(type(contact_dict_obj))
# if isinstance(contact_dict_obj, dict):
# if account_id:
# contact_dict_obj['account_id'] = account_id
# if for_type:
# contact_dict_obj['for_type'] = for_type
# if for_id:
# contact_dict_obj['for_id'] = for_id
# try:
# contact_obj = Contact_Base(**contact_dict_obj)
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(contact_obj)
# except ValidationError as e:
# log.error(e.json())
# return False
# else:
# if account_id:
# contact_obj.account_id = account_id
# if for_type:
# contact_obj.for_type = for_type
# if for_id:
# contact_obj.for_id = for_id
# contact_dict = contact_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'})
# ### SECTION ### Process data
# Look for an account_id in the contact_obj
if account_id: pass
@@ -451,6 +428,7 @@ def create_contact_obj(
# ### BEGIN ### API Contact Methods ### update_contact_obj() ###
# NOTE: This will update a contact and then also create or update a linked address if contact_obj.address data is passed.
# Updated 2022-01-06
@logger_reset
def update_contact_obj(
contact_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
contact_dict_obj: Contact_Base,
@@ -539,127 +517,14 @@ def update_contact_obj(
else: return False
else: pass
# log.debug(contact_obj_up)
# log.debug(contact_obj_up.dict(by_alias=True, exclude_unset=True))
log.debug(contact_obj.dict(by_alias=False, exclude_unset=True))
# log.debug(contact_obj_up.dict(by_alias=False, exclude_unset=False))
#contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True)
# if contact_obj_up.address:
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# address_obj_unknown = contact_obj_up.address
# log.debug(address_obj_unknown)
# if isinstance(contact_obj_up.address, dict):
# address_id = address_obj_unknown.get('address_id_random', None)
# else:
# address_id = address_obj_unknown.id
# if address_id:
# # from app.methods.address_methods import update_address_obj_v3
# if update_address_obj_result := update_address_obj(
# address_id = address_id,
# address_obj_up = address_obj_unknown,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
# ):
# address_id = update_address_obj_result
# log.info(f'Address updated. Address ID: {address_id}')
# else:
# log.warning(f'Address not updated. Contact ID: {contact_id}')
# log.debug(update_address_obj_result)
# address_id = None
# if fail_any: return False
# if isinstance(update_address_obj_result, int):
# address_id = update_address_obj_result
# log.info(f'Address updated. Address ID: {address_id}')
# else:
# log.warning(f'Address not updated. Contact ID: {contact_id}')
# log.debug(update_address_obj_result)
# address_id = None
# if fail_any: return False
# else:
# log.info(f'No Address ID found.')
# # from app.methods.address_methods import create_address_obj_v3
# if create_address_obj_result := create_address_obj(
# account_id = account_id,
# for_type = 'contact',
# for_id = contact_id,
# address_obj_new = address_obj_unknown,
# fail_any = fail_any,
# ):
# if isinstance(create_address_obj_result, int):
# address_id = create_address_obj_result
# log.info(f'Address created. Address ID: {address_id}')
# else:
# log.warning(f'Address not created. Contact ID: {contact_id}')
# log.debug(create_address_obj_result)
# address_id = None
# if fail_any: return False
# else:
# log.warning(f'Address not created. Contact ID: {contact_id}')
# log.debug(create_address_obj_result)
# address_id = None
# if fail_any: return False
# # return_dict['address_id'] = address_id
# else:
# log.info('Address not found or not in a dict.')
# pass
# if contact_obj_up.address_id and contact_obj_up.address:
# address_id = contact_obj_up.address_id
# address_obj_up = contact_obj_up.address
# log.debug(address_id)
# log.debug(address_obj_up)
# if address_obj_up_result := update_address_obj(
# address_id = address_id,
# address_obj_up = address_obj_up,
# create_sub_obj = create_sub_obj,
# ):
# log.debug(address_obj_up_result)
# else:
# log.debug(address_obj_up_result)
# return False
# elif contact_obj_up.address and not contact_obj_up.address.id:
# # NOTE: This will blindly create a new address even if there was one associated but the contact_obj.address_id was not found.
# address_obj_in = contact_obj_up.address
# log.debug(address_obj_in)
# if address_obj_in_result := create_address_obj(
# address_obj_new = address_obj_in,
# account_id = account_id,
# for_type = 'contact',
# for_id = contact_id,
# ):
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(address_obj_in_result)
# # NOTE: This last update should no longer be needed now that the contact_obj.address_id is not supposed to be used.
# # Need to update the contact with the new address_id
# contact_obj_up.address_id = address_obj_in_result # REMOVE
# else:
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(address_obj_in_result)
# return False
# contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'address'})
# log.debug(contact_dict_up)
# if contact_obj_up_result := sql_update(data=contact_dict_up, table_name='contact', rm_id_random=True):
# log.debug(contact_obj_up_result)
# return True
# else:
# log.debug(contact_obj_up_result)
# return False
return True
# ### END ### API Contact Methods ### update_contact_obj() ###
# ### BEGIN ### API Contact Methods ### create_update_contact_obj() ###
@logger_reset
def create_update_contact_obj(
contact_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value.
contact_obj: Contact_Base,

View File

@@ -4,7 +4,7 @@ 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_insert, sql_select, sql_update
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
@@ -35,9 +35,11 @@ def create_order_line_obj(order_line_obj_new:Order_Line_Base):
# ### BEGIN ### API Order Line Methods ### load_order_line_obj() ###
# Updated 2021-11-19
def load_order_line_obj(
order_line_id: int|str,
limit: int = 1000,
# 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
@@ -50,16 +52,14 @@ def load_order_line_obj(
if order_line_rec := sql_select(table_name='v_order_line', record_id=order_line_id): pass
else: return False
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(order_line_rec)
try:
order_line_obj = Order_Line_Base(**order_line_rec)
log.debug(order_line_obj)
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
@@ -72,6 +72,8 @@ def load_order_line_obj(
# Updated 2021-11-22
def load_order_line_obj_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
@@ -166,7 +168,8 @@ def get_order_line_rec_list(
status: str = 'closed', # started, in progress, complete, all
full_detail: bool = False,
# enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1000,
limit: int = 500,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -228,11 +231,8 @@ def get_order_line_rec_list(
else:
sql_from_to_datetime = ''
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
# 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)

View File

@@ -4,8 +4,8 @@ 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_insert_or_update, sql_select
from app.lib_general import log, logging
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.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
@@ -19,6 +19,7 @@ from app.models.order_line_models import Order_Line_Base, Order_Line_DB_Base # T
# ### BEGIN ### API Order Methods ### save_order_obj() ###
# @logger_reset
def save_order_obj(order_obj_new:Order_Base, repl_order_line_li:bool=False):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -174,6 +175,7 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_li:bool=False):
# ### 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,
@@ -188,24 +190,23 @@ def load_order_obj(
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
else: return False # None, false bool
if order_rec := sql_select(table_name='v_order', record_id=order_id): pass
else: return False
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
else: return order_rec # None, empty dict, empty list, false bool
log.debug(order_rec)
try:
order_obj = Order_Base(**order_rec)
log.debug(order_obj)
except ValidationError as e:
log.error(e.json())
return False
log.debug(order_obj)
# Updated 2022-01-18
if inc_order_cfg:
@@ -233,7 +234,6 @@ def load_order_obj(
order_line_result_list.append(
load_order_line_obj(
order_line_id = order_line_rec.get('order_line_id'),
limit = limit,
by_alias = by_alias,
exclude_unset = exclude_unset,
model_as_dict = model_as_dict,
@@ -287,6 +287,7 @@ def load_order_obj(
# ### BEGIN ### API Order Methods ### get_order_rec_list() ###
# Updated 2021-12-13
# @logger_reset
def get_order_rec_list(
for_obj_type: str,
for_obj_id: str,
@@ -294,6 +295,7 @@ def get_order_rec_list(
to_datetime: datetime.datetime = None,
# balance_gt: int = 0, # $0 to $99999
status: str = 'closed', # started, in progress, complete, all
# checkout_status: str = 'none', # none, canceled, waiting, success, failed, unknown
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 500,
offset: int = 0,
@@ -307,10 +309,10 @@ def get_order_rec_list(
data = {}
if for_obj_type == 'account' and for_obj_id:
data['account_id'] = redis_lookup_id_random(record_id_random=for_obj_id, table_name='account')
sql_account_person_user = f"""`order`.account_id = :account_id"""
sql_obj_type_id = f'`order`.account_id = :account_id'
elif for_obj_type == 'person' and for_obj_id:
data['person_id'] = redis_lookup_id_random(record_id_random=for_obj_id, table_name='person')
sql_account_person_user = f"""`order`.person_id = :person_id"""
sql_obj_type_id = f'`order`.person_id = :person_id'
else:
return False
@@ -347,7 +349,7 @@ def get_order_rec_list(
else:
sql_from_to_datetime = ''
sql_enabled, data['enable'] = sql_enable_part(table_name='contact', enabled=enabled) # Reasonably safe return str and bool
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)

View File

@@ -128,21 +128,19 @@ def load_person_obj(
else:
if person_rec := sql_select(table_name='v_person', record_id=person_id): pass
else: return person_rec # None or False
log.debug(person_rec)
try:
person_obj = Person_Base(**person_rec)
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
except ValidationError as e:
log.error(e.json())
return False
log.debug(person_obj)
# Updated 2021-12-14
if inc_contact:
log.info('Need to include contact data...')
contact_id = person_rec.get('contact_id', None)
contact_id = person_rec.get('contact_id')
log.debug(contact_id)
if contact_result := load_contact_obj(
contact_id = contact_id,