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.organization_methods import create_update_organization_obj, load_organization_obj from app.models.organization_models import Organization_Base from app.models.response_models import Resp_Body_Base, mk_resp router = APIRouter() @router.post('', response_model=Resp_Body_Base) async def post_organization_obj( obj: Organization_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 = 'organization' 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_organization_obj( obj: Organization_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 = 'organization' 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 Organization ### post_organization_json() ### # Updated 2021-06-25 @router.post('/json', response_model=Resp_Body_Base) async def post_organization_json( organization_obj: Organization_Base, # organization_id: str = Query(..., min_length=1, max_length=22), # create_missing_obj: bool = False, process_contact: 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 organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass # else: return mk_resp(data=None, status_code=404) if organization_obj_in_result := create_update_organization_obj( organization_id = None, organization_obj = organization_obj, process_contact = process_contact, ): log.debug(organization_obj_in_result) if return_obj: organization_obj = load_organization_obj(organization_id=organization_obj_in_result) organization_dict = organization_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) return mk_resp(data=organization_dict) else: return mk_resp(data=organization_obj_in_result) else: return mk_resp(data=False, status_code=400) # Bad Request # ### END ### API Organization ### post_organization_json() ### # ### BEGIN ### API Organization ### patch_organization_json() ### # Updated 2021-06-25 @router.patch('/{organization_id}/json', response_model=Resp_Body_Base) async def patch_organization_json( organization_obj: Organization_Base, organization_id: str = Query(..., min_length=1, max_length=22), process_contact: 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 organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass else: return mk_resp(data=None, status_code=404) if organization_obj_up_result := create_update_organization_obj( organization_id = organization_id, organization_obj = organization_obj, process_contact = process_contact, ): log.debug(organization_obj_up_result) if return_obj: organization_obj = load_organization_obj(organization_id=organization_id) organization_dict = organization_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) return mk_resp(data=organization_dict) else: return mk_resp(data=organization_obj_up_result) else: return mk_resp(data=False, status_code=400) # Bad Request # ### END ### API Organization ### patch_organization_json() ### @router.get('/list', response_model=Resp_Body_Base) async def get_organization_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 = 'organization' 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 @router.get('/{organization_id}', response_model=Resp_Body_Base) async def get_organization_obj( organization_id: str = Query(..., min_length=1, max_length=22), inc_address: bool = False, inc_contact: bool = False, x_account_id: str = Header(...), by_alias: bool = True, exclude_unset: bool = True, ): log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) organization_obj = load_organization_obj( organization_id=organization_id, inc_contact=inc_contact, inc_address=inc_address, ).dict(by_alias=by_alias, exclude_unset=exclude_unset) data = organization_obj return mk_resp(data=organization_obj) @router.delete('/{obj_id}', response_model=Resp_Body_Base) async def delete_organization_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 = 'organization' result = delete_obj_template( obj_type=obj_type, obj_id=obj_id, ) return result