Update to Redis function for looking up id_random values. Updates related to IDAA and posts

This commit is contained in:
Scott Idem
2023-11-15 10:29:33 -05:00
parent 070c75a8c6
commit 02372a6684
3 changed files with 41 additions and 41 deletions

View File

@@ -1056,12 +1056,14 @@ def sql_delete(
# If success then return the ID number
# 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
# Updated 2023-11-15
@logger_reset
def redis_lookup_id_random(
record_id_random: int|str,
table_name: str,
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:
log.setLevel(log_lvl)
@@ -1086,40 +1088,42 @@ def redis_lookup_id_random(
log.info(f'No record ID was passed. Returning None')
return None
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
if record_id_random and table_name:
# WARNING: The record_id_random string length should be checked just in case?
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
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
else:
pass
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
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
else:
log.warning('Missing table_name and record_id_random')
log.error('Missing table_name and record_id_random')
return False
# 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)
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)
log.debug(f'Record ID? {str(record_id)}')
log.debug(f'Record ID found: {record_id}')
if record_id:
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')
# log.info('The record ID was found using the record_id_random value.')
# 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)
elif table_name:
@@ -1131,27 +1135,27 @@ def redis_lookup_id_random(
"""
if select_results := sql_select(sql=sql, data=data):
log.debug(select_results)
log.debug(type(select_results))
log.debug(f'SQL SELECT result: {select_results}')
# log.debug(type(select_results))
if isinstance(select_results, dict):
log.info(f"""Record ID random found: {str(select_results['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)
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
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)
return False
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
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() ###

View File

@@ -17,11 +17,11 @@ class Post_Base(BaseModel):
log.debug(locals())
id_random: Optional[str] = Field(
**base_fields['post_id_random'],
# **base_fields['post_id_random'],
alias = 'post_id_random',
)
id: Optional[int] = Field(
#alias = 'post_id'
alias = 'post_id'
)
account_id_random: Optional[str]
account_id: Optional[int]
@@ -105,29 +105,23 @@ class Post_Base(BaseModel):
@validator('account_id', always=True)
def account_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values['account_id_random']:
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
if isinstance(v, int) and v > 0: return v
elif id_random := values.get('account_id_random'):
return redis_lookup_id_random(record_id_random=id_random, table_name='account')
return None
@validator('person_id', always=True)
def person_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values.get('person_id_random'):
return redis_lookup_id_random(record_id_random=values['person_id_random'], table_name='person')
if isinstance(v, int) and v > 0: return v
elif id_random := values.get('person_id_random'):
return redis_lookup_id_random(record_id_random=id_random, table_name='person')
return None
@validator('user_id', always=True)
def user_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values.get('user_id_random'):
return redis_lookup_id_random(record_id_random=values['user_id_random'], table_name='user')
if isinstance(v, int) and v > 0: return v
elif id_random := values.get('user_id_random'):
return redis_lookup_id_random(record_id_random=id_random, table_name='user')
return None
class Config:

View File

@@ -188,7 +188,7 @@ async def get_obj_li(
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())
if order_by_li:
@@ -231,10 +231,11 @@ async def get_obj_li(
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)
#data = {}
#data[f'{for_obj_type}_id'] = for_obj_id
field_name = f'{for_obj_type}_id'
log.debug(f'for_obj_type: {for_obj_type}')
log.debug(f'for_obj_id: {for_obj_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
sql_result = sql_select(table_name=table_name, field_name=field_name, field_value=for_obj_id, enabled=commons.enabled, hidden=hidden, order_by_li=order_by_li, limit=commons.limit, offset=commons.offset, as_list=True)
else:
@@ -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.
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.debug(sql_result)
@@ -640,7 +641,8 @@ async def post_obj(
return mk_resp(data=resp_data, response=commons.response)
else:
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')
return mk_resp(data=resp_data, response=commons.response) #, details=debug_data)
elif sql_result == None: