Files
OSIT-AE-API-FastAPI/app/routers/person.py
2021-07-15 18:10:27 -04:00

397 lines
16 KiB
Python

import datetime, pytz, time
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from app.lib_general import log, logging
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.membership_person_methods import load_membership_person_obj
from app.methods.order_methods import get_order_rec_list, load_order_obj
from app.methods.person_methods import create_update_person_obj, get_person_rec_list, load_person_obj, update_person_obj
from app.models.person_models import Person_Base
from app.models.response_models import Resp_Body_Base, mk_resp
router = APIRouter()
@router.post('/person', response_model=Resp_Body_Base)
async def post_person_obj(
obj: Person_Base,
x_account_id: str = Header(...),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'person'
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('/person/{obj_id}', response_model=Resp_Body_Base)
async def patch_person_obj(
obj: Person_Base,
obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'person'
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 Person ### post_person_json() ###
# Updated 2021-06-25
@router.post('/person/json', response_model=Resp_Body_Base)
async def post_person_json(
person_obj: Person_Base,
# person_id: str = Query(..., min_length=1, max_length=22),
# create_missing_obj: bool = False,
process_contact: bool = False,
process_organization: bool = False,
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
include: Optional[list] = [],
exclude: Optional[list] = [],
exclude_unset: Optional[bool] = True,
exclude_none: Optional[bool] = True,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
# else: return mk_resp(data=None, status_code=404)
if person_obj_in_result := create_update_person_obj(
person_id = None,
person_obj = person_obj,
process_contact = process_contact,
process_organization = process_organization,
):
log.debug(person_obj_in_result)
if return_obj:
person_obj = load_person_obj(person_id=person_obj_in_result)
person_dict = person_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
return mk_resp(data=person_dict)
else:
return mk_resp(data=person_obj_in_result)
else:
return mk_resp(data=False, status_code=400) # Bad Request
# ### END ### API Person ### post_person_json() ###
# ### BEGIN ### API Person ### patch_person_json() ###
# Updated 2021-06-25
@router.patch('/person/{person_id}/json', response_model=Resp_Body_Base)
async def patch_person_json(
person_obj: Person_Base,
person_id: str = Query(..., min_length=1, max_length=22),
process_contact: bool = False,
process_organization: bool = False,
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
include: Optional[list] = [],
exclude: Optional[list] = [],
exclude_unset: Optional[bool] = True,
exclude_none: Optional[bool] = True,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else: return mk_resp(data=None, status_code=404)
if person_obj_up_result := create_update_person_obj(
person_id = person_id,
person_obj = person_obj,
process_contact = process_contact,
process_organization = process_organization,
):
log.debug(person_obj_up_result)
if return_obj:
person_obj = load_person_obj(person_id=person_id)
person_dict = person_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
return mk_resp(data=person_dict)
else:
return mk_resp(data=person_obj_up_result)
else:
return mk_resp(data=False, status_code=400) # Bad Request
# ### END ### API Person ### patch_person_json() ###
@router.get('/person/list', response_model=Resp_Body_Base)
async def get_person_obj_li(
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'person'
result = get_obj_li_template(
obj_type=obj_type,
for_obj_type=for_obj_type,
for_obj_id=for_obj_id,
by_alias=True,
exclude_unset=True,
)
return result
# ### BEGIN ### API Person ### get_person_obj() ###
# Working well as of 2021-06-25. Using as a template for other routes.
@router.get('/person/{person_id}', response_model=Resp_Body_Base)
async def get_person_obj(
person_id: str = Query(..., min_length=1, max_length=22),
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
inc_address: bool = False, # Priority l1
# inc_archive_list: bool = False, # Priority l3
inc_contact: bool = False, # Priority l1
inc_event_list: bool = False, # Priority l1
# inc_hosted_file_list: bool = False, # Priority l3
inc_journal_list: bool = False, # Priority l2
# inc_journal_entry_list: bool = False, # Priority l3
# inc_membership_group: bool = False, # The primary membership group
# inc_membership_group_person: bool = False,
inc_membership_group_list: bool = False, # The list of all membership groups a person is a part of
inc_membership_group_person_list: bool = False,
inc_membership_person: bool = False, # Priority l2
inc_membership_person_profile: bool = False, # Priority l2
inc_membership_type: bool = False, # The primary membership type
inc_membership_type_person: bool = False,
# inc_membership_type_list: bool = False, # The list of all membership types a person is a part of
# inc_membership_type_person_list: bool = False,
inc_order_line_list: bool = False, # Priority l1
inc_order_list: bool = False, # Priority l1
inc_order_cart_list: bool = False, # Priority l1
inc_organization: bool = False, # Priority l1
# inc_organization_list: bool = False,
inc_post_list: bool = False, # Priority l2
inc_post_comment_list: bool = False, # Priority l3
inc_user: bool = False, # Priority l1
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else: return mk_resp(data=None, status_code=404)
if person_dict := load_person_obj(
person_id = person_id,
limit = limit,
model_as_dict = True, # NOTE: returning model as a dict
enabled = enabled,
inc_address = inc_address,
# inc_archive_list = inc_archive_list,
inc_contact = inc_contact,
inc_event_list = inc_event_list,
# inc_hosted_file_list = inc_hosted_file_list,
inc_journal_list = inc_journal_list,
# inc_journal_entry_list = inc_journal_entry_list,
inc_membership_group_list = inc_membership_group_list,
inc_membership_group_person_list = inc_membership_group_person_list,
inc_membership_person = inc_membership_person,
inc_membership_person_profile = inc_membership_person_profile,
inc_membership_type = inc_membership_type,
inc_membership_type_person = inc_membership_type_person,
inc_order_line_list = inc_order_line_list,
inc_order_list = inc_order_list,
inc_order_cart_list = inc_order_cart_list,
inc_organization = inc_organization,
# inc_organization_list = inc_organization_list,
inc_post_list = inc_post_list,
inc_post_comment_list = inc_post_comment_list,
inc_user = inc_user,
):
if isinstance(person_dict, dict):
response_data = person_dict
else:
response_data = person_dict
else:
return mk_resp(data=False, status_code=400) # Bad Request
return mk_resp(data=response_data)
# ### END ### API Person ### get_person_obj() ###
# ### BEGIN ### API Person ### get_person_obj_order_list() ###
# Working well as of 2021-06-28. Using as a template for other routes.
@router.get('/person/{person_id}/order_list', response_model=Resp_Body_Base)
async def get_person_obj_order_list(
person_id: str = Query(..., min_length=1, max_length=22),
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
from_datetime: datetime.datetime = None,
to_datetime: datetime.datetime = None,
# inc_address: bool = False,
# inc_contact: bool = False,
inc_order_cfg: bool = False,
inc_order_line_list: bool = False,
status: str = 'complete',
# inc_order_list: bool = False,
# inc_order_cart_list: bool = False,
# inc_user: bool = False,
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else:
return mk_resp(data=None, status_code=404)
# Updated 2021-06-28
if order_rec_list_result := get_order_rec_list(
for_obj_type = 'person',
for_obj_id = person_id,
limit = limit,
enabled = enabled,
from_datetime = from_datetime,
to_datetime = to_datetime,
status = status,
):
order_result_list = []
for order_rec in order_rec_list_result:
if load_order_result := load_order_obj(
order_id = order_rec.get('order_id', None),
limit = limit,
enabled = enabled,
by_alias = by_alias,
exclude_unset = exclude_unset,
# model_as_dict = model_as_dict,
inc_order_cfg = inc_order_cfg,
inc_order_line_list = inc_order_line_list,
):
order_result_list.append(load_order_result)
else:
order_result_list.append(None)
response_data = order_result_list
else:
return mk_resp(data=False, status_code=400) # Bad Request
return mk_resp(data=response_data)
# ### END ### API Person ### get_person_obj_order_list() ###
# ### BEGIN ### API Person ### get_account_obj_person_list() ###
# Working well as of 2021-07-09. Using as a template for other routes.
@router.get('/account/{account_id}/person/list', response_model=Resp_Body_Base)
async def get_account_obj_person_list(
account_id: str = Query(..., min_length=1, max_length=22),
limit: int = 500, # For now this covers any included objects or object lists
enabled: str = 'enabled', # For now this covers any included objects or object lists
inc_address: bool = False,
inc_contact: bool = False,
# inc_membership_group_list: bool = False, # The list of all membership groups a person is a part of
inc_membership_person: bool = False,
# inc_membership_person_profile: bool = False, # Profile?
# inc_membership_person_profile_cust: bool = False, # Extended profile?
# inc_membership_type_person: bool = False,
# inc_order: bool = False,
# inc_organization: bool = False,
# inc_product: bool = False, # The product the person actually purchased for a member_type or member_group
# inc_product_list: bool = False, # The list of products that give access to a member_type or member_group
inc_user: bool = False,
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
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_data = None
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# Updated 2021-06-30
if person_rec_list_result := get_person_rec_list(
for_obj_type = 'account',
for_obj_id = account_id,
limit = limit,
enabled = enabled,
):
person_result_list = []
for person_rec in person_rec_list_result:
if load_person_result := load_person_obj(
person_id = person_rec.get('person_id', None),
limit = limit,
by_alias = by_alias,
exclude_unset = exclude_unset,
# model_as_dict = model_as_dict,
enabled = enabled,
inc_address = inc_address,
inc_contact = inc_contact,
inc_membership_person = inc_membership_person,
inc_user = inc_user,
):
person_result_list.append(load_person_result)
else:
person_result_list.append(None)
response_data = person_result_list
else:
return mk_resp(data=False, status_code=400) # Bad Request
return mk_resp(data=response_data)
# ### END ### API Person ### get_account_obj_person_list() ###
@router.delete('/person/{obj_id}', response_model=Resp_Body_Base)
async def delete_person_obj(
obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: str = Header(...),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'person'
result = delete_obj_template(
obj_type=obj_type,
obj_id=obj_id,
)
return result