refactor(sql): complete modularization of search builders and ID resolution
This commit is contained in:
@@ -87,9 +87,9 @@ def redis_lookup_id_random(
|
||||
log.debug(f'SQL: SELECT result: {select_results}')
|
||||
if isinstance(select_results, dict):
|
||||
log.info(f"""SQL: Found ID Random for: {str(record_id_random)} = {str(select_results.get('id'))}""")
|
||||
if rid := select_results.get('id'):
|
||||
r.setex(key_name, datetime.timedelta(minutes=minutes), value=rid)
|
||||
return int(rid)
|
||||
if record_id := select_results.get('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. The ID field was not found.')
|
||||
return False
|
||||
@@ -141,4 +141,87 @@ def reset_redis():
|
||||
"""Flushes the Redis database used for ID caching."""
|
||||
r = redis.Redis(host=settings.REDIS['server'], port=settings.REDIS['port'], db=7, password=None, decode_responses=True)
|
||||
r.flushdb()
|
||||
return True
|
||||
return True
|
||||
|
||||
|
||||
def lookup_id_random_pop(
|
||||
obj_data: dict,
|
||||
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
):
|
||||
"""
|
||||
Look up and resolve id_random values to their id
|
||||
Remove the unneeded *_id_random key from the dict
|
||||
"""
|
||||
log.setLevel(log_lvl)
|
||||
|
||||
# Common prefixes for ID resolution
|
||||
id_prefixes = [
|
||||
'account', 'activity_log', 'address', 'address_location', 'archive',
|
||||
'contact', 'contact_1', 'contact_2', 'cont_edu_cert', 'cont_edu_cert_person',
|
||||
'event', 'event_id_random_only', 'event_abstract', 'event_badge',
|
||||
'event_badge_template', 'event_exhibit', 'event_file', 'event_location',
|
||||
'event_person', 'event_person_profile', 'event_presentation',
|
||||
'event_presenter', 'event_registration', 'event_session', 'event_track',
|
||||
'grant', 'hosted_file', 'journal', 'journal_entry', 'membership_group',
|
||||
'membership_person_group', 'membership_person', 'membership_type',
|
||||
'membership_person_type', 'order', 'order_line', 'order_cart',
|
||||
'order_cart_line', 'organization', 'page', 'person', 'poc_event_person',
|
||||
'poc_person', 'post', 'product', 'sponsorship', 'sponsorship_cfg',
|
||||
'site', 'user'
|
||||
]
|
||||
|
||||
for prefix in id_prefixes:
|
||||
key = f'{prefix}_id_random'
|
||||
if key in obj_data:
|
||||
# Table name mapping
|
||||
table = prefix
|
||||
if prefix == 'address_location': table = 'address'
|
||||
elif prefix in ['contact_1', 'contact_2']: table = 'contact'
|
||||
elif prefix == 'event_id_random_only': table = 'event'
|
||||
elif prefix == 'poc_event_person': table = 'event_person'
|
||||
elif prefix == 'poc_person': table = 'person'
|
||||
|
||||
resolved_id = redis_lookup_id_random(record_id_random=obj_data[key], table_name=table)
|
||||
obj_data[f'{prefix if not prefix.endswith("_id_random_only") else prefix[:-15]+"_id_only"}'] = resolved_id
|
||||
# Special case for event_id_random_only
|
||||
target_id_key = f'{prefix[:-7]}' if prefix.endswith('_random') else f'{prefix}_id'
|
||||
if prefix == 'event_id_random_only': target_id_key = 'event_id_only'
|
||||
|
||||
obj_data[target_id_key] = resolved_id
|
||||
obj_data.pop(key)
|
||||
|
||||
# Polymorphic links
|
||||
polymorphic = [
|
||||
('for_type', 'for_id_random', 'for_id'),
|
||||
('link_to_type', 'link_to_id_random', 'link_to_id'),
|
||||
('object_type', 'object_id_random', 'object_id'),
|
||||
('to_object_type', 'to_object_id_random', 'to_object_id'),
|
||||
('from_object_type', 'from_object_id_random', 'from_object_id')
|
||||
]
|
||||
|
||||
for type_key, rand_key, id_key in polymorphic:
|
||||
if type_key in obj_data and rand_key in obj_data:
|
||||
obj_data[id_key] = redis_lookup_id_random(
|
||||
record_id_random=obj_data.get(rand_key),
|
||||
table_name=obj_data.get(type_key)
|
||||
)
|
||||
obj_data.pop(rand_key)
|
||||
|
||||
return obj_data
|
||||
|
||||
|
||||
def get_account_id_w_for_type_id(
|
||||
for_type: str, # This is the table name
|
||||
for_id: int|str,
|
||||
) -> bool|int|None:
|
||||
"""Helper to find an account_id associated with an object."""
|
||||
from app.db_sql import sql_select
|
||||
log.setLevel(logging.WARNING)
|
||||
|
||||
if fid := redis_lookup_id_random(record_id_random=for_id, table_name=for_type):
|
||||
data = {'for_id': fid}
|
||||
sql = f"SELECT account_id FROM `{for_type}` WHERE id = :for_id LIMIT 1;"
|
||||
if result := sql_select(data=data, sql=sql):
|
||||
return result.get('account_id')
|
||||
return False
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user