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