Files
OSIT-AE-API-FastAPI/app/routers/account.py

387 lines
16 KiB
Python

import datetime
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from app.lib_general import log, logging, common_route_params, Common_Route_Params
from app.config import settings
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, redis_lookup_id_random
from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
from app.methods.account_methods import get_account_rec_list, load_account_obj
from app.methods.account_cfg_methods import load_account_cfg_obj
# from app.methods.event_methods import get_event_rec_list, load_event_obj
# from app.methods.membership_group_methods import get_membership_group_rec_list, load_membership_group_obj
# from app.methods.membership_person_methods import get_membership_person_rec_list, load_membership_person_obj
# from app.methods.membership_type_methods import get_membership_type_rec_list, load_membership_type_obj
# from app.methods.post_methods import get_post_rec_list, load_post_obj
from app.models.account_models import Account_Base
from app.models.response_models import Resp_Body_Base, mk_resp
router = APIRouter()
@router.post('/account', response_model=Resp_Body_Base)
async def post_account_obj(
obj: Account_Base,
return_obj: Optional[bool] = True,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'account'
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
result = post_obj_template(
obj_type=obj_type,
data=obj_data_dict,
return_obj=True,
by_alias=True,
exclude_unset=True,
)
return result
@router.patch('/account/{obj_id}', response_model=Resp_Body_Base)
async def patch_account_obj(
obj: Account_Base,
obj_id: str = Query(..., min_length=11, max_length=22),
return_obj: Optional[bool] = True,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'account'
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type)
obj_data_dict['id_random'] = obj_id
result = patch_obj_template(
obj_type=obj_type,
data=obj_data_dict,
obj_id=obj_id,
return_obj=True,
by_alias=True,
exclude_unset=True,
)
return result
# ### BEGIN ### API Account ### get_account_obj_li() ###
# Updated 2021-09-28
@router.get('/account/list', response_model=Resp_Body_Base)
async def get_account_obj_li(
# inc_address: bool = False,
# inc_address_list: bool = False,
inc_archive_list: bool = False,
# inc_contact: bool = False,
# inc_contact_list: bool = False,
inc_event_list: bool = False,
inc_hosted_file_list: bool = False,
inc_journal_list: bool = False,
inc_membership_person_list: bool = False,
inc_order_list: bool = False,
inc_organization_list: bool = False,
inc_page_list: bool = False,
inc_person_list: bool = False,
inc_post_list: bool = False,
inc_product_list: bool = False,
inc_site_list: bool = False,
inc_site_domain_list: bool = False,
inc_user_list: bool = False,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# Updated 2021-09-28
if account_rec_list_result := get_account_rec_list(
limit = commons.limit,
enabled = commons.enabled,
):
account_result_list = []
for account_rec in account_rec_list_result:
if load_account_result := load_account_obj(
account_id = account_rec.get('account_id', None),
limit = commons.limit,
by_alias = commons.by_alias,
exclude_unset = commons.exclude_unset,
# model_as_dict = model_as_dict,
enabled = commons.enabled,
# inc_address = inc_address,
# inc_address_list = inc_address_list,
# inc_archive_list = inc_archive_list,
# inc_contact = inc_contact,
# inc_contact_list = inc_contact_list,
inc_event_list = inc_event_list,
# inc_hosted_file_list = inc_hosted_file_list,
# inc_journal_list = inc_journal_list,
inc_membership_person_list = inc_membership_person_list,
# inc_order_list = inc_order_list,
# inc_organization_list = inc_organization_list,
inc_page_list = inc_page_list,
inc_person_list = inc_person_list,
inc_post_list = inc_post_list,
# inc_product_list = inc_product_list,
inc_site_list = inc_site_list,
inc_site_domain_list = inc_site_domain_list,
inc_user_list = inc_user_list,
):
account_result_list.append(load_account_result)
else:
account_result_list.append(None)
response_data = account_result_list
else:
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data, response=commons.response)
# ### BEGIN ### API Account ### get_account_obj() ###
# Working well as of 2021-06-11. Using as a template for other routes.
@router.get('/account/{account_id}', response_model=Resp_Body_Base)
async def get_account_obj(
account_id: str = Query(..., min_length=11, max_length=22),
inc_account_cfg: bool = False, # Priority l1
inc_address: bool = False, # Under contact
inc_address_list: bool = False,
# 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,
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,
inc_journal_entry_list: bool = False,
inc_membership_cfg: bool = False,
# inc_membership_group: bool = False,
inc_membership_group_list: bool = False,
inc_membership_person: bool = False,
inc_membership_person_list: bool = False,
inc_membership_person_profile: bool = False,
# inc_membership_type: bool = False,
inc_membership_type_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_organization_list: bool = False,
inc_person: bool = False,
inc_person_list: bool = False,
# 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,
# inc_site: bool = False,
inc_site_list: bool = False,
inc_site_domain_list: bool = False,
inc_user: bool = False,
inc_user_list: bool = False,
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # 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 mk_resp(data=None, status_code=404, response=commons.response)
if account_result := load_account_obj(
account_id = account_id,
enabled = commons.enabled,
limit = commons.limit,
by_alias = commons.by_alias,
exclude_unset = commons.exclude_unset,
model_as_dict = False, # NOTE: returning model as a dict
inc_account_cfg = inc_account_cfg,
inc_address = inc_address,
inc_address_list = inc_address_list,
# inc_archive = inc_archive,
inc_archive_list = inc_archive_list,
# inc_archive_content = inc_archive_content,
inc_archive_content_list = inc_archive_content_list,
inc_contact = inc_contact,
inc_contact_list = inc_contact_list,
inc_event = inc_event,
inc_event_list = inc_event_list,
# 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_file_list = inc_event_file_list,
# inc_event_location = inc_event_location,
# 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_cat = inc_event_presenter_cat,
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 = inc_event_track,
inc_event_track_list = inc_event_track_list,
inc_fundraising_cfg = inc_fundraising_cfg,
inc_hosted_file_list = inc_hosted_file_list,
inc_hosted_file_link_list = inc_hosted_file_link_list,
inc_journal_list = inc_journal_list,
inc_journal_entry_list = inc_journal_entry_list,
inc_membership_cfg = inc_membership_cfg,
inc_membership_group_list = inc_membership_group_list,
inc_membership_person = inc_membership_person,
inc_membership_person_list = inc_membership_person_list,
inc_membership_person_profile = inc_membership_person_profile,
inc_membership_type_list = inc_membership_type_list,
# inc_order = inc_order,
inc_order_list = inc_order_list,
# inc_order_cart = inc_order_cart,
inc_order_cart_list = inc_order_cart_list,
inc_organization = inc_organization,
inc_organization_list = inc_organization_list,
inc_person = inc_person,
inc_person_list = inc_person_list,
# inc_post = inc_post,
inc_post_list = inc_post_list,
# inc_post_comment = inc_post_comment,
inc_post_comment_list = inc_post_comment_list,
# inc_product = inc_product,
inc_product_list = inc_product_list,
# inc_site = inc_site,
inc_site_list = inc_site_list,
inc_site_domain_list = inc_site_domain_list,
inc_user = inc_user,
inc_user_list = inc_user_list,
):
if isinstance(account_result, dict):
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.info('Result is a dict')
response_data = account_result
log.debug(response_data)
else:
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# print('---------------------------')
log.info('Result is probably an object model')
response_data = account_result
# print('---------------------------')
log.debug(response_data)
# log.debug(response_data.__fields_set__)
# response_data.testing = 'this is a test'
# log.debug(response_data.__fields_set__)
# print('---------------------------')
# log.debug(account_result.dict())
# print('---------------------------')
# # log.debug(account_result.dict(by_alias=False, exclude_unset=True)) # pylint: disable=no-member
# log.debug(account_result.dict(by_alias=True, exclude_unset=False)) # pylint: disable=no-member
# print('---------------------------')
# log.debug(account_result.dict(by_alias=True, exclude_unset=True)) # pylint: disable=no-member
# print('---------------------------')
else:
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
return mk_resp(data=response_data, response=commons.response)
# ### END ### API Account ### get_account_obj() ###
@router.delete('/account/{obj_id}', response_model=Resp_Body_Base)
async def delete_account_obj(
obj_id: str = Query(..., min_length=11, max_length=22),
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'account'
result = delete_obj_template(
obj_type=obj_type,
obj_id=obj_id,
)
return result
@router.get('/account/{account_id}/cfg', response_model=Resp_Body_Base)
async def get_account_cfg_obj(
account_id: str = Query(..., min_length=11, max_length=22),
sys_module: Optional[str] = None, # event, fundraising, membership
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if account_id := redis_lookup_id_random(record_id_random=commons.x_account_id, table_name='account'): pass
else: return mk_resp(data=None, status_code=404, status_message=f'The Account ID was not found. Account ID: {account_id}', response=commons.response)
if sys_module:
if sys_module == 'membership':
membership_cfg_obj = load_account_cfg_obj(
account_id = account_id,
inc_membership_cfg = True
).dict(
by_alias = commons.by_alias,
exclude_unset = commons.exclude_unset
)
data = membership_cfg_obj
return mk_resp(data=data, response=commons.response)
else:
if account_cfg_obj := load_account_cfg_obj(
account_id = account_id
).dict(
by_alias = commons.by_alias,
exclude_unset = commons.exclude_unset
):
data = account_cfg_obj
return mk_resp(data=data, response=commons.response)
else:
return mk_resp(data=False, status_code=404, response=commons.response)
# obj_type = 'account_cfg'
# result = get_obj_template(
# obj_type=obj_type,
# obj_id=account_id,
# by_alias=True,
# exclude_unset=True,
# )
# #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(result)
# return result