Working on user module
This commit is contained in:
@@ -11,7 +11,8 @@ from app.db_sql import *
|
||||
|
||||
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
||||
|
||||
from ..models.user_model import User_Base
|
||||
from ..models.user_model import User_Base, User_New_Base, User_Out_Base
|
||||
from ..models.user_methods import load_user_obj
|
||||
from ..models.response_model import *
|
||||
|
||||
|
||||
@@ -41,6 +42,41 @@ async def post_user_obj(
|
||||
return result
|
||||
|
||||
|
||||
@router.post('/new', response_model=Resp_Body_Base)
|
||||
async def post_user_new_obj(
|
||||
user_obj: User_New_Base,
|
||||
x_account_id: str = Header(...),
|
||||
return_obj: bool = True,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
user_data = user_obj.dict(by_alias=False, exclude_unset=False, exclude={'new_password', 'account_id_random'})
|
||||
|
||||
log.info('Checking if the username is already in use for the account...')
|
||||
sql_select_user = f"""
|
||||
SELECT *
|
||||
FROM `user` AS user
|
||||
WHERE user.account_id = :account_id and user.username = :username
|
||||
"""
|
||||
|
||||
if sql_select_result := sql_select(sql=sql_select_user, data=user_data):
|
||||
return mk_resp(data=False, status_message='The user account was not created. This is likely because of a duplicate username.')
|
||||
|
||||
log.info('Adding new user account...')
|
||||
if sql_insert_result := sql_insert(table_name='user', data=user_data):
|
||||
log.info('Selecting new user account to return as an object...')
|
||||
sql_select_user_result = sql_select(table_name='v_user', record_id=sql_insert_result)
|
||||
|
||||
user_obj_new = User_Out_Base(**sql_select_user_result)
|
||||
|
||||
return mk_resp(data=user_obj_new.dict(by_alias=True, exclude_unset=True))
|
||||
else:
|
||||
return mk_resp(data=False, status_message='The user account was not created. Something seems to have gone wrong on insert.')
|
||||
|
||||
|
||||
@router.patch('/{obj_id}', response_model=Resp_Body_Base)
|
||||
async def patch_user_obj(
|
||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||
@@ -158,6 +194,139 @@ async def get_user_obj_li(
|
||||
return result
|
||||
|
||||
|
||||
# Look up is only for account or person records
|
||||
@router.get('/lookup', response_model=Resp_Body_Base)
|
||||
async def lookup_user_obj(
|
||||
for_obj_id: Union[int,str],
|
||||
for_obj_type: str = Query(..., min_length=2, max_length=50),
|
||||
x_account_id: str = Header(...),
|
||||
inc_contact: bool = False,
|
||||
inc_organization: bool = False,
|
||||
inc_person: bool = False,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
obj_type = 'user'
|
||||
base_name = User_Out_Base
|
||||
|
||||
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||
else: return mk_resp(data=False, status_code=404) # Not Found
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
|
||||
data = {}
|
||||
as_list = False
|
||||
if for_obj_type == 'account' and for_obj_id:
|
||||
data['account_id'] = for_obj_id
|
||||
sql_where_for_obj_type = """`user`.account_id = :account_id"""
|
||||
sql_limit = ''
|
||||
as_list = True
|
||||
elif for_obj_type == 'person' and for_obj_id:
|
||||
data['person_id'] = for_obj_id
|
||||
sql_where_for_obj_type = """`user`.person_id = :person_id"""
|
||||
sql_limit = 'LIMIT 1'
|
||||
else:
|
||||
log.debug(f'Object type={for_obj_type}; Object ID={for_obj_id}')
|
||||
return mk_resp(data=False, status_code=400) # Bad Request
|
||||
|
||||
sql = f"""
|
||||
SELECT id AS 'user_id', id_random AS 'user_id_random'
|
||||
FROM `user` AS `user`
|
||||
WHERE {sql_where_for_obj_type}
|
||||
{sql_limit}
|
||||
"""
|
||||
|
||||
# This will return a list if selecting by account ID
|
||||
user_obj_result = sql_select(data=data, sql=sql, as_list=as_list)
|
||||
if isinstance(user_obj_result, dict):
|
||||
user_id = user_obj_result.get('user_id', None)
|
||||
user_obj = load_user_obj(
|
||||
user_id=user_id,
|
||||
inc_contact=inc_contact,
|
||||
inc_organization=inc_organization,
|
||||
inc_person=inc_person
|
||||
).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
data = user_obj
|
||||
elif isinstance(user_obj_result, list):
|
||||
user_obj_li = []
|
||||
for user_obj in user_obj_result:
|
||||
user_id = user_obj.get('user_id', None)
|
||||
user_obj_li.append(
|
||||
load_user_obj(
|
||||
user_id=user_id,
|
||||
inc_contact=inc_contact,
|
||||
inc_organization=inc_organization,
|
||||
inc_person=inc_person,
|
||||
).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
)
|
||||
data = user_obj_li
|
||||
else:
|
||||
log.debug(user_obj_result)
|
||||
return mk_resp(data=None, status_code=404) # Not Found
|
||||
return mk_resp(data=data)
|
||||
|
||||
|
||||
# Look up is only for account or person records
|
||||
@router.get('/lookup_username', response_model=Resp_Body_Base)
|
||||
async def lookup_username_obj(
|
||||
account_id: Union[int,str],
|
||||
username: str = Query(..., min_length=2, max_length=50),
|
||||
x_account_id: str = Header(...),
|
||||
inc_contact: bool = False,
|
||||
inc_organization: bool = False,
|
||||
inc_person: bool = False,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
):
|
||||
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 mk_resp(data=False, status_code=404) # Not Found
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
|
||||
data = {}
|
||||
data['account_id'] = account_id
|
||||
data['username'] = username
|
||||
|
||||
sql = f"""
|
||||
SELECT id AS 'user_id', id_random AS 'user_id_random'
|
||||
FROM `user` AS `user`
|
||||
WHERE `user`.account_id = :account_id AND `user`.username = :username
|
||||
"""
|
||||
|
||||
# This will return a list if selecting by account ID
|
||||
user_obj_result = sql_select(data=data, sql=sql)
|
||||
if isinstance(user_obj_result, dict):
|
||||
user_id = user_obj_result.get('user_id', None)
|
||||
user_obj = load_user_obj(
|
||||
user_id=user_id,
|
||||
inc_contact=inc_contact,
|
||||
inc_organization=inc_organization,
|
||||
inc_person=inc_person
|
||||
).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
data = user_obj
|
||||
elif isinstance(user_obj_result, list):
|
||||
user_obj_li = []
|
||||
for user_obj in user_obj_result:
|
||||
user_id = user_obj.get('user_id', None)
|
||||
user_obj_li.append(
|
||||
load_user_obj(
|
||||
user_id=user_id,
|
||||
inc_contact=inc_contact,
|
||||
inc_organization=inc_organization,
|
||||
inc_person=inc_person,
|
||||
).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
)
|
||||
data = user_obj_li
|
||||
else:
|
||||
log.debug(user_obj_result)
|
||||
return mk_resp(data=None, status_code=404) # Not Found
|
||||
return mk_resp(data=data)
|
||||
|
||||
|
||||
@router.get('/{obj_id}', response_model=Resp_Body_Base)
|
||||
async def get_user_obj(
|
||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||
|
||||
Reference in New Issue
Block a user