Working on contact create update v4
This commit is contained in:
@@ -4,10 +4,10 @@ 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.methods.address_methods import create_address_obj, create_update_address_obj, update_address_obj
|
||||
from app.methods.address_methods import create_address_obj, create_update_address_obj, create_update_address_obj_v4, update_address_obj
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.contact_models import Contact_Base
|
||||
@@ -146,6 +146,165 @@ def get_account_id_w_contact_id(
|
||||
# ### END ### API Contact Methods ### get_account_id_w_contact_id() ###
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### 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.
|
||||
# Rewrite and updated 2021-08-25
|
||||
def create_update_contact_obj_v4(
|
||||
contact_dict_obj: Contact_Base|dict,
|
||||
contact_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 contact_id:
|
||||
log.info(f'Contact ID passed. Update existing Contact. Contact ID: {contact_id}')
|
||||
|
||||
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||
else:
|
||||
log.error('Contact 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='contact', for_id=contact_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 Contact ID passed. Create new Contact. 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(contact_dict_obj))
|
||||
if isinstance(contact_dict_obj, dict):
|
||||
contact_dict = contact_dict_obj
|
||||
if contact_id:
|
||||
contact_dict['contact_id'] = contact_id
|
||||
if account_id:
|
||||
contact_dict['account_id'] = account_id
|
||||
if for_type:
|
||||
contact_dict['for_type'] = for_type
|
||||
if for_id:
|
||||
contact_dict['for_id'] = for_id
|
||||
try:
|
||||
contact_obj = Contact_Base(**contact_dict)
|
||||
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:
|
||||
contact_obj = contact_dict_obj
|
||||
if contact_id:
|
||||
# NOTE: Can't update the ID alias if it was never set.
|
||||
contact_obj.id = contact_id
|
||||
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'})
|
||||
|
||||
if contact_id:
|
||||
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. Contact ID: {contact_id}')
|
||||
log.debug(contact_dict_up_result)
|
||||
return False
|
||||
log.debug(contact_dict_up_result)
|
||||
else:
|
||||
if contact_dict_in_result := sql_insert(data=contact_dict, table_name='contact', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
log.warning(f'Contact not created.')
|
||||
log.debug(contact_dict_in_result)
|
||||
return False
|
||||
log.debug(contact_dict_in_result)
|
||||
|
||||
contact_id = contact_dict_in_result
|
||||
|
||||
contact_outline = {}
|
||||
contact_outline['contact_id'] = contact_id
|
||||
|
||||
# NOTE: Use object model version because of better type checking and validations
|
||||
if contact_obj.address:
|
||||
contact_outline['address_id'] = None
|
||||
address_obj = contact_obj.address
|
||||
if address_id := contact_obj.address_id: pass
|
||||
else: address_id = None
|
||||
address_obj.id
|
||||
address_obj.for_type = 'contact'
|
||||
address_obj.for_id = contact_id
|
||||
create_update_address_obj_result = create_update_address_obj_v4(
|
||||
address_dict_obj = address_obj,
|
||||
address_id = address_id,
|
||||
account_id = account_id,
|
||||
for_type = 'contact',
|
||||
for_id = contact_id,
|
||||
fail_any = fail_any,
|
||||
return_outline = return_outline,
|
||||
)
|
||||
if isinstance(create_update_address_obj_result, int):
|
||||
address_id = create_update_address_obj_result
|
||||
elif create_update_address_obj_result == True: pass
|
||||
else:
|
||||
log.warning(f'Create or Update failed while trying create_update_address_obj_v4(): {create_update_address_obj_result}')
|
||||
address_id = None
|
||||
|
||||
contact_outline['address_id'] = address_id
|
||||
|
||||
if return_outline:
|
||||
log.debug(f'Returning the Contact Outline: {contact_outline}')
|
||||
return contact_outline
|
||||
else:
|
||||
log.debug(f'Returning the Contact ID: {contact_id}')
|
||||
return contact_id
|
||||
# ### END ### API Contact Methods ### create_update_contact_obj_v4() ###
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### create_contact_obj() ###
|
||||
# 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.
|
||||
|
||||
Reference in New Issue
Block a user