Work on person methods and related and event badge everything

This commit is contained in:
Scott Idem
2021-09-08 15:58:35 -04:00
parent d0d8392817
commit 54a43cc124
12 changed files with 1071 additions and 417 deletions

View File

@@ -0,0 +1,42 @@
from __future__ import annotations
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.lib_general import log, logging
from app.models.event_badge_template_models import Event_Badge_Template_Base
# ### BEGIN ### API Event Badge Template Methods ### load_event_badge_template_obj() ###
def load_event_badge_template_obj(
event_badge_template_id: int|str,
limit: int = 1000,
by_alias: bool = True,
exclude_unset: bool = True,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
) -> Event_Badge_Template_Base|dict|bool:
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if event_badge_template_id := redis_lookup_id_random(record_id_random=event_badge_template_id, table_name='event_badge_template'): pass
else: return False
if event_badge_template_rec := sql_select(table_name='v_event_badge_template', record_id=event_badge_template_id): pass
else: return False
try:
event_badge_template_obj = Event_Badge_Template_Base(**event_badge_template_rec)
log.debug(event_badge_template_obj)
except ValidationError as e:
log.error(e.json())
return False
if model_as_dict:
return event_badge_template_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
else:
return event_badge_template_obj
# ### END ### API Event Badge Template Methods ### load_event_badge_template_obj() ###

View File

@@ -11,7 +11,7 @@ from app.methods.address_methods import load_address_obj
from app.methods.contact_methods import load_contact_obj
from app.methods.event_cfg_methods import load_event_cfg_obj
from app.methods.event_session_methods import get_event_session_rec_list, load_event_session_obj
from app.methods.person_methods import create_person_obj_v3, load_person_obj, update_person_obj
from app.methods.person_methods import create_update_person_obj_v4b, load_person_obj
from app.methods.user_methods import create_user_obj, load_user_obj, update_user_obj
from app.models.event_models import Event_Base
@@ -623,10 +623,10 @@ def update_event_obj(
poc_person_obj_up = event_obj_up.poc_person
log.debug(poc_person_id)
log.debug(poc_person_obj_up)
if poc_person_obj_up_result := update_person_obj(
poc_person_id = poc_person_id,
poc_person_obj_up = poc_person_obj_up,
create_sub_obj = create_sub_obj,
if poc_person_obj_up_result := create_update_person_obj_v4b(
account_id = account_id,
person_dict_obj = poc_person_obj_up,
person_id = poc_person_id,
):
log.debug(poc_person_obj_up_result)
else:
@@ -636,7 +636,7 @@ def update_event_obj(
# NOTE: This will blindly create a new person even if there was one associated but the event.poc_person_id was not found.
poc_person_obj_in = event_obj_up.poc_person
log.debug(poc_person_obj_in)
if poc_person_obj_in_result := create_person_obj_v3(person_obj_new=poc_person_obj_in):
if poc_person_obj_in_result := create_update_person_obj_v4b(account_id=account_id, person_dict_obj=poc_person_obj_in):
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(poc_person_obj_in_result)
event_obj_up.poc_person_id = poc_person_obj_in_result

View File

@@ -304,6 +304,257 @@ def get_person_rec_list(
# ### END ### API Person Methods ### get_person_rec_list() ###
# ### BEGIN ### API Person Methods ### create_update_person_obj_v4b() ###
def create_update_person_obj_v4b(
account_id: int|str,
person_dict_obj: Person_Base,
person_id: int|str|None = None,
process_contact: bool = True, # For future v5
process_organization: bool = True, # For future v5
process_user: bool = True, # For future v5
create_sub_obj: bool = True, # For future v5
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
return_outline: bool = False, # For future v5
) -> bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('Checking requirements...')
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): log.info(f'Account ID: {account_id}')
else:
log.error('Missing or invalid Account ID passed. Failed requirement.')
log.error(f'Account ID: {account_id}')
return False
if person_id:
log.info(f'Person ID passed. Update existing Person below. Person ID: {person_id}')
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else:
log.error('Person ID passed but is invalid. Failed requirement.')
return False
# person_obj.id = person_id
else:
log.info('No Person ID passed. Create new Person below. Required: Account ID')
log.debug(type(person_dict_obj))
log.info('Create dictionary or Pydantic object variables...')
if isinstance(person_dict_obj, dict):
person_dict = person_dict_obj
person_dict['account_id'] = account_id
if person_id:
person_dict['person_id'] = person_id
try:
person_obj = Person_Base(**person_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
except ValidationError as e:
log.error(e.json())
return False
else:
person_obj = person_dict_obj
person_obj.account_id = account_id
if person_id:
# NOTE: Can't update the ID alias if it was never set.
person_obj.id = person_id
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'contact_id', 'contact_id_random', 'email', 'cc_email', 'membership_person_id', 'membership_person_id_random', 'organization', 'user', 'created_on', 'updated_on'})
log.info(f'SQL INSERT or UPDATE record... Person ID: {person_id}')
if person_id:
if person_dict_up_result := sql_update(data=person_dict, table_name='person', rm_id_random=True): pass
else:
log.warning(f'Person not updated. Person ID: {person_id}')
log.debug(person_dict_up_result)
return False
log.debug(person_dict_up_result)
else:
if person_dict_in_result := sql_insert(data=person_dict, table_name='person', rm_id_random=True, id_random_length=default_num_bytes): pass
else:
log.warning(f'Person not created.')
log.debug(person_dict_in_result)
return False
log.debug(person_dict_in_result)
person_id = person_dict_in_result
person_outline = {}
person_outline['person_id'] = person_id
person_outline['contact_id'] = None
person_outline['contact'] = {}
person_outline['contact']['address_id'] = None
person_outline['organization_id'] = None
person_outline['user_id'] = None
# Updated 2021-09-08
log.info(f'Check if processing Contact data...')
# NOTE: Use object model version because of better type checking and validations
if process_contact and person_obj.contact:
person_outline['contact_id'] = None
contact_obj = person_obj.contact
if contact_id := person_obj.contact_id : pass
elif contact_id := contact_obj.id: pass
else: contact_id = None
contact_obj.id
contact_obj.for_type = 'person'
contact_obj.for_id = person_id
create_update_contact_obj_result = create_update_contact_obj_v4(
account_id = account_id,
contact_dict_obj = contact_obj,
contact_id = contact_id,
for_type = 'person',
for_id = person_id,
# process_address = process_address,
# create_sub_obj = create_sub_obj,
fail_any = fail_any,
return_outline = return_outline,
)
if isinstance(create_update_contact_obj_result, int):
contact_id = create_update_contact_obj_result
elif create_update_contact_obj_result == True: pass
else:
log.warning(f'Create or Update failed while trying create_update_contact_obj_v4(): {create_update_contact_obj_result}')
contact_id = None
person_outline['contact_id'] = contact_id
# Updated 2021-09-08
log.info(f'Check if processing Organization data...')
if process_organization and person_obj.organization:
organization_obj = person_obj.organization
organization_id = person_obj.organization_id_random
update_person_obj = False
organization_result = create_update_organization_obj(
organization_id = organization_id,
organization_obj = organization_obj,
process_contact = True, # Setting to True under the assumption that if there is organization information then there is probably a contact (and address).
)
log.debug(organization_result)
if isinstance(organization_result, bool) and organization_result is True:
pass # Do not need to update person object.
elif isinstance(organization_result, bool) and organization_result is False:
pass # Do not need to update person object.
elif isinstance(organization_result, int):
person_obj.organization_id = organization_result
organization_id = organization_result
else:
log.warning('Something may have gone wrong while trying to create or update a organization.')
log.info(f'Check if need to update the Person with the Organization ID... Update Person Obj: {update_person_obj} Organization ID: {organization_id}')
if update_person_obj and isinstance(organization_id, int):
log.info(f'Updating the Person with the new/current Organization ID. Organization ID: {organization_id}')
person_data_up = {}
person_data_up['id'] = person_id
person_data_up['organization_id'] = organization_id
if person_data_up_result := sql_update(data=person_data_up, table_name='person'):
log.info(f'Person updated with Organization ID. Person ID: {person_id} Organization ID: {organization_id}')
else:
log.error(f'Person not updated with current Organization ID. Person ID: {person_id} Organization ID: {organization_id}')
if fail_any: return False
log.debug(person_dict_up_result)
person_outline['organization_id'] = organization_id
# Updated 2021-09-08
log.info(f'Check if processing User data... Process User: {process_user}')
if process_user and person_obj.user:
log.info(f'User data was found. Create a new User and link it to the new Person or update existing User. Person ID: {person_id}')
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
user_obj = person_obj.user
user_id = person_obj.user_id
update_person_obj = False
log.debug(user_obj)
if user_id:
log.warning('User ID found. This is not expected, but should be ok.')
# from app.methods.user_methods import update_user_obj_v3
from app.methods.user_methods import update_user_obj
if update_user_obj_result := update_user_obj(
user_id = user_id,
user_obj_up = user_obj,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
):
log.info(f'User updated. User ID: {user_id}')
log.debug(update_user_obj_result)
# NOTE: This should not be needed. Updating person just in case!
update_person_obj = True
else:
log.warning(f'User not updated. Person ID: {person_id}')
log.debug(update_user_obj_result)
user_id = None
if fail_any: return False
else:
log.info(f'No User ID found.')
# from app.methods.user_methods import create_user_obj_v3
from app.methods.user_methods import create_user_obj
if create_user_obj_result := create_user_obj(
account_id = account_id,
user_obj_new = user_obj,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
):
if isinstance(create_user_obj_result, int):
log.info(f'User created. User ID: {user_id}')
log.debug(update_user_obj_result)
user_id = create_user_obj_result
# Need to update the person with the new user_id
update_person_obj = True
else:
log.warning(f'User not created. Updated? Person ID: {person_id}')
log.debug(create_user_obj_result)
user_id = None
if fail_any: return False
else:
log.warning(f'User not created. Person ID: {person_id}')
log.debug(create_user_obj_result)
user_id = None
if fail_any: return False
log.info(f'Check if need to update the Person with the User ID... Update Person Obj: {update_person_obj} User ID: {user_id}')
if update_person_obj and isinstance(user_id, int):
log.info(f'Updating the Person with the new/current User ID. User ID: {user_id}')
person_data_up = {}
person_data_up['id'] = person_id
person_data_up['user_id'] = user_id
if person_data_up_result := sql_update(data=person_data_up, table_name='person'):
log.info(f'Person updated with User ID. Person ID: {person_id} User ID: {user_id}')
else:
log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}')
if fail_any: return False
log.debug(person_dict_up_result)
person_outline['user_id'] = user_id
else:
log.info('User data not found.')
pass
return person_id
# Process person data
person_dict_up = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
log.debug(person_dict_up)
# Update record
person_up_result = sql_update(
data = person_dict_up,
table_name = 'person',
rm_id_random = True,
)
log.debug(person_up_result)
if isinstance(person_up_result, bool) and person_up_result is True:
return person_id
elif isinstance(person_up_result, bool) and person_up_result is False:
return False
elif isinstance(person_up_result, int):
return person_up_result
else:
return False
# ### END ### API Person Methods ### create_update_person_obj_v4b() ###
# ### BEGIN ### API Person Methods ### get_person_rec_w_external_id() ###
# Updated 2021-08-19
def get_person_rec_w_external_id(
@@ -371,6 +622,8 @@ def get_account_id_w_person_id(
# ### END ### API Person Methods ### get_account_id_w_person_id() ###
# ### BEGIN ### API Person Methods ### create_person_obj_v3() ###
# NOTE: This will create a person and then also create a linked contact if person_obj.contact data is passed. The create_contact_obj will create a contact and then also create a linked address if person_obj.contact.address data is passed.
# Updated 2021-08-25
@@ -1006,361 +1259,104 @@ def update_person_obj(
# ### END ### API Person Methods ### update_person_obj() ###
# ### BEGIN ### API Person Methods ### create_update_person_obj_v4b() ###
def create_update_person_obj_v4b(
account_id: int|str,
person_dict_obj: Person_Base,
person_id: int|str|None = None,
process_contact: bool = True, # For future v5
process_organization: bool = True, # For future v5
process_user: bool = True, # For future v5
create_sub_obj: bool = True, # For future v5
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
return_outline: bool = False, # For future v5
) -> bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('Checking requirements...')
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): log.info(f'Account ID: {account_id}')
else:
log.error('Missing or invalid Account ID passed. Failed requirement.')
log.error(f'Account ID: {account_id}')
return False
# # ### BEGIN ### API Person Methods ### create_update_person_obj_v4b() ###
# def create_update_person_obj_v4b(
# person_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value.
# person_obj: Person_Base,
# process_contact: bool = False,
# process_organization: bool = False,
# ) -> bool:
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(locals())
if person_id:
log.info(f'Person ID passed. Update existing Person below. Person ID: {person_id}')
# if person_id:
# log.info(f'Person ID passed. Update existing Person. Person ID: {person_id}')
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else:
log.error('Person ID passed but is invalid. Failed requirement.')
return False
# person_obj.id = person_id
else:
log.info('No Person ID passed. Create new Person below. Required: Account ID')
# if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
# else: return False
# person_obj.id = person_id
# else:
# # Insert record now and update later
# log.info('No Person ID passed. Create new Person. Required: Account ID')
log.debug(type(person_dict_obj))
log.info('Create dictionary or Pydantic object variables...')
if isinstance(person_dict_obj, dict):
person_dict = person_dict_obj
person_dict['account_id'] = account_id
if person_id:
person_dict['person_id'] = person_id
try:
person_obj = Person_Base(**person_dict)
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
except ValidationError as e:
log.error(e.json())
return False
else:
person_obj = person_dict_obj
person_obj.account_id = account_id
if person_id:
# NOTE: Can't update the ID alias if it was never set.
person_obj.id = person_id
# person_dict_in = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
# log.debug(person_dict_in)
# person_in_result = sql_insert(
# data = person_dict_in,
# table_name = 'person',
# rm_id_random = True,
# id_random_length = default_num_bytes,
# )
# log.debug(person_in_result)
# if isinstance(person_in_result, bool) and person_in_result is True:
# return person_in_result
# elif isinstance(person_in_result, int):
# person_id = person_in_result
# person_obj.id = person_id
# else:
# return False # This should not happen.
person_dict = person_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'organization', 'user', 'created_on', 'updated_on'})
# # Process contact data
# if process_contact and person_obj.contact:
# contact_obj = person_obj.contact
# contact_obj.for_type = 'person'
# contact_obj.for_id = person_id
# contact_id = person_obj.contact_id_random
# contact_result = create_update_contact_obj(
# contact_id = contact_id,
# contact_obj = contact_obj,
# process_address = True, # Setting to True under the assumption that if there is contact information then there is probably an address.
# )
# log.debug(contact_result)
# if isinstance(contact_result, bool) and contact_result is True:
# pass # Do not need to update person object.
# elif isinstance(contact_result, bool) and contact_result is False:
# pass # Do not need to update person object.
# elif isinstance(contact_result, int):
# person_obj.contact_id = contact_result
# # pass # Do not need to update person object.
# else:
# log.warning('Something may have gone wrong while trying to create or update a contact.')
log.info(f'SQL INSERT or UPDATE record... Person ID: {person_id}')
if person_id:
if person_dict_up_result := sql_update(data=person_dict, table_name='person', rm_id_random=True): pass
else:
log.warning(f'Person not updated. Person ID: {person_id}')
log.debug(person_dict_up_result)
return False
log.debug(person_dict_up_result)
else:
if person_dict_in_result := sql_insert(data=person_dict, table_name='person', rm_id_random=True, id_random_length=default_num_bytes): pass
else:
log.warning(f'Person not created.')
log.debug(person_dict_in_result)
return False
log.debug(person_dict_in_result)
# # Process organization data
# if process_organization and person_obj.organization:
# organization_obj = person_obj.organization
# organization_id = person_obj.organization_id_random
# organization_result = create_update_organization_obj(
# organization_id = organization_id,
# organization_obj = organization_obj,
# process_contact = True, # Setting to True under the assumption that if there is organization information then there is probably a contact (and address).
# )
# log.debug(organization_result)
# if isinstance(organization_result, bool) and organization_result is True:
# pass # Do not need to update person object.
# elif isinstance(organization_result, bool) and organization_result is False:
# pass # Do not need to update person object.
# elif isinstance(organization_result, int):
# person_obj.organization_id = organization_result
# else:
# log.warning('Something may have gone wrong while trying to create or update a organization.')
person_id = person_dict_in_result
# # Process person data
# person_dict_up = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(person_obj)
# log.debug(person_dict_up)
person_outline = {}
person_outline['person_id'] = person_id
# Updated 2021-09-08
log.info(f'Check if processing Contact data...')
# NOTE: Use object model version because of better type checking and validations
if process_contact and person_obj.contact:
person_outline['contact_id'] = None
contact_obj = person_obj.contact
if contact_id := person_obj.contact_id : pass
elif contact_id := contact_obj.id: pass
else: contact_id = None
contact_obj.id
contact_obj.for_type = 'person'
contact_obj.for_id = person_id
create_update_contact_obj_result = create_update_contact_obj_v4(
account_id = account_id,
contact_dict_obj = contact_obj,
contact_id = contact_id,
for_type = 'person',
for_id = person_id,
# process_address = process_address,
# create_sub_obj = create_sub_obj,
fail_any = fail_any,
return_outline = return_outline,
)
if isinstance(create_update_contact_obj_result, int):
contact_id = create_update_contact_obj_result
elif create_update_contact_obj_result == True: pass
else:
log.warning(f'Create or Update failed while trying create_update_contact_obj_v4(): {create_update_contact_obj_result}')
contact_id = None
person_outline['contact_id'] = contact_id
# Updated 2021-09-08
log.info(f'Check if processing Organization data...')
if process_organization and person_obj.organization:
organization_obj = person_obj.organization
organization_id = person_obj.organization_id_random
organization_result = create_update_organization_obj(
organization_id = organization_id,
organization_obj = organization_obj,
process_contact = True, # Setting to True under the assumption that if there is organization information then there is probably a contact (and address).
)
log.debug(organization_result)
if isinstance(organization_result, bool) and organization_result is True:
pass # Do not need to update person object.
elif isinstance(organization_result, bool) and organization_result is False:
pass # Do not need to update person object.
elif isinstance(organization_result, int):
person_obj.organization_id = organization_result
organization_id = organization_result
else:
log.warning('Something may have gone wrong while trying to create or update a organization.')
log.info(f'Check if need to update the Person with the Organization ID... Update Person Obj: {update_person_obj} Organization ID: {organization_id}')
if update_person_obj and isinstance(organization_id, int):
log.info(f'Updating the Person with the new/current Organization ID. Organization ID: {organization_id}')
person_data_up = {}
person_data_up['id'] = person_id
person_data_up['organization_id'] = organization_id
if person_data_up_result := sql_update(data=person_data_up, table_name='person'):
log.info(f'Person updated with Organization ID. Person ID: {person_id} Organization ID: {organization_id}')
else:
log.error(f'Person not updated with current Organization ID. Person ID: {person_id} Organization ID: {organization_id}')
if fail_any: return False
log.debug(person_dict_up_result)
person_outline['organization_id'] = organization_id
# Updated 2021-09-08
log.info(f'Check if processing User data... Process User: {process_user}')
if process_user and person_obj.user:
log.info(f'User data was found. Create a new User and link it to the new Person or update existing User. Person ID: {person_id}')
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
user_obj = person_obj.user
user_id = person_obj.user_id
update_person_obj = False
log.debug(user_obj)
if user_id:
log.warning('User ID found. This is not expected, but should be ok.')
# from app.methods.user_methods import update_user_obj_v3
from app.methods.user_methods import update_user_obj
if update_user_obj_result := update_user_obj(
user_id = user_id,
user_obj_up = user_obj,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
):
log.info(f'User updated. User ID: {user_id}')
log.debug(update_user_obj_result)
# NOTE: This should not be needed. Updating person just in case!
update_person_obj = True
else:
log.warning(f'User not updated. Person ID: {person_id}')
log.debug(update_user_obj_result)
user_id = None
if fail_any: return False
# if isinstance(update_user_obj_result, int):
# user_id = update_user_obj_result
# log.info(f'User updated. User ID: {user_id}')
# # Need to update the person with the current user_id
# log.debug(person_dict_up_result)
# else:
# log.warning(f'User not updated. Person ID: {person_id}')
# log.debug(update_user_obj_result)
# user_id = None
# if fail_any: return False
else:
log.info(f'No User ID found.')
# from app.methods.user_methods import create_user_obj_v3
from app.methods.user_methods import create_user_obj
if create_user_obj_result := create_user_obj(
account_id = account_id,
user_obj_new = user_obj,
# create_sub_obj = create_sub_obj,
# fail_any = fail_any,
):
if isinstance(create_user_obj_result, int):
log.info(f'User created. User ID: {user_id}')
log.debug(update_user_obj_result)
user_id = create_user_obj_result
# Need to update the person with the new user_id
update_person_obj = True
else:
log.warning(f'User not created. Updated? Person ID: {person_id}')
log.debug(create_user_obj_result)
user_id = None
if fail_any: return False
else:
log.warning(f'User not created. Person ID: {person_id}')
log.debug(create_user_obj_result)
user_id = None
if fail_any: return False
log.info(f'Check if need to update the Person with the User ID... Update Person Obj: {update_person_obj} User ID: {user_id}')
if update_person_obj and isinstance(user_id, int):
log.info(f'Updating the Person with the new/current User ID. User ID: {user_id}')
person_data_up = {}
person_data_up['id'] = person_id
person_data_up['user_id'] = user_id
if person_data_up_result := sql_update(data=person_data_up, table_name='person'):
log.info(f'Person updated with User ID. Person ID: {person_id} User ID: {user_id}')
else:
log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}')
if fail_any: return False
log.debug(person_dict_up_result)
person_outline['user_id'] = user_id
else:
log.info('User data not found.')
pass
return person_id
# Process person data
person_dict_up = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
log.debug(person_dict_up)
# Update record
person_up_result = sql_update(
data = person_dict_up,
table_name = 'person',
rm_id_random = True,
)
log.debug(person_up_result)
if isinstance(person_up_result, bool) and person_up_result is True:
return person_id
elif isinstance(person_up_result, bool) and person_up_result is False:
return False
elif isinstance(person_up_result, int):
return person_up_result
else:
return False
# ### END ### API Person Methods ### create_update_person_obj_v4b() ###
# ### BEGIN ### API Person Methods ### create_update_person_obj() ###
def create_update_person_obj(
person_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value.
person_obj: Person_Base,
process_contact: bool = False,
process_organization: bool = False,
) -> bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if person_id:
log.info(f'Person ID passed. Update existing Person. Person ID: {person_id}')
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else: return False
person_obj.id = person_id
else:
# Insert record now and update later
log.info('No Person ID passed. Create new Person. Required: Account ID')
person_dict_in = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
log.debug(person_dict_in)
person_in_result = sql_insert(
data = person_dict_in,
table_name = 'person',
rm_id_random = True,
id_random_length = default_num_bytes,
)
log.debug(person_in_result)
if isinstance(person_in_result, bool) and person_in_result is True:
return person_in_result
elif isinstance(person_in_result, int):
person_id = person_in_result
person_obj.id = person_id
else:
return False # This should not happen.
# Process contact data
if process_contact and person_obj.contact:
contact_obj = person_obj.contact
contact_obj.for_type = 'person'
contact_obj.for_id = person_id
contact_id = person_obj.contact_id_random
contact_result = create_update_contact_obj(
contact_id = contact_id,
contact_obj = contact_obj,
process_address = True, # Setting to True under the assumption that if there is contact information then there is probably an address.
)
log.debug(contact_result)
if isinstance(contact_result, bool) and contact_result is True:
pass # Do not need to update person object.
elif isinstance(contact_result, bool) and contact_result is False:
pass # Do not need to update person object.
elif isinstance(contact_result, int):
person_obj.contact_id = contact_result
# pass # Do not need to update person object.
else:
log.warning('Something may have gone wrong while trying to create or update a contact.')
# Process organization data
if process_organization and person_obj.organization:
organization_obj = person_obj.organization
organization_id = person_obj.organization_id_random
organization_result = create_update_organization_obj(
organization_id = organization_id,
organization_obj = organization_obj,
process_contact = True, # Setting to True under the assumption that if there is organization information then there is probably a contact (and address).
)
log.debug(organization_result)
if isinstance(organization_result, bool) and organization_result is True:
pass # Do not need to update person object.
elif isinstance(organization_result, bool) and organization_result is False:
pass # Do not need to update person object.
elif isinstance(organization_result, int):
person_obj.organization_id = organization_result
else:
log.warning('Something may have gone wrong while trying to create or update a organization.')
# Process person data
person_dict_up = person_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact_id', 'contact_id_random', 'contact', 'organization', 'user'})
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(person_obj)
log.debug(person_dict_up)
# Update record
person_up_result = sql_update(
data = person_dict_up,
table_name = 'person',
rm_id_random = True,
)
log.debug(person_up_result)
if isinstance(person_up_result, bool) and person_up_result is True:
return person_id
elif isinstance(person_up_result, bool) and person_up_result is False:
return False
elif isinstance(person_up_result, int):
return person_up_result
else:
return False
# ### END ### API Person Methods ### create_update_person_obj() ###
# # Update record
# person_up_result = sql_update(
# data = person_dict_up,
# table_name = 'person',
# rm_id_random = True,
# )
# log.debug(person_up_result)
# if isinstance(person_up_result, bool) and person_up_result is True:
# return person_id
# elif isinstance(person_up_result, bool) and person_up_result is False:
# return False
# elif isinstance(person_up_result, int):
# return person_up_result
# else:
# return False
# # ### END ### API Person Methods ### create_update_person_obj_v4b() ###