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.person_methods import create_update_person_obj, 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('', 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('/{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('/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_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('/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_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_member: bool = False, # Priority l2 # inc_membership_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.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_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_member = inc_membership_member, # inc_membership_list = inc_membership_list, # ??? 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() ### @router.delete('/{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