473 lines
20 KiB
Python
473 lines
20 KiB
Python
from __future__ import annotations
|
|
import datetime
|
|
|
|
from typing import Dict, List, Optional, Set, Union
|
|
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
|
|
|
from app.db_sql import redis_lookup_id_random, sql_select
|
|
from app.lib_general import log, logging
|
|
|
|
from app.methods.account_cfg_methods import load_account_cfg_obj
|
|
from app.methods.address_methods import get_address_rec_list, load_address_obj
|
|
from app.methods.archive_methods import get_archive_rec_list, load_archive_obj
|
|
from app.methods.contact_methods import get_contact_rec_list, load_contact_obj
|
|
from app.methods.event_methods import get_event_rec_list, load_event_obj
|
|
from app.methods.hosted_file_methods import get_hosted_file_rec_list, load_hosted_file_obj
|
|
from app.methods.journal_methods import get_journal_rec_list, load_journal_obj
|
|
from app.methods.order_methods import get_order_rec_list, load_order_obj
|
|
# from app.methods.order_cart_methods import get_order_cart_rec_list, load_order_cart_obj
|
|
from app.methods.organization_methods import get_organization_rec_list, load_organization_obj
|
|
from app.methods.person_methods import get_person_rec_list, load_person_obj
|
|
from app.methods.product_methods import get_product_rec_list, load_product_obj
|
|
from app.methods.post_methods import get_post_rec_list, load_post_obj
|
|
from app.methods.site_methods import get_site_rec_list, load_site_obj
|
|
from app.methods.user_methods import get_user_rec_list, load_user_obj
|
|
|
|
from app.models.account_models import Account_Base
|
|
from app.models.account_cfg_models import Account_Cfg_Base
|
|
# from app.models.membership_cfg_models import Membership_Cfg_Base
|
|
|
|
|
|
# ### BEGIN ### API Account Methods ### load_account_obj() ###
|
|
# Working well as of 2021-06-11. Using as a template for other load objects.
|
|
def load_account_obj(
|
|
account_id: int|str,
|
|
limit: int = 1000,
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True,
|
|
model_as_dict: bool = False,
|
|
enabled: str = 'enabled', # enabled, disabled, all
|
|
inc_account_cfg: bool = False, # Priority l1
|
|
inc_address: bool = False, # Under contact
|
|
inc_address_list: bool = False, # Priority l3
|
|
inc_archive: bool = False,
|
|
inc_archive_list: bool = False, # Priority l1
|
|
inc_archive_content: bool = False,
|
|
inc_archive_content_list: bool = False, # Priority l2
|
|
inc_contact: bool = False,
|
|
inc_contact_list: bool = False, # Priority l3
|
|
inc_event: bool = False,
|
|
inc_event_list: bool = False, # Priority l1
|
|
# 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_fundraising_cfg: bool = False,
|
|
inc_hosted_file_list: bool = False,
|
|
inc_hosted_file_link_list: bool = False,
|
|
inc_journal_list: bool = False, # Priority l3
|
|
inc_journal_entry_list: bool = False, # Priority l3
|
|
# inc_membership: bool = False,
|
|
inc_membership_cfg: bool = False,
|
|
inc_membership_list: bool = False,
|
|
inc_membership_member: bool = False,
|
|
inc_membership_member_list: bool = False, # Priority l1
|
|
inc_order: bool = False,
|
|
inc_order_cfg: bool = False,
|
|
inc_order_list: bool = False, # Priority l1
|
|
inc_order_line_list: bool = False, # Priority l2
|
|
inc_order_cart: bool = False,
|
|
inc_order_cart_line_list: bool = False, # Priority l2
|
|
inc_order_cart_list: bool = False,
|
|
inc_organization: bool = False,
|
|
inc_organization_list: bool = False, # Priority l3
|
|
# inc_page: bool = False,
|
|
inc_page_list: bool = False, # Priority l3
|
|
inc_person: bool = False,
|
|
inc_person_list: bool = False, # Priority l2
|
|
inc_post: bool = False,
|
|
inc_post_list: bool = False, # Priority l1
|
|
inc_post_comment: bool = False,
|
|
inc_post_comment_list: bool = False,
|
|
inc_product: bool = False,
|
|
inc_product_list: bool = False, # Priority l3
|
|
# inc_site: bool = False,
|
|
inc_site_list: bool = False, # Priority l3
|
|
inc_site_domain_list: bool = False, # Priority l3
|
|
inc_user: bool = False,
|
|
inc_user_list: bool = False, # Priority l2
|
|
inc_user_role_list: bool = False,
|
|
) -> Account_Base|dict|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_account_cfg:
|
|
if account_cfg_dict := load_account_cfg_obj(
|
|
account_id = account_id,
|
|
model_as_dict = model_as_dict,
|
|
# inc_event_cfg = inc_event_cfg,
|
|
inc_fundraising_cfg = inc_fundraising_cfg,
|
|
inc_membership_cfg = inc_membership_cfg,
|
|
):
|
|
account_obj.account_cfg = account_cfg_dict
|
|
else: account_obj.account_cfg = None
|
|
|
|
# Updated 2021-06-17
|
|
if inc_address_list:
|
|
if address_rec_list_result := get_address_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
address_dict_list = []
|
|
for address_rec in address_rec_list_result:
|
|
address_dict_list.append(
|
|
load_address_obj(
|
|
address_id = address_rec.get('address_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
)
|
|
)
|
|
account_obj.address_list = address_dict_list
|
|
else: account_obj.address_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_archive_list:
|
|
if archive_rec_list_result := get_archive_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
archive_dict_list = []
|
|
for archive_rec in archive_rec_list_result:
|
|
archive_dict_list.append(
|
|
load_archive_obj(
|
|
archive_id = archive_rec.get('archive_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_archive_content_list = inc_archive_content_list,
|
|
)
|
|
)
|
|
account_obj.archive_list = archive_dict_list
|
|
else: account_obj.archive_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_contact_list:
|
|
if contact_rec_list_result := get_contact_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
contact_dict_list = []
|
|
for contact_rec in contact_rec_list_result:
|
|
contact_dict_list.append(
|
|
load_contact_obj(
|
|
contact_id = contact_rec.get('contact_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_address = inc_address,
|
|
)
|
|
)
|
|
account_obj.contact_list = contact_dict_list
|
|
else: account_obj.contact_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_event_list:
|
|
if event_rec_list_result := get_event_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
event_dict_list = []
|
|
for event_rec in event_rec_list_result:
|
|
event_dict_list.append(
|
|
load_event_obj(
|
|
event_id = event_rec.get('event_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
# inc_location_address = inc_address,
|
|
# inc_contact_1 = inc_contact,
|
|
# inc_contact_2 = inc_contact,
|
|
# inc_contact_3 = 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_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,
|
|
)
|
|
)
|
|
account_obj.event_list = event_dict_list
|
|
else: account_obj.event_list = []
|
|
|
|
# Updated 2021-06-18
|
|
if inc_hosted_file_list:
|
|
if hosted_file_rec_list_result := get_hosted_file_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
hosted_file_dict_list = []
|
|
for hosted_file_rec in hosted_file_rec_list_result:
|
|
hosted_file_dict_list.append(
|
|
load_hosted_file_obj(
|
|
hosted_file_id = hosted_file_rec.get('hosted_file_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_hosted_file_link_list = inc_hosted_file_link_list,
|
|
)
|
|
)
|
|
account_obj.hosted_file_list = hosted_file_dict_list
|
|
else: account_obj.hosted_file_list = []
|
|
|
|
# Updated 2021-06-18
|
|
if inc_journal_list:
|
|
if journal_rec_list_result := get_journal_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
journal_dict_list = []
|
|
for journal_rec in journal_rec_list_result:
|
|
journal_dict_list.append(
|
|
load_journal_obj(
|
|
journal_id = journal_rec.get('journal_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_journal_entry_list = inc_journal_entry_list,
|
|
)
|
|
)
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(journal_dict_list)
|
|
|
|
|
|
account_obj.journal_list = journal_dict_list
|
|
else: account_obj.journal_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_order_list:
|
|
if order_rec_list_result := get_order_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
order_dict_list = []
|
|
for order_rec in order_rec_list_result:
|
|
order_dict_list.append(
|
|
load_order_obj(
|
|
order_id = order_rec.get('order_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_order_line_list = inc_order_line_list,
|
|
inc_order_cfg = inc_order_cfg,
|
|
inc_person = inc_person,
|
|
inc_user = inc_user,
|
|
)
|
|
)
|
|
account_obj.order_list = order_dict_list
|
|
else: account_obj.order_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_organization_list:
|
|
if organization_rec_list_result := get_organization_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
organization_dict_list = []
|
|
for organization_rec in organization_rec_list_result:
|
|
organization_dict_list.append(
|
|
load_organization_obj(
|
|
organization_id = organization_rec.get('organization_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_person = inc_person,
|
|
inc_user = inc_user,
|
|
)
|
|
)
|
|
account_obj.organization_list = organization_dict_list
|
|
else: account_obj.organization_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_post_list:
|
|
if post_rec_list_result := get_post_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
post_dict_list = []
|
|
for post_rec in post_rec_list_result:
|
|
post_dict_list.append(
|
|
load_post_obj(
|
|
post_id = post_rec.get('post_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_post_comment_list = inc_post_comment_list,
|
|
inc_person = inc_person,
|
|
inc_user = inc_user,
|
|
)
|
|
)
|
|
account_obj.post_list = post_dict_list
|
|
else: account_obj.post_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_person_list:
|
|
if person_rec_list_result := get_person_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
person_dict_list = []
|
|
for person_rec in person_rec_list_result:
|
|
person_dict_list.append(
|
|
load_person_obj(
|
|
person_id = person_rec.get('person_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_organization = inc_organization,
|
|
inc_user = inc_user,
|
|
)
|
|
)
|
|
account_obj.person_list = person_dict_list
|
|
else: account_obj.person_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_product_list:
|
|
if product_rec_list_result := get_product_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
product_dict_list = []
|
|
for product_rec in product_rec_list_result:
|
|
product_dict_list.append(
|
|
load_product_obj(
|
|
product_id = product_rec.get('product_id', None),
|
|
model_as_dict = model_as_dict,
|
|
)
|
|
)
|
|
account_obj.product_list = product_dict_list
|
|
else: account_obj.product_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_site_list:
|
|
if site_rec_list_result := get_site_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
site_result_list = []
|
|
for site_rec in site_rec_list_result:
|
|
site_result_list.append(
|
|
load_site_obj(
|
|
site_id = site_rec.get('site_id', None),
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
model_as_dict = model_as_dict,
|
|
inc_site_domain_list = inc_site_domain_list,
|
|
)
|
|
)
|
|
account_obj.site_list = site_result_list
|
|
else: account_obj.site_list = []
|
|
|
|
# Updated 2021-06-17
|
|
if inc_user_list:
|
|
if user_rec_list_result := get_user_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
user_dict_list = []
|
|
for user_rec in user_rec_list_result:
|
|
user_dict_list.append(
|
|
load_user_obj(
|
|
user_id = user_rec.get('user_id', None),
|
|
limit = limit,
|
|
model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_event_list = inc_event_list,
|
|
inc_journal_list = inc_journal_list,
|
|
inc_membership_member = inc_membership_member,
|
|
inc_order_list = inc_order_list,
|
|
inc_order_cart_list = inc_order_cart_list,
|
|
inc_organization = inc_organization,
|
|
inc_person = inc_person,
|
|
inc_post_list = inc_post_list,
|
|
inc_post_comment_list = inc_post_comment_list,
|
|
inc_user_role_list = inc_user_role_list,
|
|
)
|
|
)
|
|
account_obj.user_list = user_dict_list
|
|
else: account_obj.user_list = []
|
|
|
|
if model_as_dict:
|
|
return account_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
|
else:
|
|
return account_obj
|
|
# ### END ### API Account Methods ### load_account_obj() ###
|