Code clean up. Better debugging. Work on event session, presentation, and presenter.
This commit is contained in:
@@ -3,7 +3,7 @@ import datetime, pytz, redis, secrets
|
||||
from timeit import default_timer as timer
|
||||
|
||||
from app.config import settings
|
||||
from app.log import log, logging
|
||||
from app.log import log, logging, logger_reset
|
||||
|
||||
from sqlalchemy import create_engine, text, Time
|
||||
from sqlalchemy.exc import IntegrityError, OperationalError
|
||||
@@ -26,6 +26,7 @@ db = engine.connect()
|
||||
|
||||
# ### BEGIN ### Core Help CRUD ### sql_insert() ###
|
||||
# Updated 2021-09-07
|
||||
@logger_reset
|
||||
def sql_insert(
|
||||
sql: str|None = None,
|
||||
data: dict|None = None,
|
||||
@@ -33,7 +34,7 @@ def sql_insert(
|
||||
rm_id_random: bool = False,
|
||||
id_random_length: int = 8,
|
||||
):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if sql:
|
||||
@@ -157,6 +158,7 @@ def sql_insert(
|
||||
|
||||
# ### BEGIN ### Core Help CRUD ### sql_update() ###
|
||||
# Updated 2021-09-07
|
||||
@logger_reset
|
||||
def sql_update(
|
||||
sql: str|None = None,
|
||||
data: dict|None = None,
|
||||
@@ -166,7 +168,7 @@ def sql_update(
|
||||
rm_id_random: bool = False,
|
||||
id_random_length: None|int = None
|
||||
):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if sql:
|
||||
@@ -290,6 +292,7 @@ def sql_update(
|
||||
# The catch all SQL INSERT or UPDATE function - STI 2021-02-17
|
||||
# This one does it all for SQL INSERT and UPDATE queries
|
||||
# Updated 2021-09-07
|
||||
@logger_reset
|
||||
def sql_insert_or_update(
|
||||
sql: str|None = None,
|
||||
data: dict|None = None,
|
||||
@@ -297,7 +300,7 @@ def sql_insert_or_update(
|
||||
rm_id_random: bool = False,
|
||||
id_random_length: int|None = None,
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
#if sql: pass
|
||||
@@ -426,6 +429,7 @@ def sql_insert_or_update(
|
||||
# The catch all SQL SELECT function - STI 2021-02-17
|
||||
# This one does it all for SQL SELECT queries
|
||||
# Updated 2021-09-07
|
||||
@logger_reset
|
||||
def sql_select(
|
||||
table_name: str|None = None,
|
||||
record_id: int|None = None,
|
||||
@@ -439,7 +443,6 @@ def sql_select(
|
||||
as_list: bool|None = False,
|
||||
max_count: int = 100000,
|
||||
):
|
||||
current_log_level = log.level
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -625,13 +628,11 @@ def sql_select(
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(record_li)
|
||||
|
||||
log.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
return record_li # Successful
|
||||
else:
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(record)
|
||||
|
||||
log.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
return record # Successful
|
||||
elif result.rowcount > 1:
|
||||
log.info(f'Found {result.rowcount} records. as_dict={as_dict}, as_list={as_list}')
|
||||
@@ -651,13 +652,11 @@ def sql_select(
|
||||
record_li = result.fetchall()
|
||||
log.debug(record_li)
|
||||
|
||||
log.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
return record_li # Successful
|
||||
else:
|
||||
log.info('No records found. Returning None.')
|
||||
log.debug(result)
|
||||
|
||||
log.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
return None # Successful
|
||||
# ### END ### Core Help CRUD ### sql_select() ###
|
||||
|
||||
@@ -666,6 +665,7 @@ def sql_select(
|
||||
# The catch all SQL DELETE function - STI 2021-02-17
|
||||
# This one does it all for SQL DELETE queries
|
||||
# Updated 2021-09-07
|
||||
@logger_reset
|
||||
def sql_delete(
|
||||
table_name: str|None = None,
|
||||
record_id: int|None = None,
|
||||
@@ -675,7 +675,7 @@ def sql_delete(
|
||||
sql: str|None = None,
|
||||
data: dict|None = None
|
||||
):
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if table_name and (record_id or record_id_random) and not (field_name or field_value or sql or data):
|
||||
@@ -822,11 +822,11 @@ 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
|
||||
@logger_reset
|
||||
def redis_lookup_id_random(
|
||||
record_id_random: int|str,
|
||||
table_name: str,
|
||||
):
|
||||
current_log_level = log.level
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -834,7 +834,6 @@ def redis_lookup_id_random(
|
||||
elif isinstance(record_id_random, int): return record_id_random
|
||||
else:
|
||||
log.info(f'Unexpected data type or string format: {str(type(record_id_random))} Expected type is a string 11 or 22 characters long.')
|
||||
log.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
return False
|
||||
|
||||
if record_id_random and table_name:
|
||||
@@ -867,7 +866,7 @@ def redis_lookup_id_random(
|
||||
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.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
|
||||
return int(record_id)
|
||||
elif table_name:
|
||||
data = { 'id_random': record_id_random }
|
||||
@@ -878,30 +877,25 @@ def redis_lookup_id_random(
|
||||
"""
|
||||
|
||||
if select_results := sql_select(sql=sql, data=data):
|
||||
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(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)
|
||||
log.setLevel(current_log_level) # Reset log level; should be decorator
|
||||
|
||||
return int(record_id)
|
||||
else:
|
||||
# log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.error('The SQL result was not what was expected.')
|
||||
return False
|
||||
else:
|
||||
# log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.error('More than one record may have been found. There may be a duplicate id_random.')
|
||||
log.error(select_results)
|
||||
return False
|
||||
else:
|
||||
#log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.info('Record ID random was not found')
|
||||
return None
|
||||
|
||||
# log.setLevel(logging.ERROR) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.error('We should not be here. Something unexpected happened.')
|
||||
return False # Just in case
|
||||
# ### END ### API DB SQL ### redis_lookup_id_random() ###
|
||||
@@ -910,11 +904,12 @@ def redis_lookup_id_random(
|
||||
# ### BEGIN ### API DB SQL ### get_id_random() ###
|
||||
# Changed name from lookup_id_random() to get_id_random()
|
||||
# Updated 2021-08-23
|
||||
@logger_reset
|
||||
def get_id_random(
|
||||
record_id: int,
|
||||
table_name: str
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
data = { 'id': record_id }
|
||||
@@ -952,10 +947,11 @@ def get_id_random(
|
||||
# Look up and resolve id_random values to their id
|
||||
# Remove the unneeded *_id_random key from the dict
|
||||
# This really needs to be simplified... Use a list of dicts instead. Can store as JSON in the DB.
|
||||
@logger_reset
|
||||
def lookup_id_random_pop(
|
||||
obj_data: dict
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if 'account_id_random' in obj_data:
|
||||
@@ -1182,6 +1178,7 @@ def lookup_id_random_pop(
|
||||
|
||||
# ### BEGIN ### API DB SQL Methods ### get_account_id_w_for_type_id() ###
|
||||
# Updated 2021-08-24
|
||||
@logger_reset
|
||||
def get_account_id_w_for_type_id(
|
||||
for_type: str,
|
||||
for_id: int|str,
|
||||
|
||||
Reference in New Issue
Block a user