Work on better way to update objects. A lot of work! Also a lot of clean up.

This commit is contained in:
Scott Idem
2021-06-10 17:42:31 -04:00
parent 717db418f9
commit 4e6fedcffd
15 changed files with 253 additions and 71 deletions

View File

@@ -7,9 +7,181 @@ from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, v
from app.db_sql import redis_lookup_id_random, sql_select
from app.lib_general import log, logging
from app.methods.event_methods import load_event_obj
from app.models.account_models import Account_Base
from app.models.account_cfg_models import Account_Cfg_Base
from app.models.membership_models import Membership_Cfg_Base
# ### BEGIN ### API Account Methods ### load_account_obj() ###
def load_account_obj(
account_id: int|str,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1000,
inc_address: bool = False, # Under contact
inc_address_list: bool = False,
inc_archive: bool = False,
inc_archive_list: bool = False,
inc_contact: bool = False,
inc_contact_list: bool = False,
inc_event: bool = False,
inc_event_list: bool = False,
inc_event_abstract: bool = False,
inc_event_abstract_list: bool = False,
inc_event_badge: bool = False,
inc_event_badge_list: bool = False,
inc_event_cfg: bool = False,
inc_event_device: bool = False,
inc_event_device_list: bool = False,
inc_event_exhibit: bool = False,
inc_event_exhibit_list: bool = False,
inc_event_file: bool = False,
inc_event_file_list: bool = False,
inc_event_location: bool = False, # For event_session child object
inc_event_location_list: bool = False,
inc_event_person: bool = False,
inc_event_person_list: bool = False,
inc_event_presentation: bool = False,
inc_event_presentation_list: bool = False,
inc_event_presenter_cat: bool = False, # For event_session child object
inc_event_presenter: bool = False,
inc_event_presenter_list: bool = False,
inc_event_registration: bool = False,
inc_event_registration_cfg: bool = False,
inc_event_registration_list: bool = False,
inc_event_session: bool = False,
inc_event_session_list: bool = False,
inc_event_track: bool = False, # For event_session child object
inc_event_track_list: bool = False,
inc_order: bool = False,
inc_order_list: bool = False,
inc_order_cart: bool = False,
inc_order_cart_list: bool = False,
inc_organization: bool = False,
inc_person: bool = False,
inc_user: bool = False
) -> Account_Base|bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
else: return False
if account_rec := sql_select(table_name='v_account', record_id=account_id): pass
else: return False
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(account_rec)
try:
account_obj = Account_Base(**account_rec)
log.debug(account_obj)
except ValidationError as e:
log.error(e.json())
# if inc_contact:
# contact_id = account_rec.get('contact_id', None)
# if contact_obj_result := load_contact_obj(contact_id=contact_id):
# contact_obj = contact_obj_result
# #account_obj.contact = contact_obj.dict(by_alias=True, exclude_unset=True)
# account_obj.contact = contact_obj
# else: account_obj.contact = None
# if inc_address:
# address_id = contact_obj.address_id
# if address_obj_result := load_address_obj(address_id=address_id):
# address_obj = address_obj_result
# # account_rec['contact'].address = address_obj
# # log.debug(account_rec)
# #account_obj.contact.address = address_obj.dict(by_alias=True, exclude_unset=True)
# account_obj.contact.address = address_obj
# else: account_obj.contact.address = None
# if inc_organization:
# organization_id = account_rec.get('organization_id', None)
# if organization_obj_result := load_organization_obj(organization_id=organization_id):
# organization_obj = organization_obj_result
# # account_rec['organization'] = organization_obj
# # log.debug(account_rec)
# #account_obj.organization = organization_obj.dict(by_alias=True, exclude_unset=True)
# account_obj.organization = organization_obj
# else: account_obj.organization = None
if inc_event_list:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
data = {}
data['account_id'] = account_id
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
data['enable'] = True
sql_enabled = f'AND `event`.enable = :enable'
elif enabled == 'disabled':
data['enable'] = False
sql_enabled = f'AND `event`.enable = :enable'
elif enabled == 'all':
sql_enabled = ''
# else: event_obj['account'] = None
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql = f"""
SELECT `event`.id AS 'event_id', `event`.id_random AS 'event_id_random'
FROM `event` AS `event`
WHERE `event`.account_id = :account_id
{sql_enabled}
ORDER BY `event`.created_on DESC, `event`.updated_on DESC
{sql_limit};
"""
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if event_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(event_rec_li_result)
event_obj_li = []
for event_rec in event_rec_li_result:
event_id = event_rec.get('event_id', None)
if event_obj := load_event_obj(
event_id=event_id,
enabled=enabled,
inc_address=inc_address,
inc_contact=inc_contact,
inc_event_abstract_list=inc_event_abstract_list,
inc_event_badge_list=inc_event_badge_list,
inc_event_device_list=inc_event_device_list,
inc_event_exhibit_list=inc_event_exhibit_list,
inc_event_file_list=inc_event_file_list,
inc_event_location_list=inc_event_location_list,
inc_event_person=inc_event_person,
inc_event_person_list=inc_event_person_list,
inc_event_presentation_list=inc_event_presentation_list,
inc_event_presenter_list=inc_event_presenter_list,
inc_event_registration_list=inc_event_registration_list,
inc_event_session_list=inc_event_session_list,
inc_event_track_list=inc_event_track_list,
inc_person=inc_person,
inc_user=inc_user
):
data = event_obj.dict(by_alias=True, exclude_unset=True)
event_obj_li.append(data)
log.debug(event_obj_li)
account_obj.event_list = event_obj_li
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(event_rec_li_result)
account_obj.event_list = []
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
return account_obj
# ### END ### API Account Methods ### load_account_obj() ###
# ### BEGIN ### API Account Methods ### load_account_cfg_obj() ###
def load_account_cfg_obj(account_id:int|str, inc_event_cfg:bool=False, inc_fundraising_cfg:bool=False, inc_membership_cfg:bool=False):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL