Work on data store and related

This commit is contained in:
Scott Idem
2022-07-27 18:05:08 -04:00
parent c87531aff7
commit dd1b3c0b1b
10 changed files with 167 additions and 26 deletions

View File

@@ -105,13 +105,13 @@ class Common_Route_Params_Min:
exclude_unset: bool = True,
response = None,
):
# self.x_account_id = x_account_id
# self.x_account_id_random = x_account_id_random
# self.enabled = enabled
# self.limit = limit
# self.offset = offset
# self.by_alias = by_alias
# self.exclude_unset = exclude_unset
self.x_account_id = x_account_id
self.x_account_id_random = x_account_id_random
self.enabled = enabled
self.limit = limit
self.offset = offset
self.by_alias = by_alias
self.exclude_unset = exclude_unset
self.response = response
# log.debug(response)
# ### END ### API Lib General ### class Common_Route_Params_Min ###

View File

@@ -25,7 +25,7 @@ def load_data_store_obj(
exclude_unset: bool = True,
model_as_dict: bool = False,
) -> Data_Store_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 data_store_rec := sql_select(table_name='v_data_store', record_id=data_store_id): pass
@@ -69,10 +69,14 @@ def load_data_store_obj_w_code(
data['account_id'] = account_id
data['code'] = code
log.debug(data)
log.warning(f'Can we get past this?????????? {code}')
sql_enabled, data['enable'] = sql_enable_part(table_name='data_store', enabled=enabled) # Reasonably safe return str and bool
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
log.debug(data)
log.warning(f'Where are we now??????????? {code}')
sql = f"""
SELECT *
@@ -87,6 +91,7 @@ def load_data_store_obj_w_code(
ORDER BY `data_store`.account_id DESC, `data_store`.created_on DESC, `data_store`.updated_on DESC
{sql_limit};
"""
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(sql)
if data_store_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
@@ -109,6 +114,7 @@ def load_data_store_obj_w_code(
log.debug(data_store_obj)
else: pass
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(data_store_obj_li)
return data_store_obj_li

View File

@@ -53,14 +53,14 @@ def load_site_domain_obj(
if site_domain_id := redis_lookup_id_random(record_id_random=site_domain_id, table_name='site_domain'): pass
else: return False
# if site_domain_rec := sql_select(table_name='v_site_domain', record_id=site_domain_id):
# log.debug(site_domain_rec)
# else: return False
if site_domain_rec := sql_select(table_name='site_domain', record_id=site_domain_id):
if site_domain_rec := sql_select(table_name='v_site_domain', record_id=site_domain_id):
log.debug(site_domain_rec)
else: return False
# if site_domain_rec := sql_select(table_name='site_domain', record_id=site_domain_id):
# log.debug(site_domain_rec)
# else: return False
log.debug(site_domain_rec)
try:
@@ -142,3 +142,38 @@ def get_site_domain_rec_list(
return site_domain_rec_li
# ### END ### API Site Domain Methods ### get_site_domain_rec_list() ###
# ### BEGIN ### API Site Domain Methods ### lookup_site_domain_fqdn() ###
def lookup_site_domain_fqdn(
fqdn: str,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 100,
offset: int = 0,
) -> list|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
data = {}
data['fqdn'] = fqdn
sql_enabled, data['enable'] = sql_enable_part(table_name='site_domain', 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 `site_domain`.id AS 'site_domain_id', `site_domain`.id_random AS 'site_domain_id_random'
FROM `v_site_domain` AS site_domain
WHERE
site_domain.fqdn = :fqdn
{sql_enabled}
ORDER BY `site_domain`.fqdn ASC, `site_domain`.access_key ASC, `site_domain`.required_referrer ASC, `site_domain`.created_on DESC, `site_domain`.updated_on DESC
{sql_limit};
"""
if site_domain_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
site_domain_rec_li = site_domain_rec_li_result
else:
site_domain_rec_li = []
return site_domain_rec_li
# ### END ### API Site Domain Methods ### get_site_domain_rec_list() ###

View File

@@ -11,7 +11,7 @@ from app.models.common_field_schema import base_fields, default_num_bytes
# ### BEGIN ### API Data Store Models ### Data_Store_Base() ###
class Data_Store_Base(BaseModel):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
id_random: Optional[str] = Field(

View File

@@ -37,6 +37,11 @@ class Site_Domain_Base(BaseModel):
created_on: Optional[datetime.datetime] = None
updated_on: Optional[datetime.datetime] = None
# Including convenience data
# This is only for convenience. Probably going to keep unless it causes a problem.
account_id: Optional[int]
account_id_random: Optional[str]
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
@validator('id', always=True)

View File

@@ -224,12 +224,14 @@ async def get_obj_li(
base_name = obj_type_li[obj_name]['base_name']
resp_data_li = []
for record in sql_result:
resp_data = base_name(**record).dict(by_alias=by_alias, exclude_unset=exclude_unset)
resp_data_li.append(resp_data)
if sql_result:
resp_data_li = []
for record in sql_result:
resp_data = base_name(**record).dict(by_alias=by_alias, exclude_unset=exclude_unset)
resp_data_li.append(resp_data)
return mk_resp(data=resp_data_li, response=response)
return mk_resp(data=resp_data_li, response=response)
else: return mk_resp(data=None, response=response, status_code=404)
#@router.get('/{obj_type_l1}/{obj_id_int}')
@@ -267,7 +269,7 @@ async def get_obj(
- /order/cart/line = order_cart_line
- /lu/some_lookup = lu_some_lookup
"""
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
debug_data = {}

View File

@@ -143,7 +143,7 @@ async def get_data_store_obj_w_code(
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# ### SECTION ### Secondary data validation
@@ -159,6 +159,7 @@ async def get_data_store_obj_w_code(
):
log.info('Loading successful. Returning result')
data_store_obj = data_store_obj_result[0] # Get first record only
log.debug(data_store_obj)
return mk_resp(data=data_store_obj, response=commons.response)
elif isinstance(data_store_obj_result, list) or data_store_obj_result is None: # Empty list or None
log.info('No results')

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, get_id_random, redis_lookup_id_random

View File

@@ -4,14 +4,16 @@ 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, common_route_params_min, Common_Route_Params_Min
#from ..log import *
from app.config import settings
from app.db_sql import *
from app.db_sql import sql_enable_part, sql_insert, sql_update, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_delete, redis_lookup_id_random
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
from app.models.response_models import *
from app.methods.site_domain_methods import load_site_domain_obj, lookup_site_domain_fqdn
from app.models.response_models import Resp_Body_Base, mk_resp
from app.models.site_models import Site_Base
from app.models.site_domain_models import Site_Domain_Base
@@ -73,6 +75,93 @@ async def patch_site_domain_obj(
return result
@router.get('/site/domain/fqdn/{fqdn}', response_model=Resp_Body_Base)
async def lookup_site_domain_obj(
fqdn: str,
# x_account_id: str = Header(...),
# response: Response = Response,
commons: Common_Route_Params_Min = Depends(common_route_params_min),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# Updated 2021-12-13
if site_domain_rec_list_result := lookup_site_domain_fqdn(
fqdn = fqdn,
enabled = commons.enabled,
limit = commons.limit,
offset = commons.offset
):
site_domain_result_list = []
for site_domain_rec in site_domain_rec_list_result:
if load_site_domain_result := load_site_domain_obj(
site_domain_id = site_domain_rec.get('site_domain_id', None),
enabled = commons.enabled,
limit = commons.limit,
# by_alias = commons.by_alias,
# exclude_unset = commons.exclude_unset,
# model_as_dict = commons.model_as_dict,
):
site_domain_result_list.append(load_site_domain_result)
else:
site_domain_result_list.append(None)
response_data = site_domain_result_list
elif isinstance(site_domain_rec_list_result, list) or site_domain_rec_list_result is None: # Empty list or None
log.info('No results')
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=commons.response) # Bad Request
return mk_resp(data=response_data, response=commons.response)
# data = {}
# data['fqdn'] = fqdn
# log.debug(data)
# sql = f"""
# SELECT *, site_enable_from AS 'enable_from', site_enable_to AS 'enable_to'
# FROM `v_site_domain` AS site_domain
# WHERE site_domain.fqdn = :fqdn;
# """
# log.debug(sql)
# # NOTE: This need to broken out into a methods function for site and or site_domain
# if site_domain_obj_result := sql_select(data=data, sql=sql):
# if isinstance(site_domain_obj_result, dict):
# log.info('Got a site domain result')
# try:
# site_obj = Site_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
# except ValidationError as e:
# log.error(e.json())
# log.debug(site_obj)
# try:
# site_domain_obj = Site_Domain_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
# except ValidationError as e:
# log.error(e.json())
# log.debug(site_domain_obj)
# site_domain_obj['site'] = site_obj
# return mk_resp(data=site_domain_obj, response=response)
# elif isinstance(site_domain_obj_result, list):
# log.info('Got multiple site domain results')
# log.warning(f'More than one site domain records were returned. This was unexpected and needs to be corrected. FQDN: {fqdn}')
# site_domain_obj_li = site_domain_obj_result
# return mk_resp(data=site_domain_obj_li, response=response)
# else:
# log.error(f'Unexpected results for the site domain records was returned. FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}')
# return mk_resp(data=False, status_code=500, status_message=f'Unexpected results for the site domain records was returned. FQDN: {fqdn}; Access Key {access_key}; Referrer: {referrer}', response=response)
# else:
# log.info('No site domain results')
# log.debug(site_domain_obj_result)
# return mk_resp(data=False, status_code=404, response=response)
@router.get('/site/domain/lookup_fqdn', response_model=Resp_Body_Base)
async def lookup_site_domain_obj(
fqdn: str,

View File

@@ -25,13 +25,15 @@ async def select_result(
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
sql = sql_request.sql_qry
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(sql)
data = sql_request.sql_data
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(data)
if sql_query_result := sql_select(data=data, sql=sql, as_list=as_list):
@@ -41,5 +43,6 @@ async def select_result(
data = None
status_code = 404
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(sql_query_result)
return mk_resp(data=data, status_code=status_code, response=commons.response)