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

@@ -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'