Working on contact and address create update v4
This commit is contained in:
@@ -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 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.models.address_models import Address_Base
|
||||
@@ -98,6 +98,130 @@ def get_address_rec_list(
|
||||
# ### END ### API Address Methods ### 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
|
||||
def create_update_address_obj_v4(
|
||||
address_dict_obj: Address_Base|dict,
|
||||
address_id: int|str = None,
|
||||
account_id: int|str = None,
|
||||
for_type: str = None,
|
||||
for_id: int|str = None,
|
||||
create_sub_obj: bool = False,
|
||||
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.debug(locals())
|
||||
|
||||
log.info('Checking requirements...')
|
||||
if address_id:
|
||||
log.info(f'Address ID passed. Update existing Address. Address ID: {address_id}')
|
||||
|
||||
if address_id := redis_lookup_id_random(record_id_random=address_id, table_name='address'): pass
|
||||
else:
|
||||
log.error('Address ID passed but is invalid. Failed requirement.')
|
||||
return False
|
||||
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else:
|
||||
log.error('Missing or invalid Account ID passed. Not required. Ignoring.')
|
||||
log.info(f'Account ID: {account_id}')
|
||||
|
||||
log.info('Attempting to get Account ID from related object.')
|
||||
if account_id := get_account_id_w_for_type_id(for_type='address', for_id=address_id): pass
|
||||
else:
|
||||
log.error('Unable to get Account ID from related object.')
|
||||
False
|
||||
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
|
||||
else:
|
||||
log.error('Missing or invalid For Type and For ID ID passed. Not required. Ignoring.')
|
||||
log.info(f'For Type: {for_type} and For ID: {for_id}')
|
||||
else:
|
||||
log.info('No Address ID passed. Create new Address. Required: Account ID, For Type, For ID')
|
||||
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
|
||||
else:
|
||||
log.error('Missing or invalid For Type and For ID ID passed. Failed requirement.')
|
||||
log.info(f'For Type: {for_type} and For ID: {for_id}')
|
||||
return False
|
||||
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else:
|
||||
log.error('Missing or invalid Account ID passed. Failed requirement.')
|
||||
log.info(f'Account ID: {account_id}')
|
||||
if for_type and for_id:
|
||||
log.info('Attempting to get Account ID from related object.')
|
||||
if account_id := get_account_id_w_for_type_id(for_type=for_type, for_id=for_id): pass
|
||||
else:
|
||||
log.error('Unable to get Account ID from related object.')
|
||||
False
|
||||
else:
|
||||
return False
|
||||
|
||||
log.debug(type(address_dict_obj))
|
||||
if isinstance(address_dict_obj, dict):
|
||||
address_dict = address_dict_obj
|
||||
if address_id:
|
||||
address_dict['address_id'] = address_id
|
||||
if account_id:
|
||||
address_dict['account_id'] = account_id
|
||||
if for_type:
|
||||
address_dict['for_type'] = for_type
|
||||
if for_id:
|
||||
address_dict['for_id'] = for_id
|
||||
try:
|
||||
address_obj = Address_Base(**address_dict)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(address_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
address_obj = address_dict_obj
|
||||
if address_id:
|
||||
# NOTE: Can't update the ID alias if it was never set.
|
||||
address_obj.id = address_id
|
||||
if account_id:
|
||||
address_obj.account_id = account_id
|
||||
if for_type:
|
||||
address_obj.for_type = for_type
|
||||
if for_id:
|
||||
address_obj.for_id = for_id
|
||||
|
||||
address_dict = address_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
|
||||
|
||||
if address_id:
|
||||
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. Address ID: {address_id}')
|
||||
log.debug(address_dict_up_result)
|
||||
return False
|
||||
log.debug(address_dict_up_result)
|
||||
else:
|
||||
if address_dict_in_result := sql_insert(data=address_dict, table_name='address', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
log.warning(f'Address not created.')
|
||||
log.debug(address_dict_in_result)
|
||||
return False
|
||||
log.debug(address_dict_in_result)
|
||||
|
||||
address_id = address_dict_in_result
|
||||
|
||||
address_outline = {}
|
||||
address_outline['address_id'] = address_id
|
||||
# Should this outline include for_type and for_id? Probably later.
|
||||
|
||||
if return_outline:
|
||||
log.debug(f'Returning the Address Outline: {address_outline}')
|
||||
return address_outline
|
||||
else:
|
||||
log.debug(f'Returning the Address ID: {address_id}')
|
||||
return address_id
|
||||
# ### END ### API Address Methods ### create_update_address_obj_v4() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Address Methods ### create_address_obj() ###
|
||||
# NOTE: This will create an address.
|
||||
# Reviewed and updated 2021-08-10
|
||||
|
||||
Reference in New Issue
Block a user