Working on membership, person, and user
This commit is contained in:
@@ -7,53 +7,12 @@ from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, v
|
||||
from app.db_sql import 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, update_address_obj
|
||||
from app.methods.address_methods import create_address_obj, create_update_address_obj, update_address_obj
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.contact_models import Contact_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### create_contact_obj() ###
|
||||
def create_contact_obj(contact_obj_new:Contact_Base):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not contact_obj_new:
|
||||
return False
|
||||
|
||||
contact_obj_data = contact_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'})
|
||||
|
||||
if contact_obj_in_result := sql_insert(data=contact_obj_data, table_name='contact', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
return False
|
||||
|
||||
log.debug(contact_obj_in_result)
|
||||
|
||||
contact_id = contact_obj_in_result
|
||||
|
||||
if contact_obj_new.address:
|
||||
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)
|
||||
if isinstance(create_address_obj_result, int):
|
||||
address_id = create_address_obj_result
|
||||
# Need to update the contact with the new address_id
|
||||
contact_obj_up = {}
|
||||
contact_obj_up['id'] = contact_id
|
||||
contact_obj_up['address_id'] = address_id
|
||||
if contact_obj_up_result := sql_update(data=contact_obj_up, table_name='contact'): pass
|
||||
else:
|
||||
return False
|
||||
log.debug(contact_obj_up_result)
|
||||
else:
|
||||
log.debug(f'No address_id was returned when tyring to create_address_obj(): {create_address_obj_result}')
|
||||
address_id = None
|
||||
|
||||
log.debug(f'Returning the new contact_id: {contact_id}')
|
||||
return contact_id
|
||||
# ### END ### API Contact Methods ### create_contact_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### load_contact_obj() ###
|
||||
def load_contact_obj(
|
||||
contact_id:int|str,
|
||||
@@ -101,6 +60,103 @@ def load_contact_obj(
|
||||
# ### END ### API Contact Methods ### load_contact_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### get_contact_rec_list() ###
|
||||
def get_contact_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name='for_obj_type'): pass
|
||||
else: return False
|
||||
data = {}
|
||||
data[f'{for_obj_type}_id'] = for_obj_id
|
||||
# data['for_obj_type'] = for_obj_type
|
||||
sql_obj_type_id = f'`tbl`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
else:
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'contact_id', `tbl`.id_random AS 'contact_id_random'
|
||||
FROM `contact` AS `tbl`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if contact_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
contact_rec_li = contact_rec_li_result
|
||||
else:
|
||||
contact_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(contact_rec_li_result)
|
||||
|
||||
return contact_rec_li
|
||||
# ### END ### API Contact Methods ### get_contact_rec_list() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### create_contact_obj() ###
|
||||
def create_contact_obj(contact_obj_new:Contact_Base):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not contact_obj_new:
|
||||
return False
|
||||
|
||||
contact_obj_data = contact_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'})
|
||||
|
||||
if contact_obj_in_result := sql_insert(data=contact_obj_data, table_name='contact', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
return False
|
||||
|
||||
log.debug(contact_obj_in_result)
|
||||
|
||||
contact_id = contact_obj_in_result
|
||||
|
||||
if contact_obj_new.address:
|
||||
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)
|
||||
if isinstance(create_address_obj_result, int):
|
||||
address_id = create_address_obj_result
|
||||
# Need to update the contact with the new address_id
|
||||
contact_obj_up = {}
|
||||
contact_obj_up['id'] = contact_id
|
||||
contact_obj_up['address_id'] = address_id
|
||||
if contact_obj_up_result := sql_update(data=contact_obj_up, table_name='contact'): pass
|
||||
else:
|
||||
return False
|
||||
log.debug(contact_obj_up_result)
|
||||
else:
|
||||
log.debug(f'No address_id was returned when tyring to create_address_obj(): {create_address_obj_result}')
|
||||
address_id = None
|
||||
|
||||
log.debug(f'Returning the new contact_id: {contact_id}')
|
||||
return contact_id
|
||||
# ### END ### API Contact Methods ### create_contact_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### update_contact_obj() ###
|
||||
def update_contact_obj(
|
||||
contact_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
@@ -161,56 +217,76 @@ def update_contact_obj(
|
||||
# ### END ### API Contact Methods ### update_contact_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact Methods ### get_contact_rec_list() ###
|
||||
def get_contact_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# ### BEGIN ### API Contact Methods ### create_update_contact_obj() ###
|
||||
def create_update_contact_obj(
|
||||
contact_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
contact_obj: Contact_Base,
|
||||
process_address: bool = False,
|
||||
process_organization: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name='for_obj_type'): pass
|
||||
else: return False
|
||||
data = {}
|
||||
data[f'{for_obj_type}_id'] = for_obj_id
|
||||
# data['for_obj_type'] = for_obj_type
|
||||
sql_obj_type_id = f'`tbl`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
if contact_id:
|
||||
if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass
|
||||
else: return False
|
||||
contact_obj.id = contact_id
|
||||
else:
|
||||
sql_limit = ''
|
||||
# Insert record now and update later
|
||||
contact_dict_in = contact_obj.dict(by_alias=False, exclude_unset=True, exclude={'contact', 'organization', 'user'})
|
||||
log.debug(contact_dict_in)
|
||||
contact_in_result = sql_insert(
|
||||
data = contact_dict_in,
|
||||
table_name = 'contact',
|
||||
rm_id_random = True,
|
||||
id_random_length = default_num_bytes,
|
||||
)
|
||||
log.debug(contact_in_result)
|
||||
if isinstance(contact_in_result, bool) and contact_in_result is True:
|
||||
return contact_in_result
|
||||
elif isinstance(contact_in_result, int):
|
||||
contact_id = contact_in_result
|
||||
contact_obj.id = contact_id
|
||||
else:
|
||||
return False # This should not happen.
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'contact_id', `tbl`.id_random AS 'contact_id_random'
|
||||
FROM `contact` AS `tbl`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
# Process address data
|
||||
if process_address and contact_obj.address:
|
||||
address_obj = contact_obj.address
|
||||
address_obj.for_type = 'contact'
|
||||
address_obj.for_id = contact_id
|
||||
address_id = contact_obj.address_id_random
|
||||
address_result = create_update_address_obj(
|
||||
address_id = address_id,
|
||||
address_obj = address_obj,
|
||||
)
|
||||
log.debug(address_result)
|
||||
if isinstance(address_result, bool) and address_result is True:
|
||||
pass # Do not need to update contact object.
|
||||
elif isinstance(address_result, bool) and address_result is False:
|
||||
pass # Do not need to update contact object.
|
||||
elif isinstance(address_result, int):
|
||||
contact_obj.address_id = address_result
|
||||
else:
|
||||
log.warning('Something may have gone wrong while trying to create or update a address.')
|
||||
|
||||
if contact_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
contact_rec_li = contact_rec_li_result
|
||||
# Process contact data
|
||||
contact_dict_up = contact_obj.dict(by_alias=False, exclude_unset=True, exclude={'address'})
|
||||
log.debug(contact_dict_up)
|
||||
|
||||
# Update record
|
||||
contact_up_result = sql_update(
|
||||
data = contact_dict_up,
|
||||
table_name = 'contact',
|
||||
rm_id_random = True,
|
||||
)
|
||||
log.debug(contact_up_result)
|
||||
if isinstance(contact_up_result, bool) and contact_up_result is True:
|
||||
return contact_id
|
||||
elif isinstance(contact_up_result, bool) and contact_up_result is False:
|
||||
return False
|
||||
elif isinstance(contact_up_result, int):
|
||||
return contact_up_result
|
||||
else:
|
||||
contact_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(contact_rec_li_result)
|
||||
|
||||
return contact_rec_li
|
||||
# ### END ### API Contact Methods ### get_contact_rec_list() ###
|
||||
return False
|
||||
# ### END ### API Contact Methods ### create_update_contact_obj() ###
|
||||
|
||||
Reference in New Issue
Block a user