Code clean up and standardize
This commit is contained in:
@@ -95,6 +95,112 @@ def create_user_obj(
|
||||
# ### END ### API User Methods ### create_user_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### update_user_obj() ###
|
||||
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,
|
||||
) -> bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass
|
||||
else: return False
|
||||
|
||||
user_obj_up.id = user_id
|
||||
|
||||
log.debug(user_obj_up)
|
||||
# log.debug(user_obj_up.dict(by_alias=True, exclude_unset=True))
|
||||
log.debug(user_obj_up.dict(by_alias=False, exclude_unset=True))
|
||||
# log.debug(user_obj_up.dict(by_alias=False, exclude_unset=False))
|
||||
|
||||
# if user_obj_up.contact_id and user_obj_up.contact:
|
||||
# contact_id = user_obj_up.contact_id
|
||||
# contact_obj_up = user_obj_up.contact
|
||||
# log.debug(contact_id)
|
||||
# log.debug(contact_obj_up)
|
||||
# if contact_obj_up_result := update_contact_obj(
|
||||
# contact_id=contact_id,
|
||||
# contact_obj_up=contact_obj_up,
|
||||
# create_sub_obj=create_sub_obj,
|
||||
# ):
|
||||
# log.debug(contact_obj_up_result)
|
||||
# else:
|
||||
# log.debug(contact_obj_up_result)
|
||||
# return False
|
||||
# elif user_obj_up.contact and not user_obj_up.contact.id:
|
||||
# # NOTE: This will blindly create a new contact even if there was one associated but the user.contact_id was not found.
|
||||
# contact_obj_in = user_obj_up.contact
|
||||
# log.debug(contact_obj_in)
|
||||
# if contact_obj_in_result := create_contact_obj(contact_obj_new=contact_obj_in):
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(contact_obj_in_result)
|
||||
# user_obj_up.contact_id = contact_obj_in_result
|
||||
# else:
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(contact_obj_in_result)
|
||||
# return False
|
||||
|
||||
# if organization_obj_update := user_obj_up.organization:
|
||||
# log.debug(organization_obj_update)
|
||||
# if organization_obj_up_result := update_organization_obj(organization_obj_update):
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_up_result)
|
||||
# return True
|
||||
# else:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_up_result)
|
||||
# return False
|
||||
# else:
|
||||
# if organization_obj_in_result := insert_organization_obj(organization_obj_insert):
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_in_result)
|
||||
# return True # NOTE: This needs to return the new organization ID
|
||||
# else:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_in_result)
|
||||
# return False
|
||||
|
||||
# if user_obj_up.person_id and user_obj_up.person:
|
||||
# person_id = user_obj_up.person_id
|
||||
# person_obj_up = user_obj_up.person
|
||||
# log.debug(person_id)
|
||||
# log.debug(person_obj_up)
|
||||
# if person_obj_up_result := update_person_obj(
|
||||
# person_id=person_id,
|
||||
# person_obj_up=person_obj_up,
|
||||
# create_sub_obj=create_sub_obj,
|
||||
# ):
|
||||
# log.debug(person_obj_up_result)
|
||||
# else:
|
||||
# log.debug(person_obj_up_result)
|
||||
# return False
|
||||
# elif user_obj_up.person and not user_obj_up.person.id:
|
||||
# # NOTE: This will blindly create a new person even if there was one associated but the user.person_id was not found.
|
||||
# person_obj_in = user_obj_up.person
|
||||
# log.debug(person_obj_in)
|
||||
# if person_obj_in_result := create_person_obj_v3(person_obj_new=person_obj_in):
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(person_obj_in_result)
|
||||
# person_obj_up.person_id = person_obj_in_result
|
||||
# else:
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(person_obj_in_result)
|
||||
# return False
|
||||
|
||||
# IMPORTANT NOTE: Need to be extra careful if allowing an update to password, super, or manager. Maybe other fields?
|
||||
user_dict_up = user_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'password', 'super', 'manager', 'contact', 'organization', 'person'})
|
||||
log.debug(user_dict_up)
|
||||
|
||||
if user_obj_up_result := sql_update(data=user_dict_up, table_name='user', rm_id_random=True):
|
||||
log.debug(user_obj_up_result)
|
||||
return True
|
||||
else:
|
||||
log.debug(user_obj_up_result)
|
||||
return False
|
||||
# ### END ### API User Methods ### update_user_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### load_user_obj() ###
|
||||
def load_user_obj(
|
||||
user_id: int|str,
|
||||
@@ -283,112 +389,6 @@ def load_user_obj(
|
||||
# ### END ### API User Methods ### load_user_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### update_user_obj() ###
|
||||
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_missing_obj: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if user_id := redis_lookup_id_random(record_id_random=user_id, table_name='user'): pass
|
||||
else: return False
|
||||
|
||||
user_obj_up.id = user_id
|
||||
|
||||
log.debug(user_obj_up)
|
||||
# log.debug(user_obj_up.dict(by_alias=True, exclude_unset=True))
|
||||
log.debug(user_obj_up.dict(by_alias=False, exclude_unset=True))
|
||||
# log.debug(user_obj_up.dict(by_alias=False, exclude_unset=False))
|
||||
|
||||
# if user_obj_up.contact_id and user_obj_up.contact:
|
||||
# contact_id = user_obj_up.contact_id
|
||||
# contact_obj_up = user_obj_up.contact
|
||||
# log.debug(contact_id)
|
||||
# log.debug(contact_obj_up)
|
||||
# if contact_obj_up_result := update_contact_obj(
|
||||
# contact_id=contact_id,
|
||||
# contact_obj_up=contact_obj_up,
|
||||
# create_missing_obj=create_missing_obj,
|
||||
# ):
|
||||
# log.debug(contact_obj_up_result)
|
||||
# else:
|
||||
# log.debug(contact_obj_up_result)
|
||||
# return False
|
||||
# elif user_obj_up.contact and not user_obj_up.contact.id:
|
||||
# # NOTE: This will blindly create a new contact even if there was one associated but the user.contact_id was not found.
|
||||
# contact_obj_in = user_obj_up.contact
|
||||
# log.debug(contact_obj_in)
|
||||
# if contact_obj_in_result := create_contact_obj(contact_obj_new=contact_obj_in):
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(contact_obj_in_result)
|
||||
# user_obj_up.contact_id = contact_obj_in_result
|
||||
# else:
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(contact_obj_in_result)
|
||||
# return False
|
||||
|
||||
# if organization_obj_update := user_obj_up.organization:
|
||||
# log.debug(organization_obj_update)
|
||||
# if organization_obj_up_result := update_organization_obj(organization_obj_update):
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_up_result)
|
||||
# return True
|
||||
# else:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_up_result)
|
||||
# return False
|
||||
# else:
|
||||
# if organization_obj_in_result := insert_organization_obj(organization_obj_insert):
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_in_result)
|
||||
# return True # NOTE: This needs to return the new organization ID
|
||||
# else:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(organization_obj_in_result)
|
||||
# return False
|
||||
|
||||
# if user_obj_up.person_id and user_obj_up.person:
|
||||
# person_id = user_obj_up.person_id
|
||||
# person_obj_up = user_obj_up.person
|
||||
# log.debug(person_id)
|
||||
# log.debug(person_obj_up)
|
||||
# if person_obj_up_result := update_person_obj(
|
||||
# person_id=person_id,
|
||||
# person_obj_up=person_obj_up,
|
||||
# create_missing_obj=create_missing_obj,
|
||||
# ):
|
||||
# log.debug(person_obj_up_result)
|
||||
# else:
|
||||
# log.debug(person_obj_up_result)
|
||||
# return False
|
||||
# elif user_obj_up.person and not user_obj_up.person.id:
|
||||
# # NOTE: This will blindly create a new person even if there was one associated but the user.person_id was not found.
|
||||
# person_obj_in = user_obj_up.person
|
||||
# log.debug(person_obj_in)
|
||||
# if person_obj_in_result := create_person_obj_v3(person_obj_new=person_obj_in):
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(person_obj_in_result)
|
||||
# person_obj_up.person_id = person_obj_in_result
|
||||
# else:
|
||||
# # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(person_obj_in_result)
|
||||
# return False
|
||||
|
||||
# IMPORTANT NOTE: Need to be extra careful if allowing an update to password, super, or manager. Maybe other fields?
|
||||
user_dict_up = user_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'password', 'super', 'manager', 'contact', 'organization', 'person'})
|
||||
log.debug(user_dict_up)
|
||||
|
||||
if user_obj_up_result := sql_update(data=user_dict_up, table_name='user', rm_id_random=True):
|
||||
log.debug(user_obj_up_result)
|
||||
return True
|
||||
else:
|
||||
log.debug(user_obj_up_result)
|
||||
return False
|
||||
# ### END ### API User Methods ### update_user_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### get_user_rec_list() ###
|
||||
def get_user_rec_list(
|
||||
for_obj_type: str,
|
||||
|
||||
Reference in New Issue
Block a user