From 5489b80ff0a127480bb3731726efca5b68c933ca Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Fri, 7 Jan 2022 13:35:44 -0500 Subject: [PATCH] Code clean up. Bug fixes for person, user, contact, and address methods. Fix bug for get_account_id_w_for_type_id() --- app/db_sql.py | 12 ++-- app/methods/account_methods.py | 8 +-- app/methods/address_methods.py | 105 ++++++++++++++++----------------- app/methods/contact_methods.py | 50 +++++++++------- app/routers/person.py | 42 ++++++++----- 5 files changed, 117 insertions(+), 100 deletions(-) diff --git a/app/db_sql.py b/app/db_sql.py index b62ded3..0a82ad7 100644 --- a/app/db_sql.py +++ b/app/db_sql.py @@ -1218,10 +1218,12 @@ def lookup_id_random_pop( # ### BEGIN ### API DB SQL Methods ### get_account_id_w_for_type_id() ### -# Updated 2021-08-24 +# NOTE: This is only useful for a few tables that have account_id as a field or views that have it included. +# address, contact, event, people, user +# Updated 2022-01-07 @logger_reset def get_account_id_w_for_type_id( - for_type: str, + for_type: str, # This is the table name for_id: int|str, ) -> bool|int|None: log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL @@ -1235,9 +1237,9 @@ def get_account_id_w_for_type_id( data['for_id'] = for_id sql = f""" - SELECT `for`.id AS 'for_id', `for`.id_random AS 'for_id_random', `for`.account_id AS account_id - FROM :for_type AS `for` - WHERE `for`.id = :for_id + SELECT `tbl`.id AS 'for_id', `tbl`.id_random AS 'for_id_random', `tbl`.account_id AS account_id + FROM `{for_type}` AS `tbl` + WHERE `tbl`.id = :for_id LIMIT 1; """ diff --git a/app/methods/account_methods.py b/app/methods/account_methods.py index 9c10f28..8d526e1 100644 --- a/app/methods/account_methods.py +++ b/app/methods/account_methods.py @@ -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, ): diff --git a/app/methods/address_methods.py b/app/methods/address_methods.py index c06eb22..933d14d 100644 --- a/app/methods/address_methods.py +++ b/app/methods/address_methods.py @@ -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 diff --git a/app/methods/contact_methods.py b/app/methods/contact_methods.py index ae7117a..914f500 100644 --- a/app/methods/contact_methods.py +++ b/app/methods/contact_methods.py @@ -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: diff --git a/app/routers/person.py b/app/routers/person.py index fa74174..bd22710 100644 --- a/app/routers/person.py +++ b/app/routers/person.py @@ -27,7 +27,13 @@ async def post_person_obj( contact_id: str = Query(None, min_length=11, max_length=22), organization_id: str = Query(None, min_length=11, max_length=22), user_id: str = Query(None, min_length=11, max_length=22), + + inc_address: bool = False, + inc_contact: bool = False, + inc_organization: bool = False, + inc_user: bool = False, return_obj: bool = True, + commons: Common_Route_Params = Depends(common_route_params), ): log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL @@ -62,10 +68,10 @@ async def post_person_obj( if return_obj: person_obj = load_person_obj( person_id = person_id, - inc_address = False, - inc_contact = False, - inc_organization = False, - inc_user = False + inc_address = inc_address, + inc_contact = inc_contact, + inc_organization = inc_organization, + inc_user = inc_user ).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset) data = person_obj else: @@ -83,7 +89,13 @@ async def patch_person_obj( contact_id: str = Query(None, min_length=11, max_length=22), organization_id: str = Query(None, min_length=11, max_length=22), user_id: str = Query(None, min_length=11, max_length=22), + + inc_address: bool = False, + inc_contact: bool = False, + inc_organization: bool = False, + inc_user: bool = False, return_obj: Optional[bool] = True, + commons: Common_Route_Params = Depends(common_route_params), ): log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL @@ -121,10 +133,10 @@ async def patch_person_obj( if return_obj: person_obj = load_person_obj( person_id = person_id, - inc_address = False, - inc_contact = False, - inc_organization = False, - inc_user = False + inc_address = inc_address, + inc_contact = inc_contact, + inc_organization = inc_organization, + inc_user = inc_user ).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset) data = person_obj else: @@ -153,7 +165,7 @@ async def v3_post_person_obj_new( commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if create_update_person_obj_result := create_update_person_obj_v4b( @@ -214,7 +226,7 @@ async def v3_patch_person_obj_exist( commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass @@ -272,7 +284,7 @@ async def post_person_json( return_obj: Optional[bool] = True, commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if person_obj_in_result := create_update_person_obj_v4b( @@ -307,7 +319,7 @@ async def patch_person_json( return_obj: Optional[bool] = True, commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass @@ -339,7 +351,7 @@ async def get_person_obj_li( for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22), commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) obj_type = 'person' @@ -366,7 +378,7 @@ async def person_obj_external_id( inc_user_role_list: bool = False, commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) account_id = commons.x_account_id @@ -408,7 +420,7 @@ async def lookup_email( inc_user_role_list: bool = False, commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) account_id = commons.x_account_id