Update to Redis function for looking up id_random values. Updates related to IDAA and posts
This commit is contained in:
@@ -1056,12 +1056,14 @@ def sql_delete(
|
|||||||
# If success then return the ID number
|
# If success then return the ID number
|
||||||
# If not success and there is a table_name then check the database table passed
|
# If not success and there is a table_name then check the database table passed
|
||||||
# If found in database table then store in Redis and return the ID number
|
# If found in database table then store in Redis and return the ID number
|
||||||
|
# Updated 2023-11-15
|
||||||
@logger_reset
|
@logger_reset
|
||||||
def redis_lookup_id_random(
|
def redis_lookup_id_random(
|
||||||
record_id_random: int|str,
|
record_id_random: int|str,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
check_int_id: bool = False,
|
check_int_id: bool = False,
|
||||||
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log_lvl: int = logging.INFO, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
|
minutes: int = 7,
|
||||||
) -> str|int|bool|None:
|
) -> str|int|bool|None:
|
||||||
log.setLevel(log_lvl)
|
log.setLevel(log_lvl)
|
||||||
|
|
||||||
@@ -1086,40 +1088,42 @@ def redis_lookup_id_random(
|
|||||||
log.info(f'No record ID was passed. Returning None')
|
log.info(f'No record ID was passed. Returning None')
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
log.warning(f'Unexpected data type or string format: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.')
|
log.error(f'Unexpected data type or string format: {type(record_id_random)} Expected type is a string 11 or 22 characters long.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if record_id_random and table_name:
|
if record_id_random and table_name:
|
||||||
# WARNING: The record_id_random string length should be checked just in case?
|
# WARNING: The record_id_random string length should be checked just in case?
|
||||||
if len(record_id_random) < 11:
|
if len(record_id_random) < 11:
|
||||||
log.warning(f'The length of id_random is too short: {str(record_id_random)} ({len(record_id_random)} chars)')
|
log.error(f'The length of id_random is too short: {record_id_random} ({len(record_id_random)} chars)')
|
||||||
return False
|
return False
|
||||||
elif len(record_id_random) > 22:
|
elif len(record_id_random) > 22:
|
||||||
log.warning(f'The length of id_random is too long {str(record_id_random)} ({len(record_id_random)} chars)')
|
log.error(f'The length of id_random is too long: {record_id_random} ({len(record_id_random)} chars)')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
elif record_id_random:
|
elif record_id_random:
|
||||||
log.warning('Missing id_random')
|
log.error(f'Missing table_name to select from for id_random "{record_id_random}"')
|
||||||
return False
|
return False
|
||||||
elif table_name:
|
elif table_name:
|
||||||
log.warning('Missing table_name to select from for id_random')
|
log.error(f'Missing id_random to select from table "{table_name}"')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
log.warning('Missing table_name and record_id_random')
|
log.error('Missing table_name and record_id_random')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# r = redis.Redis(host='localhost', port=6379, db=7, password=None, decode_responses=True)
|
# r = redis.Redis(host='localhost', port=6379, db=7, password=None, decode_responses=True)
|
||||||
r = redis.Redis(host=settings.REDIS['server'], port=settings.REDIS['port'], db=7, password=None, decode_responses=True)
|
r = redis.Redis(host=settings.REDIS['server'], port=settings.REDIS['port'], db=7, password=None, decode_responses=True)
|
||||||
|
|
||||||
key_name = 'record_id:'+record_id_random
|
# key_name = 'record_id:'+record_id_random
|
||||||
|
key_name = f'{table_name}:{record_id_random}'
|
||||||
|
|
||||||
record_id = r.get(key_name)
|
record_id = r.get(key_name)
|
||||||
log.debug(f'Record ID? {str(record_id)}')
|
log.debug(f'Record ID found: {record_id}')
|
||||||
|
|
||||||
if record_id:
|
if record_id:
|
||||||
log.info('The record ID was found using the record_id_random value.')
|
# log.info('The record ID was found using the record_id_random value.')
|
||||||
log.info(f'TTL for: {key_name} : {str(record_id)} is {str(r.ttl(key_name))} seconds')
|
# r.setex(key_name, datetime.timedelta(minutes=1), value=record_id)
|
||||||
|
log.info(f'Redis entry found. TTL for: {key_name} : {record_id} is {r.ttl(key_name)} seconds')
|
||||||
|
|
||||||
return int(record_id)
|
return int(record_id)
|
||||||
elif table_name:
|
elif table_name:
|
||||||
@@ -1131,27 +1135,27 @@ def redis_lookup_id_random(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if select_results := sql_select(sql=sql, data=data):
|
if select_results := sql_select(sql=sql, data=data):
|
||||||
log.debug(select_results)
|
log.debug(f'SQL SELECT result: {select_results}')
|
||||||
log.debug(type(select_results))
|
# log.debug(type(select_results))
|
||||||
if isinstance(select_results, dict):
|
if isinstance(select_results, dict):
|
||||||
log.info(f"""Record ID random found: {str(select_results['id'])}""")
|
log.info(f"""Record ID random found: {str(select_results['id'])}""")
|
||||||
if record_id := select_results.get('id'):
|
if record_id := select_results.get('id'):
|
||||||
r.setex(key_name, datetime.timedelta(minutes=90), value=record_id)
|
r.setex(key_name, datetime.timedelta(minutes=minutes), value=record_id)
|
||||||
|
|
||||||
return int(record_id)
|
return int(record_id)
|
||||||
else:
|
else:
|
||||||
log.error('The SQL result was not what was expected.')
|
log.error('The SQL result was not what was expected. The ID field was not found.')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
log.error('More than one record may have been found. There may be a duplicate id_random.')
|
log.error(f'More than one record may have been found in the table "{table_name}". There may be a duplicate id_random value in this table.')
|
||||||
log.error(select_results)
|
log.error(select_results)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
log.info('Record ID random was not found')
|
log.info(f'Record ID random "{record_id_random}" was not found in table "{table_name}". Returning None.')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
log.error('We should not be here. Something unexpected happened.')
|
log.error('We should not be here. Something unexpected happened.')
|
||||||
return False # Just in case
|
return False
|
||||||
# ### END ### API DB SQL ### redis_lookup_id_random() ###
|
# ### END ### API DB SQL ### redis_lookup_id_random() ###
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ class Post_Base(BaseModel):
|
|||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
id_random: Optional[str] = Field(
|
id_random: Optional[str] = Field(
|
||||||
**base_fields['post_id_random'],
|
# **base_fields['post_id_random'],
|
||||||
alias = 'post_id_random',
|
alias = 'post_id_random',
|
||||||
)
|
)
|
||||||
id: Optional[int] = Field(
|
id: Optional[int] = Field(
|
||||||
#alias = 'post_id'
|
alias = 'post_id'
|
||||||
)
|
)
|
||||||
account_id_random: Optional[str]
|
account_id_random: Optional[str]
|
||||||
account_id: Optional[int]
|
account_id: Optional[int]
|
||||||
@@ -105,29 +105,23 @@ class Post_Base(BaseModel):
|
|||||||
|
|
||||||
@validator('account_id', always=True)
|
@validator('account_id', always=True)
|
||||||
def account_id_lookup(cls, v, values, **kwargs):
|
def account_id_lookup(cls, v, values, **kwargs):
|
||||||
log.setLevel(logging.WARNING)
|
if isinstance(v, int) and v > 0: return v
|
||||||
log.debug(locals())
|
elif id_random := values.get('account_id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='account')
|
||||||
if values['account_id_random']:
|
|
||||||
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@validator('person_id', always=True)
|
@validator('person_id', always=True)
|
||||||
def person_id_lookup(cls, v, values, **kwargs):
|
def person_id_lookup(cls, v, values, **kwargs):
|
||||||
log.setLevel(logging.WARNING)
|
if isinstance(v, int) and v > 0: return v
|
||||||
log.debug(locals())
|
elif id_random := values.get('person_id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='person')
|
||||||
if values.get('person_id_random'):
|
|
||||||
return redis_lookup_id_random(record_id_random=values['person_id_random'], table_name='person')
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@validator('user_id', always=True)
|
@validator('user_id', always=True)
|
||||||
def user_id_lookup(cls, v, values, **kwargs):
|
def user_id_lookup(cls, v, values, **kwargs):
|
||||||
log.setLevel(logging.WARNING)
|
if isinstance(v, int) and v > 0: return v
|
||||||
log.debug(locals())
|
elif id_random := values.get('user_id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='user')
|
||||||
if values.get('user_id_random'):
|
|
||||||
return redis_lookup_id_random(record_id_random=values['user_id_random'], table_name='user')
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ async def get_obj_li(
|
|||||||
|
|
||||||
commons: Common_Route_Params = Depends(common_route_params),
|
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())
|
log.debug(locals())
|
||||||
|
|
||||||
if order_by_li:
|
if order_by_li:
|
||||||
@@ -231,8 +231,9 @@ async def get_obj_li(
|
|||||||
|
|
||||||
if for_obj_type and for_obj_id:
|
if for_obj_type and for_obj_id:
|
||||||
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type)
|
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type)
|
||||||
#data = {}
|
log.debug(f'for_obj_type: {for_obj_type}')
|
||||||
#data[f'{for_obj_type}_id'] = for_obj_id
|
log.debug(f'for_obj_id: {for_obj_id}')
|
||||||
|
|
||||||
field_name = f'{for_obj_type}_id'
|
field_name = f'{for_obj_type}_id'
|
||||||
|
|
||||||
# NOTE: The enabled and hidden parameters are new to this endpoint and the sql_select function! -2023-07-06
|
# NOTE: The enabled and hidden parameters are new to this endpoint and the sql_select function! -2023-07-06
|
||||||
@@ -614,7 +615,7 @@ async def post_obj(
|
|||||||
|
|
||||||
# NOTE: Add a check for the object ID... assuming it is a random ID string for now. Using rm_id_random. That helps with some field names.
|
# NOTE: Add a check for the object ID... assuming it is a random ID string for now. Using rm_id_random. That helps with some field names.
|
||||||
if sql_result := sql_insert(data=crud_data, table_name=table_name, rm_id_random=True, log_lvl=logging.INFO):
|
if sql_result := sql_insert(data=crud_data, table_name=table_name, rm_id_random=True, log_lvl=logging.INFO):
|
||||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.info('The record was inserted.')
|
log.info('The record was inserted.')
|
||||||
log.debug(sql_result)
|
log.debug(sql_result)
|
||||||
|
|
||||||
@@ -640,7 +641,8 @@ async def post_obj(
|
|||||||
return mk_resp(data=resp_data, response=commons.response)
|
return mk_resp(data=resp_data, response=commons.response)
|
||||||
else:
|
else:
|
||||||
log.debug(sql_select_result)
|
log.debug(sql_select_result)
|
||||||
return mk_resp(data=False, status_code=404, response=commons.response)
|
status_message = 'The record was not found. This may be because a SQL VIEW was used for the SQL SELECT query?'
|
||||||
|
return mk_resp(data=False, status_code=404, response=commons.response, status_message=status_message)
|
||||||
log.info('Returning IDs only created from POST data')
|
log.info('Returning IDs only created from POST data')
|
||||||
return mk_resp(data=resp_data, response=commons.response) #, details=debug_data)
|
return mk_resp(data=resp_data, response=commons.response) #, details=debug_data)
|
||||||
elif sql_result == None:
|
elif sql_result == None:
|
||||||
|
|||||||
Reference in New Issue
Block a user