Working on event person, registration, badge, session, presentation, and presenter create and update. _v3 things
This commit is contained in:
@@ -101,12 +101,44 @@ def get_address_rec_list(
|
||||
# ### BEGIN ### API Address Methods ### create_address_obj() ###
|
||||
# NOTE: This will create an address.
|
||||
# Reviewed and updated 2021-08-10
|
||||
def create_address_obj(address_obj_new:Address_Base):
|
||||
def create_address_obj(
|
||||
address_obj_new: Address_Base,
|
||||
account_id: int|str = None,
|
||||
for_type: str = None,
|
||||
for_id: int|str = None,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
) -> int|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not address_obj_new:
|
||||
return False
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else: return False
|
||||
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(address_obj_new))
|
||||
if isinstance(address_obj_new, 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_obj_data = address_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
|
||||
|
||||
@@ -130,6 +162,7 @@ def update_address_obj(
|
||||
address_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
address_obj_up: Address_Base,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
) -> bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -115,20 +115,80 @@ def get_contact_rec_list(
|
||||
# ### END ### API Contact Methods ### get_contact_rec_list() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### get_account_id_w_contact_id() ###
|
||||
# Updated 2021-08-24
|
||||
def get_account_id_w_contact_id(
|
||||
contact_id: int|str,
|
||||
) -> bool|int|None:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||
else: return False
|
||||
|
||||
data = {}
|
||||
data['contact_id'] = contact_id
|
||||
|
||||
sql = f"""
|
||||
SELECT `contact`.id AS 'contact_id', `contact`.id_random AS 'contact_id_random', `contact`.account_id AS account_id
|
||||
FROM `contact` AS `contact`
|
||||
WHERE `contact`.id = :contact_id
|
||||
LIMIT 1;
|
||||
"""
|
||||
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
if contact_data_result := sql_select(data=data, sql=sql):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(contact_data_result)
|
||||
if account_id := contact_data_result.get('account_id', None): return account_id
|
||||
else: return False
|
||||
else: return None
|
||||
# ### END ### API Contact Methods ### get_account_id_w_contact_id() ###
|
||||
|
||||
|
||||
# ### 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.
|
||||
# Reviewed and updated 2021-08-24
|
||||
def create_contact_obj(
|
||||
contact_obj_new: Contact_Base,
|
||||
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
|
||||
) -> int|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not contact_obj_new:
|
||||
return False
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else: return False
|
||||
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(contact_obj_new))
|
||||
if isinstance(contact_obj_new, dict):
|
||||
if account_id:
|
||||
contact_obj_new['account_id'] = account_id
|
||||
if for_type:
|
||||
contact_obj_new['for_type'] = for_type
|
||||
if for_id:
|
||||
contact_obj_new['for_id'] = for_id
|
||||
try:
|
||||
contact_obj_new = Contact_Base(**contact_obj_new)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(contact_obj_new)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
else:
|
||||
if account_id:
|
||||
contact_obj_new.account_id = account_id
|
||||
if for_type:
|
||||
contact_obj_new.for_type = for_type
|
||||
if for_id:
|
||||
contact_obj_new.for_id = for_id
|
||||
|
||||
contact_obj_data = contact_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'})
|
||||
|
||||
@@ -144,7 +204,13 @@ def create_contact_obj(
|
||||
address_obj_new = contact_obj_new.address
|
||||
address_obj_new.for_type = 'contact'
|
||||
address_obj_new.for_id = contact_id
|
||||
create_address_obj_result = create_address_obj(address_obj_new=address_obj_new)
|
||||
create_address_obj_result = create_address_obj(
|
||||
address_obj_new = address_obj_new,
|
||||
account_id = account_id,
|
||||
for_type = 'contact',
|
||||
for_id = contact_id,
|
||||
fail_any = fail_any,
|
||||
)
|
||||
if isinstance(create_address_obj_result, int):
|
||||
address_id = create_address_obj_result
|
||||
# NOTE: This last update should no longer be needed now that the contact.address_id is not supposed to be used.
|
||||
@@ -173,13 +239,16 @@ def update_contact_obj(
|
||||
contact_obj_up: Contact_Base,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
return_dict: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||
else: return False
|
||||
|
||||
account_id = get_account_id_w_contact_id(contact_id)
|
||||
|
||||
contact_obj_up.id = contact_id
|
||||
|
||||
log.debug(contact_obj_up)
|
||||
@@ -189,34 +258,104 @@ def update_contact_obj(
|
||||
|
||||
#contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True)
|
||||
|
||||
if contact_obj_up.address_id and contact_obj_up.address:
|
||||
address_id = contact_obj_up.address_id
|
||||
address_obj_up = contact_obj_up.address
|
||||
log.debug(address_id)
|
||||
log.debug(address_obj_up)
|
||||
if address_obj_up_result := update_address_obj(
|
||||
address_id = address_id,
|
||||
address_obj_up = address_obj_up,
|
||||
create_sub_obj = create_sub_obj,
|
||||
):
|
||||
log.debug(address_obj_up_result)
|
||||
if contact_obj_up.address:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
address_obj_unknown = contact_obj_up.address
|
||||
log.debug(address_obj_unknown)
|
||||
if isinstance(contact_obj_up.address, dict):
|
||||
address_id = address_obj_unknown.get('address_id_random', None)
|
||||
else:
|
||||
log.debug(address_obj_up_result)
|
||||
return False
|
||||
elif contact_obj_up.address and not contact_obj_up.address.id:
|
||||
# NOTE: This will blindly create a new address even if there was one associated but the contact.address_id was not found.
|
||||
address_obj_in = contact_obj_up.address
|
||||
log.debug(address_obj_in)
|
||||
if address_obj_in_result := create_address_obj(address_obj_new=address_obj_in):
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(address_obj_in_result)
|
||||
# NOTE: This last update should no longer be needed now that the contact.address_id is not supposed to be used.
|
||||
# Need to update the contact with the new address_id
|
||||
contact_obj_up.address_id = address_obj_in_result # REMOVE
|
||||
address_id = address_obj_unknown.id
|
||||
|
||||
if address_id:
|
||||
# from app.methods.address_methods import update_address_obj_v3
|
||||
if update_address_obj_result := update_address_obj(
|
||||
address_id = address_id,
|
||||
address_obj_up = address_obj_unknown,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
):
|
||||
address_id = update_address_obj_result
|
||||
log.info(f'Address updated. Address ID: {address_id}')
|
||||
else:
|
||||
log.warning(f'Address not updated. Contact ID: {contact_id}')
|
||||
log.debug(update_address_obj_result)
|
||||
address_id = None
|
||||
if fail_any: return False
|
||||
|
||||
if isinstance(update_address_obj_result, int):
|
||||
address_id = update_address_obj_result
|
||||
log.info(f'Address updated. Address ID: {address_id}')
|
||||
else:
|
||||
log.warning(f'Address not updated. Contact ID: {contact_id}')
|
||||
log.debug(update_address_obj_result)
|
||||
address_id = None
|
||||
if fail_any: return False
|
||||
else:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(address_obj_in_result)
|
||||
return False
|
||||
log.info(f'No Address ID found.')
|
||||
# from app.methods.address_methods import create_address_obj_v3
|
||||
if create_address_obj_result := create_address_obj(
|
||||
account_id = account_id,
|
||||
for_type = 'contact',
|
||||
for_id = contact_id,
|
||||
address_obj_new = address_obj_unknown,
|
||||
fail_any = fail_any,
|
||||
):
|
||||
if isinstance(create_address_obj_result, int):
|
||||
address_id = create_address_obj_result
|
||||
log.info(f'Address created. Address ID: {address_id}')
|
||||
else:
|
||||
log.warning(f'Address not created. Contact ID: {contact_id}')
|
||||
log.debug(create_address_obj_result)
|
||||
address_id = None
|
||||
if fail_any: return False
|
||||
else:
|
||||
log.warning(f'Address not created. Contact ID: {contact_id}')
|
||||
log.debug(create_address_obj_result)
|
||||
address_id = None
|
||||
if fail_any: return False
|
||||
# return_dict['address_id'] = address_id
|
||||
else:
|
||||
log.info('Address not found or not in a dict.')
|
||||
pass
|
||||
|
||||
|
||||
|
||||
# if contact_obj_up.address_id and contact_obj_up.address:
|
||||
# address_id = contact_obj_up.address_id
|
||||
# address_obj_up = contact_obj_up.address
|
||||
# log.debug(address_id)
|
||||
# log.debug(address_obj_up)
|
||||
# if address_obj_up_result := update_address_obj(
|
||||
# address_id = address_id,
|
||||
# address_obj_up = address_obj_up,
|
||||
# create_sub_obj = create_sub_obj,
|
||||
# ):
|
||||
# log.debug(address_obj_up_result)
|
||||
# else:
|
||||
# log.debug(address_obj_up_result)
|
||||
# return False
|
||||
# elif contact_obj_up.address and not contact_obj_up.address.id:
|
||||
# # NOTE: This will blindly create a new address even if there was one associated but the contact.address_id was not found.
|
||||
# address_obj_in = contact_obj_up.address
|
||||
# log.debug(address_obj_in)
|
||||
# if address_obj_in_result := create_address_obj(
|
||||
# address_obj_new = address_obj_in,
|
||||
# account_id = account_id,
|
||||
# for_type = 'contact',
|
||||
# for_id = contact_id,
|
||||
# ):
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(address_obj_in_result)
|
||||
# # NOTE: This last update should no longer be needed now that the contact.address_id is not supposed to be used.
|
||||
# # Need to update the contact with the new address_id
|
||||
# contact_obj_up.address_id = address_obj_in_result # REMOVE
|
||||
# else:
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(address_obj_in_result)
|
||||
# return False
|
||||
|
||||
|
||||
|
||||
contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'address'})
|
||||
log.debug(contact_dict_up)
|
||||
|
||||
@@ -250,7 +250,7 @@ def get_event_id_w_event_person_id(
|
||||
# NOTE: This will create an event_person. This event_person should include at least a person_id.
|
||||
# NOTE: Is it a good idea to create and or update a person and or user here??? The create_event_person_obj() below does do that.
|
||||
# NOTE NOTE NOTE NOTE: I don't like the idea of creating or updating person and or user here. It just does not seem right... Security risk? Complexity?
|
||||
# Updated 2021-08-24
|
||||
# Updated 2021-08-25
|
||||
def create_event_person_obj(
|
||||
event_id: int|str,
|
||||
event_person_obj_new: Event_Person_Base,
|
||||
@@ -263,6 +263,16 @@ def create_event_person_obj(
|
||||
if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(event_person_obj_new))
|
||||
if isinstance(event_person_obj_new, dict):
|
||||
try:
|
||||
event_person_obj_new = Event_Person_Base(**event_person_obj_new)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(event_person_obj_new)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
event_person_obj_data = event_person_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'event_badge', 'event_registration', 'created_on', 'updated_on'})
|
||||
log.debug(event_person_obj_data)
|
||||
|
||||
@@ -411,7 +421,7 @@ def create_event_person_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Event Person Methods ### update_event_person_obj_v3() ###
|
||||
# Updated 2021-08-24
|
||||
# Updated 2021-08-25
|
||||
def update_event_person_obj_v3(
|
||||
event_person_id: int|str,
|
||||
event_person_obj_exist: Event_Person_Base,
|
||||
@@ -424,6 +434,16 @@ def update_event_person_obj_v3(
|
||||
if event_person_id := redis_lookup_id_random(record_id_random=event_person_id, table_name='event_person'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(event_person_obj_exist))
|
||||
if isinstance(event_person_obj_exist, dict):
|
||||
try:
|
||||
event_person_obj_exist = Event_Person_Base(**event_person_obj_exist)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(event_person_obj_exist)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
# Can't update the event_person_id alias if the .id was never set.
|
||||
# event_person_obj_exist.event_person_id = event_person_id
|
||||
if not event_person_obj_exist.id:
|
||||
|
||||
@@ -174,7 +174,7 @@ def get_event_id_w_event_session_id(
|
||||
|
||||
|
||||
# ### BEGIN ### API Event Presentation Methods ### create_event_presentation_obj() ###
|
||||
# Updated 2021-08-21
|
||||
# Updated 2021-08-25
|
||||
def create_event_presentation_obj(
|
||||
event_session_id: int|str,
|
||||
event_presentation_obj_new: Event_Presentation_Base,
|
||||
@@ -265,7 +265,7 @@ def create_event_presentation_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Event Presentation Methods ### update_event_presentation_obj_v3() ###
|
||||
# Updated 2021-08-21
|
||||
# Updated 2021-08-25
|
||||
def update_event_presentation_obj_v3(
|
||||
event_presentation_id: int|str,
|
||||
event_presentation_obj_exist: Event_Presentation_Base,
|
||||
|
||||
@@ -141,7 +141,7 @@ def get_event_session_id_w_event_presentation_id(
|
||||
|
||||
|
||||
# ### BEGIN ### API Event Presenter Methods ### create_event_presenter_obj() ###
|
||||
# Updated 2021-08-21
|
||||
# Updated 2021-08-25
|
||||
def create_event_presenter_obj(
|
||||
event_presentation_id: int|str,
|
||||
event_presenter_obj_new: Event_Presenter_Base,
|
||||
@@ -172,12 +172,6 @@ def create_event_presenter_obj(
|
||||
if event_session_id := redis_lookup_id_random(record_id_random=event_session_id, table_name='event_session'): pass
|
||||
else: return False
|
||||
|
||||
# if event_presentation_id := event_presenter_obj_new.event_presentation_id: pass
|
||||
# else:
|
||||
# log.error('Event Presentation ID is required')
|
||||
# return False
|
||||
|
||||
|
||||
log.debug(type(event_presenter_obj_new))
|
||||
if isinstance(event_presenter_obj_new, dict):
|
||||
try:
|
||||
|
||||
@@ -138,7 +138,7 @@ def get_event_id_w_event_person_id(
|
||||
# Event Registration can only update the primary event_person because they should already exist before registration is started. Chicken and egg problem...
|
||||
# Event Registration can create or update the event_person_list.
|
||||
# NOTE: Should there be a check before trying to update the primary event_person if they are also in the event_person_list??? Chicken and egg problem again?
|
||||
# Updated 2021-08-24
|
||||
# Updated 2021-08-25
|
||||
def create_event_registration_obj(
|
||||
event_id: int|str,
|
||||
event_person_id: int|str,
|
||||
@@ -231,7 +231,7 @@ def create_event_registration_obj(
|
||||
# Event Registration can only update the primary event_person because they should already exist since registration is started and registration "requires" the primary event_person first. Chicken and egg problem...
|
||||
# Event Registration can create or update the event_person_list.
|
||||
# NOTE: Should there be a check before trying to update the primary event_person if they are also in the event_person_list??? Chicken and egg problem again?
|
||||
# Updated 2021-08-24
|
||||
# Updated 2021-08-25
|
||||
def update_event_registration_obj_v3(
|
||||
event_registration_id: int|str,
|
||||
event_registration_obj_exist: Event_Registration_Base,
|
||||
|
||||
@@ -203,7 +203,7 @@ def load_event_session_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Event Session Methods ### create_event_session_obj() ###
|
||||
# Updated 2021-08-21
|
||||
# Updated 2021-08-25
|
||||
def create_event_session_obj(
|
||||
event_id: int|str,
|
||||
event_session_obj_new: Event_Session_Base,
|
||||
@@ -216,6 +216,16 @@ def create_event_session_obj(
|
||||
if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(event_session_obj_new))
|
||||
if isinstance(event_session_obj_new, dict):
|
||||
try:
|
||||
event_session_obj_new = Event_Session_Base(**event_session_obj_new)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(event_session_obj_new)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
event_session_obj_data = event_session_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'event_presentation', 'event_presentation_list', 'event_presenter', 'event_presenter_list', 'created_on', 'updated_on'})
|
||||
log.debug(event_session_obj_data)
|
||||
|
||||
@@ -265,7 +275,7 @@ def create_event_session_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Event Session Methods ### update_event_session_obj_v3() ###
|
||||
# Updated 2021-08-21
|
||||
# Updated 2021-08-25
|
||||
def update_event_session_obj_v3(
|
||||
event_session_id: int|str,
|
||||
event_session_obj_exist: Event_Session_Base,
|
||||
@@ -278,6 +288,16 @@ def update_event_session_obj_v3(
|
||||
if event_session_id := redis_lookup_id_random(record_id_random=event_session_id, table_name='event_session'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(event_session_obj_exist))
|
||||
if isinstance(event_session_obj_exist, dict):
|
||||
try:
|
||||
event_session_obj_exist = Event_Session_Base(**event_session_obj_exist)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(event_session_obj_exist)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
# Can't update the event_session_id alias if the .id was never set.
|
||||
# event_session_obj_exist.event_session_id = event_session_id
|
||||
if not event_session_obj_exist.id:
|
||||
|
||||
@@ -14,6 +14,7 @@ from app.methods.organization_methods import create_update_organization_obj, loa
|
||||
# from app.methods.user_methods import create_user_obj # , load_user_obj, update_user_obj
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.contact_models import Contact_Base
|
||||
from app.models.person_models import Person_Base
|
||||
|
||||
|
||||
@@ -372,6 +373,7 @@ def 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
|
||||
# Reviewed and updated 2021-08-24
|
||||
# Reviewed and updated 2021-08-21
|
||||
# Reviewed and updated 2021-08-10
|
||||
@@ -388,8 +390,19 @@ def create_person_obj_v3(
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(person_obj_new))
|
||||
if isinstance(person_obj_new, dict):
|
||||
try:
|
||||
person_obj_new = Person_Base(**person_obj_new)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(person_obj_new)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
person_obj_data = person_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'organization', 'user', 'created_on', 'updated_on'})
|
||||
log.debug(person_obj_data)
|
||||
person_obj_data['account_id'] = account_id
|
||||
|
||||
if person_obj_in_result := sql_insert(data=person_obj_data, table_name='person', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
@@ -414,7 +427,7 @@ def create_person_obj_v3(
|
||||
log.debug(contact_obj_unknown)
|
||||
if contact_id := contact_obj_unknown.get('contact_id_random', None):
|
||||
log.warning('Contact ID found. This is not expected, but should be ok.')
|
||||
from app.methods.contact_methods import update_contact_obj_v3
|
||||
# from app.methods.contact_methods import update_contact_obj_v3
|
||||
if update_contact_obj_result := update_contact_obj(
|
||||
contact_id = contact_id,
|
||||
contact_obj_up = contact_obj_unknown,
|
||||
@@ -446,6 +459,9 @@ def create_person_obj_v3(
|
||||
contact_obj_unknown.for_id = person_id
|
||||
|
||||
if create_contact_obj_result := create_contact_obj(
|
||||
account_id = account_id,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
contact_obj_new = contact_obj_unknown,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
@@ -547,8 +563,9 @@ def create_person_obj_v3(
|
||||
log.debug(user_obj_unknown)
|
||||
if user_id := user_obj_unknown.get('user_id_random', None):
|
||||
log.warning('User ID found. This is not expected, but should be ok.')
|
||||
from app.methods.user_methods import update_user_obj_v3
|
||||
if update_user_obj_result := update_user_obj_v3(
|
||||
# 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_unknown,
|
||||
# create_sub_obj = create_sub_obj,
|
||||
@@ -572,13 +589,10 @@ def create_person_obj_v3(
|
||||
if fail_any: return False
|
||||
else:
|
||||
log.info(f'No User ID found.')
|
||||
from app.methods.user_methods import create_user_obj_v3
|
||||
|
||||
user_obj_unknown = person_obj_new.user
|
||||
user_obj_unknown.for_type = 'person'
|
||||
user_obj_unknown.for_id = person_id
|
||||
|
||||
if create_user_obj_result := create_user_obj_v3(
|
||||
# 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_unknown,
|
||||
# create_sub_obj = create_sub_obj,
|
||||
# fail_any = fail_any,
|
||||
@@ -646,14 +660,16 @@ def create_person_obj_v3(
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### update_person_obj_v3() ###
|
||||
# Updated 2021-08-25
|
||||
# Updated 2021-08-24
|
||||
def update_person_obj_v3(
|
||||
person_id: int|str,
|
||||
person_obj_exist: Person_Base,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
return_dict: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # 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
|
||||
@@ -661,6 +677,18 @@ def update_person_obj_v3(
|
||||
|
||||
account_id = get_account_id_w_person_id(person_id=person_id)
|
||||
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(type(person_obj_exist))
|
||||
log.debug(person_obj_exist)
|
||||
if isinstance(person_obj_exist, dict):
|
||||
try:
|
||||
person_obj_exist = Person_Base(**person_obj_exist)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(person_obj_exist)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
# Can't update the person_id alias if the .id was never set.
|
||||
# person_obj_exist.person_id = person_id
|
||||
if not person_obj_exist.id:
|
||||
@@ -683,15 +711,28 @@ def update_person_obj_v3(
|
||||
return_dict['organization_id'] = None
|
||||
return_dict['user_id'] = None
|
||||
|
||||
if person_obj_exist.contact and isinstance(person_obj_exist.contact, dict):
|
||||
if person_obj_exist.contact:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
contact_obj_unknown = person_obj_exist.contact
|
||||
log.debug(contact_obj_unknown)
|
||||
if contact_id := contact_obj_unknown.get('contact_id_random', None):
|
||||
from app.methods.contact_methods import update_contact_obj_v3
|
||||
if update_contact_obj_result := update_contact_obj_v3(
|
||||
if isinstance(person_obj_exist.contact, dict):
|
||||
contact_id = contact_obj_unknown.get('contact_id_random', None)
|
||||
# try:
|
||||
# contact_obj_new = Contact_Base(**contact_obj_new)
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(contact_obj_new)
|
||||
# except ValidationError as e:
|
||||
# log.error(e.json())
|
||||
# return False
|
||||
else:
|
||||
contact_id = contact_obj_unknown.id
|
||||
|
||||
# if contact_id := contact_obj_unknown.get('contact_id_random', None):
|
||||
if contact_id:
|
||||
# from app.methods.contact_methods import update_contact_obj_v3
|
||||
if update_contact_obj_result := update_contact_obj(
|
||||
contact_id = contact_id,
|
||||
contact_obj_exist = contact_obj_unknown,
|
||||
contact_obj_up = contact_obj_unknown,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
):
|
||||
@@ -713,9 +754,11 @@ def update_person_obj_v3(
|
||||
if fail_any: return False
|
||||
else:
|
||||
log.info(f'No Contact ID found.')
|
||||
from app.methods.contact_methods import create_contact_obj
|
||||
# from app.methods.contact_methods import create_contact_obj_v3
|
||||
if create_contact_obj_result := create_contact_obj(
|
||||
person_id = person_id,
|
||||
account_id = account_id,
|
||||
for_type = 'person',
|
||||
for_id = person_id,
|
||||
contact_obj_new = contact_obj_unknown,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
@@ -799,9 +842,10 @@ def update_person_obj_v3(
|
||||
log.debug(user_obj_unknown)
|
||||
if user_id := user_obj_unknown.get('user_id_random', None):
|
||||
# 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_exist = user_obj_unknown,
|
||||
user_obj_up = user_obj_unknown,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
):
|
||||
@@ -824,6 +868,7 @@ def update_person_obj_v3(
|
||||
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_unknown,
|
||||
|
||||
@@ -21,6 +21,7 @@ from app.models.user_models import User_Base, User_New_Base, User_Out_Base
|
||||
# ### BEGIN ### API User Methods ### create_user_obj() ###
|
||||
# NOTE: This will create a new user and also hash a new password string if given.
|
||||
# NOTE: This uses the User_New_Base model, not User_Base or User_Out_Base
|
||||
# Updated 2021-08-25
|
||||
# Reviewed and updated 2021-08-21
|
||||
# Reviewed and updated 2021-08-10
|
||||
def create_user_obj(
|
||||
@@ -33,8 +34,23 @@ def create_user_obj(
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(user_obj_new))
|
||||
if isinstance(user_obj_new, dict):
|
||||
user_obj_new['account_id'] = account_id
|
||||
try:
|
||||
user_obj_new = User_New_Base(**user_obj_new)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(user_obj_new)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
user_obj_data = user_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'new_password', 'organization', 'person', 'created_on', 'updated_on'})
|
||||
log.debug(user_obj_data)
|
||||
user_obj_data['account_id'] = account_id
|
||||
|
||||
user_obj_data['password'] = user_obj_new.password # There has to be a better way to do this??? It thinks "password" is unset and so is excluded?
|
||||
log.debug(user_obj_data)
|
||||
@@ -101,10 +117,13 @@ def create_user_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### update_user_obj() ###
|
||||
# Updated 2021-08-25
|
||||
def update_user_obj(
|
||||
user_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
user_obj_up: User_Base,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = True, # Fail if any thing goes wrong for sub objects
|
||||
return_dict: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
@@ -112,6 +131,16 @@ def update_user_obj(
|
||||
if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass
|
||||
else: return False
|
||||
|
||||
log.debug(type(user_obj_up))
|
||||
if isinstance(user_obj_up, dict):
|
||||
try:
|
||||
user_obj_up = User_Base(**user_obj_up)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(user_obj_up)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
user_obj_up.id = user_id
|
||||
|
||||
log.debug(user_obj_up)
|
||||
|
||||
@@ -28,7 +28,7 @@ class User_New_Base(BaseModel):
|
||||
id: Optional[int] = Field(
|
||||
alias = 'user_id'
|
||||
)
|
||||
account_id_random: str
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int]
|
||||
account_name: Optional[str]
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_
|
||||
|
||||
# from app.methods.membership_person_methods import load_membership_person_obj
|
||||
from app.methods.order_methods import get_order_rec_list, load_order_obj
|
||||
from app.methods.person_methods import create_person_obj_v3, create_update_person_obj, get_person_rec_list, get_person_rec_w_external_id, load_person_obj, update_person_obj
|
||||
from app.methods.person_methods import create_person_obj_v3, create_update_person_obj, get_person_rec_list, get_person_rec_w_external_id, load_person_obj, update_person_obj, update_person_obj_v3
|
||||
|
||||
from app.models.person_models import Person_Base
|
||||
from app.models.response_models import Resp_Body_Base, mk_resp
|
||||
@@ -74,23 +74,35 @@ async def patch_person_obj(
|
||||
|
||||
# ### BEGIN ### API Person ### post_person_obj_new_v3() ###
|
||||
# Updated 2021-08-24
|
||||
@router.post('/new_v3', response_model=Resp_Body_Base)
|
||||
@router.post('/person/new_v3', response_model=Resp_Body_Base)
|
||||
async def post_person_obj_new_v3(
|
||||
person_obj: Person_Base,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = True, # Fail if any thing goes wrong for sub objects
|
||||
enabled: str = 'enabled', # For now this covers any included objects or object lists
|
||||
inc_address: bool = False, # Priority l1
|
||||
inc_contact: bool = False, # Priority l1
|
||||
inc_user: bool = False, # Priority l1
|
||||
x_account_id: str = Header(...),
|
||||
return_obj: bool = True,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
log.debug(person_obj.account_id)
|
||||
log.debug(person_obj.account_id_random)
|
||||
|
||||
# if person_obj.account_id: pass
|
||||
# else: return mk_resp(data=False, status_code=500, response=response, status_message='Testing')
|
||||
|
||||
# abort(501)
|
||||
|
||||
# There should probably be a check for the account ID before calling the create function?
|
||||
if create_person_obj_result := create_person_obj_v3(
|
||||
account_id = person_obj.account_id,
|
||||
account_id = x_account_id,
|
||||
person_obj_new = person_obj,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any
|
||||
@@ -100,7 +112,13 @@ async def post_person_obj_new_v3(
|
||||
if isinstance(create_person_obj_result, int):
|
||||
person_id = create_person_obj_result
|
||||
if return_obj:
|
||||
if load_person_obj_result := load_person_obj(person_id=person_id):
|
||||
if load_person_obj_result := load_person_obj(
|
||||
person_id=person_id,
|
||||
enabled = enabled,
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_user = inc_user,
|
||||
):
|
||||
data = load_person_obj_result
|
||||
else:
|
||||
data = False
|
||||
@@ -118,7 +136,7 @@ async def post_person_obj_new_v3(
|
||||
|
||||
# ### BEGIN ### API Person ### patch_person_obj_exist_v3() ###
|
||||
# Updated 2021-08-24
|
||||
@router.patch('/{person_id}/exist_v3', response_model=Resp_Body_Base)
|
||||
@router.patch('/person/{person_id}/exist_v3', response_model=Resp_Body_Base)
|
||||
async def patch_person_obj_exist_v3(
|
||||
person_obj: Person_Base,
|
||||
person_id: str = Query(..., min_length=11, max_length=22),
|
||||
|
||||
Reference in New Issue
Block a user