A lot of route common params clean up

This commit is contained in:
Scott Idem
2022-01-05 15:22:49 -05:00
parent 9a51e75892
commit a70f931688
5 changed files with 166 additions and 248 deletions

View File

@@ -28,42 +28,86 @@ async def get_token_header(x_token: str = Header(...)):
# ### BEGIN ### API Lib General ### async get_account_header() ###
# Updated 2022-01-05
async def get_account_header(x_account_id: str = Header(..., min_length=11, max_length=22)) -> dict:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# async def get_account_header(x_account_id: str = Header(..., min_length=11, max_length=22)) -> dict:
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(locals())
# log.info(f'The x-account-id header has a value. x-account-id: {x_account_id}')
# if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
# log.setLevel(logging.DEBUG)
# log.info(f'Found the x-account-id with the value: {x_account_id}')
# x_account = { 'id': account_id, 'id_random': x_account_id }
# log.debug(x_account)
# return x_account
# else:
# log.warning(f'The x-account-id Account ID was not found. Account ID: {x_account_id}')
# raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
# ### END ### API Lib General ### async get_account_header() ###
# ### BEGIN ### API Lib General ### class Common_Route_Params ###
# Updated 2022-01-05
class Common_Route_Params:
def __init__(
self,
x_account_id: int,
x_account_id_random: str,
limit: int = 10,
enabled: str = 'enabled',
by_alias: bool = True,
exclude_unset: bool = True,
response = None,
):
self.x_account_id = x_account_id
self.x_account_id_random = x_account_id_random
self.limit = limit
self.enabled = enabled
self.by_alias = by_alias
self.exclude_unset = exclude_unset
self.response = response
# log.debug(response)
# ### END ### API Lib General ### class Common_Route_Params ###
# ### BEGIN ### API Lib General ### async route_commons() ###
# Updated 2022-01-05
async def common_route_params(
x_account_id: str = Header(..., min_length=11, max_length=22),
enabled: str = 'enabled', # all, enabled, disabled
limit: int = 100,
by_alias: bool = True,
exclude: Optional[list] = [],
exclude_none: Optional[bool] = True,
exclude_unset: bool = True,
include: Optional[list] = [],
response: Response = Response,
) -> Common_Route_Params:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info(f'Setting commons values')
log.info(f'The x-account-id header has a value. x-account-id: {x_account_id}')
if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
log.setLevel(logging.DEBUG)
x_account_id_random = x_account_id
if x_account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
log.info(f'Found the x-account-id with the value: {x_account_id}')
account = { 'id': account_id, 'id_random': x_account_id }
log.debug(account)
return account
# x_account = { 'id': account_id, 'id_random': x_account_id }
# log.debug(x_account)
# return x_account
else:
log.warning(f'The x-account-id Account ID was not found. Account ID: {x_account_id}')
raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
# if len(x_account_id) >= 11 and len(x_account_id) <= 22:
# log.info(f'The x-account-id header has a value. x-account-id: {x_account_id}')
# if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id):
# log.setLevel(logging.DEBUG)
# log.info(f'Found the x-account-id with the value: {x_account_id}')
# account = { 'id': account_id, 'id_random': x_account_id }
# x_account_id = account_id
# else:
# log.warning(f'The x-account-id Account ID was not found. Account ID: {x_account_id}')
# raise HTTPException(status_code=403, detail='The x-account-id Account ID was not found.') # Forbidden
# elif x_account_id == '':
# log.info('The x-account-id header was empty.')
# raise HTTPException(status_code=403, detail='The x-account-id header was empty.') # Forbidden
# # account = { 'id': None, 'id_random': None }
# else:
# log.info('The x-account-id header was not valid.')
# raise HTTPException(status_code=403, detail='The x-account-id header was not valid.') # Forbidden
commons = Common_Route_Params( x_account_id=x_account_id, x_account_id_random=x_account_id_random, limit=limit, enabled=enabled, by_alias=by_alias, exclude_unset=exclude_unset, response=response )
# commons = { 'limit': limit, 'enabled': enabled, 'by_alias': by_alias, 'exclude_unset': exclude_unset }
# ### END ### API Lib General ### async get_account_header() ###
log.debug(commons)
return commons
# ### END ### API Lib General ### async route_commons() ###
def secure_hash_string(string:str):

View File

@@ -14,7 +14,7 @@ from sqlalchemy import create_engine, text
from sqlalchemy.exc import IntegrityError, OperationalError
from . import config
from app.lib_general import get_account_header
# from app.lib_general import common_route_params, Common_Route_Params
from app.log import log, logging
# Import the routers here first:
@@ -82,7 +82,6 @@ app.include_router(
account.router,
prefix='/account',
tags=['Account'],
dependencies=[Depends(get_account_header)],
)
app.include_router(
activity_log.router,
@@ -93,7 +92,6 @@ app.include_router(
address.router,
prefix='/address',
tags=['Address'],
dependencies=[Depends(get_account_header)],
)
app.include_router(
archive.router,
@@ -109,7 +107,6 @@ app.include_router(
contact.router,
prefix='/contact',
tags=['Contact'],
dependencies=[Depends(get_account_header)],
)
app.include_router(
cont_edu_cert.router,
@@ -233,7 +230,6 @@ app.include_router(
app.include_router(
membership_person.router,
tags=['Membership Person'],
dependencies=[Depends(get_account_header)],
)
app.include_router(
membership_type.router,
@@ -270,15 +266,12 @@ app.include_router(
)
app.include_router(
person.router,
# prefix='/person',
tags=['Person'],
# dependencies=[Depends(get_account_header)],
)
app.include_router(
person_user.router,
prefix='/person_user',
tags=['Person User'],
dependencies=[Depends(get_account_header)],
)
app.include_router(
post.router,
@@ -307,7 +300,6 @@ app.include_router(
)
app.include_router(
user.router,
# prefix='/user',
tags=['User'],
)
app.include_router(

View File

@@ -351,7 +351,7 @@ def load_person_obj(
def get_person_rec_list(
for_obj_type: str,
for_obj_id: str,
email: str,
email: str = None,
limit: int = 1000,
enabled: str = 'enabled', # enabled, disabled, all
) -> list|bool:
@@ -1534,8 +1534,9 @@ def email_person_create_url(
account_short_name = account_cfg.account_short_name
person_create_url = f'{root_url}person/{person_id_random}/create'
# person_create_url = f'{root_url}person/{person_id_random}/create'
# person_create_auth_key_url = f'{root_url}?user_id={user_id_random}&auth_key={new_auth_key}'
person_create_auth_key_url = f'{root_url}?person_id={user_id_random}&auth_key={new_auth_key}'
subject = f'{account_short_name}: One Time Use Create Account Link'
# subject = f'{account_short_name}: One Time Use Create Account Link ({new_auth_key})'

View File

@@ -3,7 +3,7 @@ from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Resp
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from app.lib_general import log, logging, get_account_header
from app.lib_general import log, logging, common_route_params, Common_Route_Params
from app.config import settings
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, get_id_random, redis_lookup_id_random
@@ -22,11 +22,8 @@ router = APIRouter()
@router.post('/person', response_model=Resp_Body_Base)
async def post_person_obj(
obj: Person_Base,
x_account_id: str = Header(...),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -46,12 +43,9 @@ async def post_person_obj(
@router.patch('/person/{obj_id}', response_model=Resp_Body_Base)
async def patch_person_obj(
obj: Person_Base,
obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: Optional[str] = Header(..., ),
obj_id: str = Query(..., min_length=11, max_length=22),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -83,41 +77,33 @@ async def v3_post_person_obj_new(
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,
inc_contact: bool = False,
inc_organization: bool = False,
inc_user: bool = False,
return_obj: bool = True,
limit: int = 50,
by_alias: bool = True,
# include: Optional[list] = [],
# exclude: Optional[list] = [],
exclude_unset: Optional[bool] = True,
# exclude_none: Optional[bool] = True,
x_account_id: str = Header(...),
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if create_update_person_obj_result := create_update_person_obj_v4b(
account_id = x_account_id,
account_id = x_account['id'],
person_dict_obj = person_obj,
person_id = None,
# process_contact = process_contact,
# process_organization = process_organization,
# process_user = process_user,
): pass
else: return mk_resp(data=False, status_code=400, response=response, status_message='The person was not created. Check the field names and data types.')
else: return mk_resp(data=False, status_code=400, response=commons.response, status_message='The person was not created. Check the field names and data types.')
if isinstance(create_update_person_obj_result, int):
person_id = create_update_person_obj_result
if return_obj:
if load_person_obj_result := load_person_obj(
person_id = person_id,
enabled = enabled,
enabled = commons.enabled,
inc_address = inc_address,
inc_contact = inc_contact,
inc_organization = inc_organization,
@@ -126,16 +112,16 @@ async def v3_post_person_obj_new(
data = load_person_obj_result
else:
data = False
return mk_resp(data=data, response=response, status_message='The person was probably created, but there was a problem returning the data.')
return mk_resp(data=data, response=commons.response, status_message='The person was probably created, but there was a problem returning the data.')
else:
person_id = create_update_person_obj_result
person_id_random = get_id_random(record_id=person_id, table_name='person')
data = {}
data['person_id'] = person_id
data['person_id_random'] = person_id_random
return mk_resp(data=data, response=response, status_message='The person was created.')
return mk_resp(data=data, response=commons.response, status_message='The person was created.')
else:
return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to create a person was unexpected.')
return mk_resp(data=False, status_code=400, response=commons.response, status_message='The result from trying to create a person was unexpected.')
# ### BEGIN ### API Person ### v3_post_person_obj_new() ###
@@ -152,37 +138,29 @@ async def v3_patch_person_obj_exist(
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,
inc_contact: bool = False,
inc_organization: bool = False,
inc_user: bool = False,
return_obj: bool = True,
limit: int = 50,
by_alias: bool = True,
# include: Optional[list] = [],
# exclude: Optional[list] = [],
exclude_unset: Optional[bool] = True,
# exclude_none: Optional[bool] = True,
x_account_id: str = Header(...),
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
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
else: return mk_resp(data=None, status_code=404, response=response, status_message='The person was not updated. The person ID was invalid or not found.')
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person was not updated. The person ID was invalid or not found.')
if create_update_person_obj_result := create_update_person_obj_v4b(
account_id = x_account_id,
account_id = x_account['id'],
person_dict_obj = person_obj,
person_id = person_id,
# process_contact = process_contact,
# process_organization = process_organization,
# process_user = process_user,
): pass
else: return mk_resp(data=False, status_code=400, response=response, status_message='The person was not updated. Check the field names and data types.')
else: return mk_resp(data=False, status_code=400, response=commons.response, status_message='The person was not updated. Check the field names and data types.')
if isinstance(create_update_person_obj_result, int):
log.info('Create/Update successful')
@@ -190,7 +168,7 @@ async def v3_patch_person_obj_exist(
if return_obj:
if load_person_obj_result := load_person_obj(
person_id = person_id,
enabled = enabled,
enabled = commons.enabled,
inc_address = inc_address,
inc_contact = inc_contact,
inc_organization = inc_organization,
@@ -199,16 +177,16 @@ async def v3_patch_person_obj_exist(
data = load_person_obj_result
else:
data = False
return mk_resp(data=data, response=response, status_message='The person was probably updated, but there was a problem returning the data.')
return mk_resp(data=data, response=commons.response, status_message='The person was probably updated, but there was a problem returning the data.')
else:
person_id = create_update_person_obj_result
person_id_random = get_id_random(record_id=person_id, table_name='person')
data = {}
data['person_id'] = person_id
data['person_id_random'] = person_id_random
return mk_resp(data=data, response=response, status_message='The person was updated.')
return mk_resp(data=data, response=commons.response, status_message='The person was updated.')
else:
return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to update the person was unexpected.')
return mk_resp(data=False, status_code=400, response=commons.response, status_message='The result from trying to update the person was unexpected.')
# ### END ### API Person ### v3_patch_person_obj_exist ###
@@ -224,19 +202,13 @@ async def post_person_json(
process_organization: bool = False,
process_user: bool = False,
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
# include: Optional[list] = [],
# exclude: Optional[list] = [],
exclude_unset: Optional[bool] = True,
# exclude_none: Optional[bool] = True,
x_account_id: Optional[str] = Header(..., ),
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if person_obj_in_result := create_update_person_obj_v4b(
account_id = x_account_id,
account_id = x_account['id'],
person_dict_obj = person_obj,
person_id = None,
process_contact = process_contact,
@@ -246,12 +218,12 @@ async def post_person_json(
log.debug(person_obj_in_result)
if return_obj:
person_obj = load_person_obj(person_id=person_obj_in_result)
person_dict = person_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
person_dict = person_obj.dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
return mk_resp(data=person_dict)
else:
return mk_resp(data=person_obj_in_result)
else:
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
# ### END ### API Person ### post_person_json() ###
@@ -264,23 +236,17 @@ async def patch_person_json(
process_contact: bool = False,
process_organization: bool = False,
process_user: bool = False,
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
include: Optional[list] = [],
exclude: Optional[list] = [],
exclude_unset: Optional[bool] = True,
exclude_none: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
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
else: return mk_resp(data=None, status_code=404, response=response)
else: return mk_resp(data=None, status_code=404, response=commons.response)
if person_obj_up_result := create_update_person_obj_v4b(
account_id = x_account_id,
account_id = x_account['id'],
person_dict_obj = person_obj,
process_contact = process_contact,
process_organization = process_organization,
@@ -289,12 +255,12 @@ async def patch_person_json(
log.debug(person_obj_up_result)
if return_obj:
person_obj = load_person_obj(person_id=person_id)
person_dict = person_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
person_dict = person_obj.dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
return mk_resp(data=person_dict)
else:
return mk_resp(data=person_obj_up_result)
else:
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
# ### END ### API Person ### patch_person_json() ###
@@ -303,10 +269,7 @@ async def patch_person_json(
async def get_person_obj_li(
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -329,34 +292,29 @@ async def get_person_obj_li(
async def person_obj_external_id(
account_id: str = Query(..., min_length=1, max_length=22),
external_id: str = Query(..., min_length=1, max_length=75),
limit: int = 500,
enabled: str = 'enabled',
inc_address: bool = False,
inc_contact: bool = False,
# inc_person: bool = False,
inc_user: bool = False,
inc_user_role_list: bool = False,
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
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=None, status_code=404, response=response)
else: return mk_resp(data=None, status_code=404, response=commons.response)
# account_id = 99 # WARNING!!!! Get rid of 99!
if person_data := get_person_rec_w_external_id(account_id=account_id, external_id=external_id): pass
else: return mk_resp(data=None, status_code=404, response=response)
else: return mk_resp(data=None, status_code=404, response=commons.response)
person_id = person_data.get('person_id')
if person_dict := load_person_obj(
person_id = person_id,
limit = limit,
limit = commons.limit,
model_as_dict = True, # NOTE: returning model as a dict
enabled = enabled,
enabled = commons.enabled,
inc_address = inc_address,
inc_contact = inc_contact,
inc_user = inc_user,
@@ -367,7 +325,7 @@ async def person_obj_external_id(
else:
response_data = person_dict
else:
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data)
# ### END ### API Person ### person_obj_external_id() ###
@@ -382,18 +340,13 @@ async def lookup_email(
inc_contact: bool = False,
inc_user: bool = False,
inc_user_role_list: bool = False,
enabled: str = 'enabled',
limit: int = 50,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
x_account_id: str = Header(...),
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if account_id := redis_lookup_id_random(record_id_random=x_account_id, table_name='account'): pass
else: return mk_resp(data=None, status_code=404, response=response)
if account_id := redis_lookup_id_random(record_id_random=x_account['id'], table_name='account'): pass
else: return mk_resp(data=None, status_code=404, response=commons.response)
# import time
@@ -405,8 +358,8 @@ async def lookup_email(
for_obj_type = 'account',
for_obj_id = account_id,
email = email,
enabled = enabled,
limit = limit,
enabled = commons.enabled,
limit = commons.limit,
):
person_result_list = []
for person_rec in person_rec_list_result:
@@ -416,10 +369,10 @@ async def lookup_email(
inc_contact = inc_contact,
inc_user = inc_user,
inc_user_role_list = inc_user_role_list,
enabled = enabled,
limit = limit,
by_alias = by_alias,
exclude_unset = exclude_unset,
enabled = commons.enabled,
limit = commons.limit,
by_alias = commons.by_alias,
exclude_unset = commons.exclude_unset,
# model_as_dict = model_as_dict,
):
person_result_list.append(load_person_result)
@@ -428,10 +381,10 @@ async def lookup_email(
response_data = person_result_list
elif isinstance(person_rec_list_result, list) or person_rec_list_result is None: # Empty list or None
log.info('No results')
return mk_resp(data=None, status_code=404, response=response) # Not Found
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
else:
log.warning('Likely bad request')
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data)
# ### END ### API Person ### lookup_email() ###
@@ -444,20 +397,16 @@ async def lookup_email(
async def email_auth_key_url(
person_id: Optional[str] = Query(None, min_length=11, max_length=22),
root_url: Optional[str] = Query(None, min_length=10, max_length=100), # Absolute min = 7
x_account_id: Optional[str] = Header(..., ),
return_obj: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if account_id := redis_lookup_id_random(record_id_random=x_account_id, table_name='account'): pass
else: return mk_resp(data=False, status_code=404, response=response) # Not Found
if account_id := redis_lookup_id_random(record_id_random=x_account['id'], table_name='account'): pass
else: return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else: return mk_resp(data=False, status_code=404, response=response) # Not Found
else: return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
if result := email_person_create_url(
account_id = account_id,
@@ -465,10 +414,10 @@ async def email_auth_key_url(
root_url = root_url,
):
log.info('Email with create URL was sent.')
return mk_resp(data=True, response=response)
return mk_resp(data=True, response=commons.response)
else:
log.warning('Email with create URL was not sent.')
return mk_resp(data=False, status_code=500, response=response)
return mk_resp(data=False, status_code=500, response=commons.response)
# ### END ### API Person ### email_create_url() ###
@@ -480,8 +429,6 @@ async def email_auth_key_url(
async def get_person_obj(
person_id: str = Query(..., min_length=11, max_length=22),
auth_key: str = Query(None, min_length=11, max_length=22), # If passed, it must match in the person record. New 2021-12-15
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
inc_address: bool = False, # Priority l1
# inc_archive_list: bool = False, # Priority l3
inc_contact: bool = False, # Priority l1
@@ -509,24 +456,21 @@ async def get_person_obj(
inc_post_list: bool = False, # Priority l2
inc_post_comment_list: bool = False, # Priority l3
inc_user: bool = False, # Priority l1
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # 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
else: return mk_resp(data=None, status_code=404, response=response)
else: return mk_resp(data=None, status_code=404, response=commons.response)
if person_rec_result := load_person_obj(
person_id = person_id,
auth_key = auth_key,
limit = limit,
limit = commons.limit,
exclude_unset = False,
model_as_dict = False, # NOTE: returning model as a dict
enabled = enabled,
enabled = commons.enabled,
inc_address = inc_address,
# inc_archive_list = inc_archive_list,
inc_contact = inc_contact,
@@ -557,50 +501,40 @@ async def get_person_obj(
# else:
# response_data = person_rec_result
# else:
# return mk_resp(data=False, status_code=400, response=response) # Bad Request
# return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
elif isinstance(person_rec_result, list) or person_rec_result is None: # Empty list or None
log.info('No results')
if auth_key: log.info('It is likely the auth_key did not match.')
return mk_resp(data=False, status_code=404, response=response) # Not Found
return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
else:
log.warning('Likely bad request')
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data, response=response)
return mk_resp(data=response_data, response=commons.response)
# ### END ### API Person ### get_person_obj() ###
# ### BEGIN ### API Person ### get_account_obj_person_list() ###
# Working well as of 2021-07-09. Using as a template for other routes.
# Updated 2022-01-05
@router.get('/account/{account_id}/person/list', response_model=Resp_Body_Base)
async def get_account_obj_person_list(
account_id: str = Query(..., min_length=11, max_length=22),
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
inc_address: bool = False,
inc_contact: bool = False,
# inc_membership_group_list: bool = False, # The list of all membership groups a person is a part of
inc_membership_person: bool = False,
# inc_membership_person_profile: bool = False, # Profile?
# inc_membership_person_profile_cust: bool = False, # Extended profile?
# inc_membership_type_person: bool = False,
# inc_order: bool = False,
# inc_organization: bool = False,
# inc_product: bool = False, # The product the person actually purchased for a member_type or member_group
# inc_product_list: bool = False, # The list of products that give access to a member_type or member_group
inc_user: bool = False,
# x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
account: dict = Depends(get_account_header),
commons: Common_Route_Params = Depends(common_route_params),
):
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=None, status_code=404, response=response)
else: return mk_resp(data=None, status_code=404, response=commons.response)
response_data = None
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -609,18 +543,18 @@ async def get_account_obj_person_list(
if person_rec_list_result := get_person_rec_list(
for_obj_type = 'account',
for_obj_id = account_id,
limit = limit,
enabled = enabled,
limit = commons.limit,
enabled = commons.enabled,
):
person_result_list = []
for person_rec in person_rec_list_result:
if load_person_result := load_person_obj(
person_id = person_rec.get('person_id', None),
limit = limit,
by_alias = by_alias,
exclude_unset = exclude_unset,
limit = commons.limit,
by_alias = commons.by_alias,
exclude_unset = commons.exclude_unset,
# model_as_dict = model_as_dict,
enabled = enabled,
enabled = commons.enabled,
inc_address = inc_address,
inc_contact = inc_contact,
inc_membership_person = inc_membership_person,
@@ -631,17 +565,16 @@ async def get_account_obj_person_list(
person_result_list.append(None)
response_data = person_result_list
else:
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data, response=response)
return mk_resp(data=response_data, response=commons.response)
# ### END ### API Person ### get_account_obj_person_list() ###
@router.delete('/person/{obj_id}', response_model=Resp_Body_Base)
async def delete_person_obj(
obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: str = Header(...),
response: Response = Response,
obj_id: str = Query(..., min_length=11, max_length=22),
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())

View File

@@ -3,7 +3,7 @@ from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Resp
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from app.lib_general import log, logging, secure_hash_string, verify_secure_hash_string
from app.lib_general import log, logging, secure_hash_string, verify_secure_hash_string, common_route_params, Common_Route_Params
from app.config import settings
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, get_id_random,redis_lookup_id_random
@@ -23,11 +23,8 @@ router = APIRouter()
@router.post('/user', response_model=Resp_Body_Base)
async def post_user_obj(
obj: User_Base,
x_account_id: str = Header(...),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -51,11 +48,7 @@ async def post_user_obj_new(
user_obj: User_New_Base,
allow_update: bool = False,
avoid_dup_username: bool = False,
x_account_id: str = Header(...),
return_obj: bool = True,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -90,15 +83,12 @@ async def post_user_obj_new(
async def user_obj_change_password(
user_id: Union[int,str],
user_obj: User_Base,
x_account_id: Optional[str] = Header(..., ),
return_obj: bool = False,
inc_user_role_list: bool = False,
# inc_contact: bool = False,
# inc_organization: bool = False,
# inc_person: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -152,11 +142,8 @@ async def user_obj_change_password(
async def patch_user_obj(
obj: User_Base,
obj_id: str = Query(..., min_length=11, max_length=22),
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -181,12 +168,8 @@ async def patch_user_obj(
@router.get('/user/new_auth_key', response_model=Resp_Body_Base)
async def user_new_auth_key(
user_id: Optional[str] = Query(None, min_length=2, max_length=50),
x_account_id: str = Header(...),
return_obj: Optional[bool] = False,
by_alias: bool = True,
exclude_unset: bool = True,
exclude_none: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -231,15 +214,11 @@ async def user_authenticate(
password: Optional[str] = Query(None, min_length=8, max_length=100),
auth_key: Optional[str] = Query(None, min_length=11, max_length=22),
valid_email: Optional[bool] = None,
x_account_id: str = Header(...),
inc_user_role_list: bool = False,
inc_contact: bool = False,
inc_organization: bool = False,
inc_person: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
exclude_none: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -402,11 +381,8 @@ async def user_verify_password(
# user_id: Optional[str] = Query(None, min_length=11, max_length=22),
# username: Optional[str] = Query(None, min_length=3, max_length=50),
# password: Optional[str] = Query(None, min_length=8, max_length=50),
x_account_id: Optional[str] = Header(..., ),
return_obj: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -492,17 +468,12 @@ async def user_verify_password(
@router.get('/account/{account_id}/user/list', response_model=Resp_Body_Base)
async def get_account_user_obj_li(
account_id: str = Query(..., min_length=11, max_length=22),
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
hidden: str = 'not_hidden', # hidden, not_hidden, all
inc_address: bool = False, # Priority l1
inc_contact: bool = False, # Priority l1
inc_person: bool = False, # Priority l1
inc_user_role_list: bool = False, # Priority l1
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -551,10 +522,7 @@ async def get_account_user_obj_li(
async def get_user_obj_li(
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -575,14 +543,11 @@ async def get_user_obj_li(
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_user_role_list: bool = False,
inc_contact: bool = False,
inc_organization: bool = False,
inc_person: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -653,16 +618,11 @@ async def lookup_user_obj(
async def lookup_email(
account_id: Union[int,str],
email: str = Query(..., min_length=2, max_length=50),
x_account_id: str = Header(...),
inc_user_role_list: bool = False,
inc_contact: bool = False,
inc_organization: bool = False,
inc_person: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -753,15 +713,12 @@ async def lookup_email(
async def lookup_username(
account_id: Union[int,str],
username: str = Query(..., min_length=2, max_length=50),
x_account_id: str = Header(...),
inc_address: bool = False,
inc_contact: bool = False,
inc_organization: bool = False,
inc_person: bool = False,
inc_user_role_list: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -831,11 +788,8 @@ async def lookup_username(
async def email_auth_key_url(
user_id: Optional[str] = Query(None, min_length=11, max_length=22),
root_url: Optional[str] = Query(None, min_length=10, max_length=100), # Absolute min = 7
x_account_id: Optional[str] = Header(..., ),
return_obj: bool = False,
by_alias: bool = True,
exclude_unset: bool = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -865,8 +819,6 @@ async def email_auth_key_url(
@router.get('/user/{user_id}', response_model=Resp_Body_Base)
async def get_user_obj(
user_id: str = Query(..., min_length=11, max_length=22),
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
inc_address: bool = False, # Priority l1
# inc_archive_list: bool = False, # Priority l3
inc_contact: bool = False, # Priority l1
@@ -886,10 +838,7 @@ async def get_user_obj(
inc_post_list: bool = False, # Priority l2
inc_post_comment_list: bool = False, # Priority l3
inc_user_role_list: bool = False, # Priority l1
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -935,8 +884,8 @@ async def get_user_obj(
# @router.get('/user/{user_id}/order_list', response_model=Resp_Body_Base)
# async def get_user_obj_order_list(
# user_id: str = Query(..., min_length=1, max_length=22),
# limit: int = 500, # For now this covers any included objects or object lists
# enabled: str = 'enabled', # For now this covers any included objects or object lists
#
#
# from_datetime: datetime.datetime = None,
# to_datetime: datetime.datetime = None,
# # inc_address: bool = False,
@@ -947,7 +896,7 @@ async def get_user_obj(
# # inc_person: bool = False,
# # inc_order_list: bool = False,
# # inc_order_cart_list: bool = False,
# x_account_id: str = Header(...),
#
# by_alias: Optional[bool] = True,
# exclude_unset: Optional[bool] = True,
# response: Response = Response,
@@ -999,9 +948,8 @@ async def get_user_obj(
@router.delete('/user/{obj_id}', response_model=Resp_Body_Base)
async def delete_user_obj(
obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: str = Header(...),
response: Response = Response,
obj_id: str = Query(..., min_length=11, max_length=22),
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())