Work on event, event_session, event_presentation, event_presenter, user, and person routes, methods, and models

This commit is contained in:
Scott Idem
2021-08-21 16:04:17 -04:00
parent 67b8435e08
commit 824bdd29a2
19 changed files with 631 additions and 80 deletions

View File

@@ -11,7 +11,7 @@ from app.lib_general import log, logging
from app.methods.contact_methods import create_contact_obj, create_update_contact_obj, load_contact_obj, update_contact_obj
from app.methods.order_methods import load_order_obj, get_order_rec_list
from app.methods.organization_methods import create_update_organization_obj, load_organization_obj, update_organization_obj
# from app.methods.user_methods import create_user_obj, load_user_obj, update_user_obj
# 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.person_models import Person_Base
@@ -341,45 +341,74 @@ def get_person_rec_w_external_id(
# ### BEGIN ### API Person Methods ### create_person_obj() ###
# 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.
# Reviewed and updated 2021-08-21
# Reviewed and updated 2021-08-10
def create_person_obj(person_obj_new:Person_Base):
def create_person_obj(
person_obj_new: Person_Base,
create_sub_obj: bool = False,
fail_any: bool = True, # Fail if any thing goes wrong for sub objects
return_dict: bool = False,
) -> bool|dict|int:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if not person_obj_new:
return False
person_obj_data = person_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'organization', 'created_on', 'updated_on'})
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)
if person_obj_in_result := sql_insert(data=person_obj_data, table_name='person', rm_id_random=True, id_random_length=8): pass
else:
log.warning(f'Person not created.')
log.debug(person_obj_in_result)
return False
#log.setLevel(logging.DEBUG)
log.debug(person_obj_in_result)
person_id = person_obj_in_result
return_dict = {}
return_dict['person_id'] = None
return_dict['contact_id'] = None
return_dict['contact'] = {}
return_dict['contact']['address_id'] = None
return_dict['user_id'] = None
if person_obj_new.contact:
log.info(f'Contact data was found. Create a new contact and link it to the new person. Person ID: {person_id}')
contact_obj_new = person_obj_new.contact
contact_obj_new.for_type = 'person'
contact_obj_new.for_id = person_id
create_contact_obj_result = create_contact_obj(contact_obj_new=contact_obj_new)
if isinstance(create_contact_obj_result, int):
contact_id = create_contact_obj_result
log.debug(f'Update person with new contact_id: {contact_id}')
# NOTE: This last update should no longer be needed now that the person.contact_id is not supposed to be used.
# Need to update the person with the new contact_id
person_obj_up = {} # REMOVE
person_obj_up['id'] = person_id # REMOVE
person_obj_up['contact_id_old'] = contact_id # REMOVE
if person_obj_up_result := sql_update(data=person_obj_up, table_name='person'): pass # REMOVE
else: # REMOVE
return False # REMOVE
log.debug(person_obj_up_result) # REMOVE
log.info(f'Contact created. Contact ID: {contact_id}')
else:
log.debug(f'No contact_id was returned when tyring to create_contact_obj(): {create_contact_obj_result}')
log.warning(f'Contact not created. Person ID: {person_id}')
log.debug(create_contact_obj_result)
contact_id = None
if fail_any: return False
if person_obj_new.user:
log.info(f'User data was found. Create a new user and link it to the new person. Person ID: {person_id}')
from app.methods.user_methods import create_user_obj
user_obj_new = person_obj_new.user
user_obj_new.person_id = person_id
create_user_obj_result = create_user_obj(user_obj_new=user_obj_new)
if isinstance(create_user_obj_result, int):
log.info(f'User created. User ID: {user_id} Update person {person_id} with new user ID.')
user_id = create_user_obj_result
# Need to update the person with the new user_id
person_obj_up = {}
person_obj_up['id'] = person_id
person_obj_up['user_id'] = user_id
if person_obj_up_result := sql_update(data=person_obj_up, table_name='person'):
log.info(f'Person updated with user ID. Person ID: {person_id} User ID: {user_id}')
else:
log.warning(f'Person not updated with user ID. Person ID: {person_id} User ID: {user_id}')
if fail_any: return False
log.debug(person_obj_up_result)
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.debug(f'Returning the new person_id: {person_id}')
return person_id
@@ -393,6 +422,7 @@ def update_person_obj(
person_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
person_obj_up: Person_Base,
create_missing_obj: bool = False,
fail_any: bool = True, # Fail if any thing goes wrong for sub objects
) -> bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())