Code clean up. Bug fixes for person, user, contact, and address methods. Fix bug for get_account_id_w_for_type_id()
This commit is contained in:
@@ -150,8 +150,8 @@ def load_account_obj(
|
||||
# Updated 2021-06-17
|
||||
if inc_address_list:
|
||||
if address_rec_list_result := get_address_rec_list(
|
||||
for_obj_type = 'account',
|
||||
for_obj_id = account_id,
|
||||
for_type = 'account', # 'account' is a special case
|
||||
for_id = account_id,
|
||||
limit = limit,
|
||||
enabled = enabled,
|
||||
):
|
||||
@@ -193,8 +193,8 @@ def load_account_obj(
|
||||
# Updated 2021-06-17
|
||||
if inc_contact_list:
|
||||
if contact_rec_list_result := get_contact_rec_list(
|
||||
for_obj_type = 'account',
|
||||
for_obj_id = account_id,
|
||||
for_type = 'account', # 'account' is a special case
|
||||
for_id = account_id,
|
||||
limit = limit,
|
||||
enabled = enabled,
|
||||
):
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import get_account_id_w_for_type_id, redis_lookup_id_random, sql_insert, sql_select, sql_update
|
||||
from app.lib_general import log, logging
|
||||
from app.lib_general import log, logging, logger_reset
|
||||
|
||||
from app.models.address_models import Address_Base
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
@@ -44,32 +44,40 @@ def load_address_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Address Methods ### get_address_rec_list() ###
|
||||
# Updated 2022-01-07
|
||||
@logger_reset
|
||||
def get_address_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
limit: int = 1000,
|
||||
for_type: str, # 'account' is a special case
|
||||
for_id: str,
|
||||
limit: int = 500,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # 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
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_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'`tbl`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
if for_type == 'account':
|
||||
sql_for_type_id = f'`address`.account_id = :for_id'
|
||||
else:
|
||||
sql_for_type_id = f'`address`.for_type = :for_type AND `address`.for_id = :for_id'
|
||||
|
||||
data = {}
|
||||
# data[f'{for_type}_id'] = for_id
|
||||
data['for_type'] = for_type
|
||||
data['for_id'] = for_id
|
||||
# sql_obj_type_id = f'`address`.{for_type}_id = :{for_type}_id'
|
||||
|
||||
if enabled in ['enabled', 'disabled', 'all']:
|
||||
if enabled == 'enabled':
|
||||
data['enable'] = True
|
||||
sql_enabled = f'AND `address`.enable = :enable'
|
||||
elif enabled == 'disabled':
|
||||
data['enable'] = False
|
||||
sql_enabled = f'AND `address`.enable = :enable'
|
||||
elif enabled == 'all':
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
@@ -78,20 +86,20 @@ def get_address_rec_list(
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'address_id', `tbl`.id_random AS 'address_id_random'
|
||||
FROM `address` AS `tbl`
|
||||
SELECT `address`.id AS 'address_id', `address`.id_random AS 'address_id_random'
|
||||
FROM `address` AS `address`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_for_type_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
ORDER BY `address`.created_on DESC, `address`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if address_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
address_rec_li = address_rec_li_result
|
||||
else:
|
||||
address_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
else: # [] or False
|
||||
address_rec_li = address_rec_li_result
|
||||
|
||||
log.debug(address_rec_li_result)
|
||||
|
||||
return address_rec_li
|
||||
@@ -231,7 +239,7 @@ def create_address_obj(
|
||||
for_id: int|str = None,
|
||||
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.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
@@ -244,6 +252,7 @@ def create_address_obj(
|
||||
|
||||
log.info('Create dictionary or Pydantic object')
|
||||
log.debug(type(address_dict_obj))
|
||||
log.debug(address_dict_obj)
|
||||
if isinstance(address_dict_obj, dict):
|
||||
address_dict = address_dict_obj
|
||||
try:
|
||||
@@ -257,31 +266,7 @@ def create_address_obj(
|
||||
address_obj.account_id = account_id
|
||||
|
||||
address_dict = address_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
|
||||
|
||||
# log.debug(type(address_dict_obj))
|
||||
# if isinstance(address_dict_obj, dict):
|
||||
# if account_id:
|
||||
# address_obj_new['account_id'] = account_id
|
||||
# if for_type:
|
||||
# address_obj_new['for_type'] = for_type
|
||||
# if for_id:
|
||||
# address_obj_new['for_id'] = for_id
|
||||
# try:
|
||||
# address_obj_new = Address_Base(**address_obj_new)
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(address_obj_new)
|
||||
# except ValidationError as e:
|
||||
# log.error(e.json())
|
||||
# return False
|
||||
# else:
|
||||
# if account_id:
|
||||
# address_obj_new.account_id = account_id
|
||||
# if for_type:
|
||||
# address_obj_new.for_type = for_type
|
||||
# if for_id:
|
||||
# address_obj_new.for_id = for_id
|
||||
|
||||
# address_dict = address_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
|
||||
log.debug(address_dict)
|
||||
|
||||
# ### SECTION ### Process data
|
||||
# Look for an account_id in the address_obj
|
||||
@@ -296,6 +281,16 @@ def create_address_obj(
|
||||
address_dict['for_type'] = for_type
|
||||
address_dict['for_id'] = for_id
|
||||
|
||||
if address_obj.id:
|
||||
log.warning(f'There should not be an Address ID: {address_obj.id}')
|
||||
return False
|
||||
else:
|
||||
log.info(f'Should there be a check for an existing address with? For Type: {for_type}; For ID: {for_id}')
|
||||
if get_address_rec_result := get_address_rec_list(for_type=for_type, for_id=for_id):
|
||||
log.warning(f'One or more addresses were found with: For Type: {for_type}; For ID: {for_id}')
|
||||
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
|
||||
else:
|
||||
return False
|
||||
@@ -303,7 +298,7 @@ def create_address_obj(
|
||||
log.debug(address_dict_in_result)
|
||||
address_id = address_dict_in_result
|
||||
|
||||
log.inf(f'Returning the new address_id: {address_id}')
|
||||
log.info(f'Returning the new address_id: {address_id}')
|
||||
|
||||
return address_id
|
||||
# ### END ### API Address Methods ### create_address_obj() ###
|
||||
@@ -319,7 +314,7 @@ def update_address_obj(
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
) -> bool:
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
|
||||
@@ -66,34 +66,38 @@ def load_contact_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### get_contact_rec_list() ###
|
||||
# Updated 2021-12-13
|
||||
# Updated 2022-01-07
|
||||
@logger_reset
|
||||
def get_contact_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
limit: int = 1000,
|
||||
for_type: str, # 'account' is a special case
|
||||
for_id: str,
|
||||
limit: int = 500,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.INFO) # 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
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_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'`contact`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `contact`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `contact`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
if for_type == 'account':
|
||||
sql_for_type_id = f'`contact`.account_id = :for_id'
|
||||
else:
|
||||
sql_for_type_id = f'`contact`.for_type = :for_type AND `contact`.for_id = :for_id'
|
||||
|
||||
data = {}
|
||||
data['for_type'] = for_type
|
||||
data['for_id'] = for_id
|
||||
|
||||
if enabled in ['enabled', 'disabled', 'all']:
|
||||
if enabled == 'enabled':
|
||||
data['enable'] = True
|
||||
sql_enabled = f'AND `contact`.enable = :enable'
|
||||
elif enabled == 'disabled':
|
||||
data['enable'] = False
|
||||
sql_enabled = f'AND `contact`.enable = :enable'
|
||||
elif enabled == 'all':
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
@@ -105,7 +109,7 @@ def get_contact_rec_list(
|
||||
SELECT `contact`.id AS 'contact_id', `contact`.id_random AS 'contact_id_random'
|
||||
FROM `contact` AS `contact`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_for_type_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `contact`.created_on DESC, `contact`.updated_on DESC
|
||||
{sql_limit};
|
||||
@@ -468,7 +472,7 @@ def update_contact_obj(
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
return_dict: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
@@ -482,6 +486,7 @@ def update_contact_obj(
|
||||
|
||||
log.info('Create dictionary or Pydantic object')
|
||||
log.debug(type(contact_dict_obj))
|
||||
log.debug(contact_dict_obj)
|
||||
if isinstance(contact_dict_obj, dict):
|
||||
contact_dict = contact_dict_obj
|
||||
try:
|
||||
@@ -494,6 +499,7 @@ def update_contact_obj(
|
||||
contact_obj = contact_dict_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(contact_dict)
|
||||
|
||||
# ### SECTION ### Process data
|
||||
contact_obj.id = contact_id # Is this needed?
|
||||
@@ -513,6 +519,8 @@ def update_contact_obj(
|
||||
# Look for an address_id in the contact_obj if one was not passed separately
|
||||
if address_id: pass
|
||||
elif address_id := contact_obj.address.id: pass
|
||||
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
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user