Moving things to use the common_route_params and unified functions for SQL enable and SQL LIMIT OFFSET

This commit is contained in:
Scott Idem
2022-01-17 18:57:31 -05:00
parent 9efaa2bf61
commit 6b21a33625
9 changed files with 139 additions and 134 deletions

View File

@@ -1253,3 +1253,51 @@ def get_account_id_w_for_type_id(
else: return False
else: return None
# ### END ### API DB SQL Methods ### get_account_id_w_for_type_id() ###
# ### BEGIN ### API DB SQL Methods ### sql_enable_part() ###
# Updated 2022-01-17
@logger_reset
def sql_enable_part(table_name:str, enabled: str) -> bool|dict:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if not table_name: return False
if enabled in ['enabled', 'disabled', 'all']:
log.info(f'Creating partial SQL string for "enabled" check. Enabled: {enabled}')
if enabled == 'enabled':
# sql = f'AND `person`.enable = :enable'
sql = f'AND `{table_name}`.enable = true'
enable = True
elif enabled == 'disabled':
# sql = f'AND `person`.enable = :enable'
sql = f'AND `{table_name}`.enable = false'
enable = False
elif enabled == 'all':
sql = f'AND (`{table_name}`.enable = true OR `{table_name}`.enable = false OR `{table_name}`.enable IS NULL)'
enable = None
log.debug(sql)
log.debug(enable)
# return result
return sql, enable
else:
return False
# ### END ### API DB SQL Methods ### sql_enable_part() ###
# ### BEGIN ### API DB SQL Methods ### sql_limit_offset_part() ###
# Updated 2022-01-17
@logger_reset
def sql_limit_offset_part(limit: int, offset: int = 0) -> bool|str:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if limit >= 0 and offset >= 0:
log.info(f'Creating partial SQL string for LIMIT and OFFSET. Limit: {limit}; Offset: {offset}')
sql = f'LIMIT {limit} OFFSET {offset}'
return sql
else:
return False
# ### END ### API DB SQL Methods ### sql_limit_offset_part() ###

View File

@@ -33,16 +33,18 @@ class Common_Route_Params:
self,
x_account_id: int,
x_account_id_random: str,
limit: int = 10,
enabled: str = 'enabled',
limit: int = 10,
offset: int = 0,
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.limit = limit
self.offset = offset
self.by_alias = by_alias
self.exclude_unset = exclude_unset
self.response = response
@@ -56,6 +58,7 @@ 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,
offset: int = 0,
by_alias: bool = True,
exclude_none: Optional[bool] = True,
exclude_unset: bool = True,
@@ -82,7 +85,7 @@ async def common_route_params(
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
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 = Common_Route_Params( x_account_id=x_account_id, x_account_id_random=x_account_id_random, limit=limit, offset=offset, enabled=enabled, by_alias=by_alias, exclude_unset=exclude_unset, response=response )
log.debug(commons)

View File

@@ -4,7 +4,7 @@ import datetime
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from app.db_sql import redis_lookup_id_random, sql_select
from app.db_sql import redis_lookup_id_random, sql_enable_part, sql_limit_offset_part, sql_select
from app.lib_general import log, logging
from app.methods.account_cfg_methods import load_account_cfg_obj
@@ -36,11 +36,12 @@ from app.models.account_cfg_models import Account_Cfg_Base
# Working well as of 2021-06-11. Using as a template for other load objects.
def load_account_obj(
account_id: int|str,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1000,
offset: int = 0,
by_alias: bool = True,
exclude_unset: bool = True,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_account_cfg: bool = False, # Priority l1
inc_address: bool = False, # Under contact
inc_address_list: bool = False, # Priority l3
@@ -152,17 +153,19 @@ def load_account_obj(
if address_rec_list_result := get_address_rec_list(
for_type = 'account', # 'account' is a special case
for_id = account_id,
limit = limit,
enabled = enabled,
limit = limit,
offset = offset,
):
address_dict_list = []
for address_rec in address_rec_list_result:
address_dict_list.append(
load_address_obj(
address_id = address_rec.get('address_id', None),
limit = limit,
model_as_dict = model_as_dict,
enabled = enabled,
limit = limit,
offset = offset,
model_as_dict = model_as_dict,
)
)
account_obj.address_list = address_dict_list
@@ -195,17 +198,19 @@ def load_account_obj(
if contact_rec_list_result := get_contact_rec_list(
for_type = 'account', # 'account' is a special case
for_id = account_id,
limit = limit,
enabled = enabled,
limit = limit,
offset = offset,
):
contact_dict_list = []
for contact_rec in contact_rec_list_result:
contact_dict_list.append(
load_contact_obj(
contact_id = contact_rec.get('contact_id', None),
limit = limit,
model_as_dict = model_as_dict,
enabled = enabled,
limit = limit,
offset = offset,
model_as_dict = model_as_dict,
inc_address = inc_address,
)
)
@@ -438,8 +443,9 @@ def load_account_obj(
if person_rec_list_result := get_person_rec_list(
for_obj_type = 'account',
for_obj_id = account_id,
limit = limit,
enabled = enabled,
limit = limit,
offset = offset,
):
person_result_list = []
for person_rec in person_rec_list_result:
@@ -447,6 +453,7 @@ def load_account_obj(
load_person_obj(
person_id = person_rec.get('person_id', None),
limit = limit,
offset = offset,
by_alias = by_alias,
exclude_unset = exclude_unset,
model_as_dict = model_as_dict,
@@ -569,32 +576,17 @@ def load_account_obj(
# ### BEGIN ### API Account Methods ### get_account_rec_list() ###
# Updated 2021-12-13
def get_account_rec_list(
limit: int = 25,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 25,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
data = {}
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
data['enable'] = True
sql_enabled = f'`account`.enable = :enable'
elif enabled == 'disabled':
data['enable'] = False
sql_enabled = f'`account`.enable = :enable'
elif enabled == 'all':
sql_enabled = ''
else:
log.warning(f'Unexpected value for enabled: {enabled}')
return False
sql_enabled, data['enable'] = sql_enable_part(table_name='account', enabled=enabled) # Reasonably safe return str and bool
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
sql = f"""
SELECT `account`.id AS 'account_id', `account`.id_random AS 'account_id_random'

View File

@@ -4,7 +4,7 @@ import datetime
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from app.db_sql import get_account_id_w_for_type_id, redis_lookup_id_random, sql_insert, sql_select, sql_update
from app.db_sql import get_account_id_w_for_type_id, redis_lookup_id_random, sql_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update
from app.lib_general import log, logging, logger_reset
from app.models.address_models import Address_Base
@@ -14,11 +14,12 @@ from app.models.common_field_schema import default_num_bytes
# ### BEGIN ### API Address Methods ### load_address_obj() ###
def load_address_obj(
address_id:int|str,
limit: int = 1000, # Probably not needed for the address
enabled: str = 'enabled', # enabled, disabled, all # Probably not needed for the address
limit: int = 100, # Probably not needed for the address
offset: int = 0,
by_alias: bool = True,
exclude_unset: bool = True,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all # Probably not needed for the address
) -> Address_Base|dict|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -49,8 +50,9 @@ def load_address_obj(
def get_address_rec_list(
for_type: str, # 'account' is a special case
for_id: str,
limit: int = 500,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 500,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -69,21 +71,8 @@ def get_address_rec_list(
data['for_id'] = for_id
# sql_obj_type_id = f'`address`.{for_type}_id = :{for_type}_id'
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
data['enable'] = True
sql_enabled = f'AND `address`.enable = :enable'
elif enabled == 'disabled':
data['enable'] = False
sql_enabled = f'AND `address`.enable = :enable'
elif enabled == 'all':
sql_enabled = ''
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql_enabled, data['enable'] = sql_enable_part(table_name='address', enabled=enabled) # Reasonably safe return str and bool
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
sql = f"""
SELECT `address`.id AS 'address_id', `address`.id_random AS 'address_id_random'

View File

@@ -4,7 +4,7 @@ import datetime
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from app.db_sql import get_account_id_w_for_type_id, redis_lookup_id_random, sql_insert, sql_select, sql_update
from app.db_sql import get_account_id_w_for_type_id, redis_lookup_id_random, sql_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update
from app.lib_general import log, logging, logger_reset
from app.methods.address_methods import create_address_obj, create_update_address_obj, create_update_address_obj_v4, update_address_obj
@@ -16,11 +16,12 @@ from app.models.contact_models import Contact_Base
# ### BEGIN ### API Contact Methods ### load_contact_obj() ###
def load_contact_obj(
contact_id: int|str,
limit: int = 1000,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 100,
offset: int = 0,
by_alias: bool = True,
exclude_unset: bool = True,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_address: bool = False
) -> Contact_Base|dict|bool:
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -71,8 +72,9 @@ def load_contact_obj(
def get_contact_rec_list(
for_type: str, # 'account' is a special case
for_id: str,
limit: int = 500,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 500,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -89,21 +91,8 @@ def get_contact_rec_list(
data['for_type'] = for_type
data['for_id'] = for_id
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
data['enable'] = True
sql_enabled = f'AND `contact`.enable = :enable'
elif enabled == 'disabled':
data['enable'] = False
sql_enabled = f'AND `contact`.enable = :enable'
elif enabled == 'all':
sql_enabled = ''
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql_enabled, data['enable'] = sql_enable_part(table_name='contact', enabled=enabled) # Reasonably safe return str and bool
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
sql = f"""
SELECT `contact`.id AS 'contact_id', `contact`.id_random AS 'contact_id_random'

View File

@@ -4,7 +4,7 @@ import datetime, pytz, secrets
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from app.db_sql import redis_lookup_id_random, sql_insert, sql_insert_or_update, sql_select, sql_update
from app.db_sql import redis_lookup_id_random, sql_enable_part, sql_insert, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_update
from app.lib_general import log, logging, logger_reset, send_email
# from app.methods.address_methods import load_address_obj
@@ -24,11 +24,12 @@ from app.models.person_models import Person_Base
def load_person_obj(
person_id: int|str,
auth_key: str = None,
limit: int = 1000,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 100,
offset: int = 0,
by_alias: bool = True,
exclude_unset: bool = True,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_address: bool = False, # Under contact
inc_contact: bool = False,
inc_event_list: bool = False,
@@ -56,7 +57,7 @@ def load_person_obj(
inc_user: bool = False,
inc_user_role_list: bool = False,
) -> Person_Base|dict|bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
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
@@ -349,10 +350,11 @@ def get_person_rec_list(
for_obj_type: str,
for_obj_id: str,
email: str = None,
limit: int = 1000,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1000,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
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
@@ -366,22 +368,8 @@ def get_person_rec_list(
if email:
sql_where_email = 'AND (user.email = :email OR contact.email = :email)'
# if enabled in ['enabled', 'disabled', 'all']:
# if enabled == 'enabled':
# data['enable'] = True
# sql_enabled = f'AND `person`.enable = :enable'
# elif enabled == 'disabled':
# data['enable'] = False
# sql_enabled = f'AND `person`.enable = :enable'
# elif enabled == 'all':
# sql_enabled = ''
sql_enabled = ''
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql_enabled, data['enable'] = sql_enable_part(table_name='person', enabled=enabled) # Reasonably safe return str and bool
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
if not email:
sql = f"""

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
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, redis_lookup_id_random
@@ -98,31 +98,27 @@ async def get_account_obj_li(
inc_site_list: bool = False,
inc_site_domain_list: bool = False,
inc_user_list: bool = False,
x_account_id: str = Header(...),
return_obj: bool = True,
limit: int = 25,
enabled: str = 'enabled',
by_alias: 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())
# Updated 2021-09-28
if account_rec_list_result := get_account_rec_list(
limit = limit,
enabled = enabled,
limit = commons.limit,
enabled = commons.enabled,
):
account_result_list = []
for account_rec in account_rec_list_result:
if load_account_result := load_account_obj(
account_id = account_rec.get('account_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_address_list = inc_address_list,
# inc_archive_list = inc_archive_list,
@@ -147,9 +143,9 @@ async def get_account_obj_li(
account_result_list.append(None)
response_data = account_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)
# ### BEGIN ### API Account ### get_account_obj_new() ###
@@ -157,8 +153,6 @@ async def get_account_obj_li(
@router.get('/{account_id}', response_model=Resp_Body_Base)
async def get_account_obj_new(
account_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
inc_account_cfg: bool = False, # Priority l1
inc_address: bool = False, # Under contact
inc_address_list: bool = False,
@@ -229,23 +223,21 @@ async def get_account_obj_new(
inc_site_domain_list: bool = False,
inc_user: bool = False,
inc_user_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.WARNING) # 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)
if account_result := load_account_obj(
account_id = account_id,
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 = False, # NOTE: returning model as a dict
inc_account_cfg = inc_account_cfg,
inc_address = inc_address,
@@ -327,9 +319,9 @@ async def get_account_obj_new(
# log.debug(account_result.dict(by_alias=True, exclude_unset=True)) # pylint: disable=no-member
# print('---------------------------')
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 Account ### get_account_obj_new() ###
@@ -354,10 +346,8 @@ async def delete_account_obj(
async def get_account_cfg_obj(
account_id: str = Query(..., min_length=11, max_length=22),
sys_module: Optional[str] = None, # event, fundraising, membership
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())
@@ -365,19 +355,19 @@ async def get_account_cfg_obj(
# print(x_account_id)
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, status_message=f'The Account ID was not found. Account ID: {account_id}', response=response)
else: return mk_resp(data=None, status_code=404, status_message=f'The Account ID was not found. Account ID: {account_id}', response=commons.response)
if sys_module:
if sys_module == 'membership':
membership_cfg_obj = load_account_cfg_obj(account_id=account_id, inc_membership_cfg=True).dict(by_alias=by_alias, exclude_unset=exclude_unset)
data = membership_cfg_obj
return mk_resp(data=data, response=response)
return mk_resp(data=data, response=commons.response)
else:
if account_cfg_obj := load_account_cfg_obj(account_id=account_id).dict(by_alias=by_alias, exclude_unset=exclude_unset):
data = account_cfg_obj
return mk_resp(data=data, response=response)
return mk_resp(data=data, response=commons.response)
else:
return mk_resp(data=False, status_code=404, response=response)
return mk_resp(data=False, status_code=404, response=commons.response)
# obj_type = 'account_cfg'
# result = get_obj_template(

View File

@@ -446,6 +446,7 @@ async def lookup_email(
email = email,
enabled = commons.enabled,
limit = commons.limit,
offset = commons.offset,
):
person_result_list = []
for person_rec in person_rec_list_result:
@@ -614,7 +615,7 @@ async def get_account_obj_person_list(
inc_user: bool = False,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.INFO) # 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
@@ -623,11 +624,12 @@ async def get_account_obj_person_list(
response_data = None
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# Updated 2021-06-30
# Updated 2022-01-17
if person_rec_list_result := get_person_rec_list(
for_obj_type = 'account',
for_obj_id = account_id,
limit = commons.limit,
offset = commons.offset,
enabled = commons.enabled,
):
person_result_list = []
@@ -648,7 +650,11 @@ async def get_account_obj_person_list(
else:
person_result_list.append(None)
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=commons.response) # Not Found
else:
log.warning('Likely bad request')
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data, response=commons.response)

View File

@@ -32,11 +32,11 @@ async def post_user_obj(
obj_type = 'user'
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
result = post_obj_template(
obj_type=obj_type,
data=obj_data_dict,
return_obj=True,
by_alias=True,
exclude_unset=True,
obj_type = obj_type,
data = obj_data_dict,
return_obj = True,
by_alias = True,
exclude_unset = True,
)
return result