Files
OSIT-AE-API-FastAPI/app/routers/api_crud.py
2021-03-05 18:27:12 -05:00

137 lines
5.1 KiB
Python

import datetime
#from datetime import datetime, time, timedelta
from fastapi import APIRouter, Depends, Header, HTTPException, Query, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from ..lib_general import *
from ..log import *
from app.config import settings
from app.db import *
#from .journal_models import *
#from .user_models import *
from .user_model import *
from .response_model import *
obj_type_li = {}
obj_type_li['account'] = 'v_account'
obj_type_li['activity_log'] = 'activity_log'
obj_type_li['address'] = 'v_address'
obj_type_li['archive'] = 'v_archive'
obj_type_li['archive_content'] = 'v_archive_content'
obj_type_li['contact'] = 'v_contact'
obj_type_li['event'] = 'v_event'
obj_type_li['event_badge'] = 'v_event_badge'
obj_type_li['event_exhibit'] = 'v_event_exhibit'
obj_type_li['event_location'] = 'v_event_location'
obj_type_li['event_presentation'] = 'v_event_presentation'
obj_type_li['event_presenter'] = 'v_event_presenter'
obj_type_li['event_session'] = 'v_event_session'
obj_type_li['event_track'] = 'v_event_track'
obj_type_li['hosted_file'] = 'v_hosted_file'
obj_type_li['journal'] = 'v_journal'
obj_type_li['log'] = 'log' #'v_log'
obj_type_li['log_client_viewing'] = 'log_client_viewing'
obj_type_li['message'] = 'message' #'v_message'
obj_type_li['order'] = 'v_order'
obj_type_li['order_cart'] = 'v_order_cart'
obj_type_li['order_cart_line'] = 'v_order_cart_line'
obj_type_li['order_line'] = 'v_order_line'
obj_type_li['order_transaction'] = 'order_transaction'
obj_type_li['organization'] = 'v_organization'
obj_type_li['page'] = 'page'
obj_type_li['person'] = 'v_person'
obj_type_li['post'] = 'v_post_detail'
obj_type_li['post_comment'] = 'v_post_comment_detail'
obj_type_li['product'] = 'v_product'
obj_type_li['site'] = 'v_site'
obj_type_li['site_domain'] = 'v_site_domain'
obj_type_li['user'] = 'v_user'
obj_type_li['lu_education_degree'] = 'lu_education_degree'
obj_type_li['lu_education_level'] = 'lu_education_level'
obj_type_li['lu_html_color'] = 'lu_html_color'
obj_type_li['lu_time_zone'] = 'v_lu_time_zone'
obj_type_li['lu_user_status'] = 'lu_user_status'
obj_type_li['stripe_customer'] = 'stripe_customer'
obj_type_li['stripe_log'] = 'stripe_log'
router = APIRouter()
# Working on the basic API CRUD - STI 2021-03-05
#@router.get('/{object_l1}/list')
@router.get('/{object_l1}/{object_id}/list')
@router.get('/{object_l1}/{object_l2}/{object_id}/list')
@router.get('/{object_l1}/{object_l2}/{object_id}/{object_l3}/list')
async def get_obj_li(object_l1: str=None, object_l2: str=None, object_l3: str=None, object_id: str=None, x_account_id: str = Header(...)):
response_data = {}
response_data['object_l1'] = object_l1
response_data['object_l2'] = object_l2
response_data['object_l3'] = object_l3
response_data['object_id'] = object_id
response_data['list'] = 'li'
sql_result = sql_select(table_name='user', record_id=1)
response_data['sql_result'] = sql_result
return response_data
#@router.get('/{obj_type_l1}/{obj_id_int}')
@router.get('/{obj_type_l1}/{obj_id}')
@router.get('/{obj_type_l1}/{obj_type_l2}/{obj_id}')
@router.get('/{obj_type_l1}/{obj_type_l2}/{obj_type_l3}/{obj_id}')
async def get_obj(obj_type_l1: str=None, obj_type_l2: str=None, obj_type_l3: str=None, obj_id: str=None, x_account_id: str = Header(...),
qry_str: Optional[str] = Query(None, max_length=50),
qry_int: Optional[int] = None,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
debug_data = {}
debug_data['obj_type_l1'] = obj_type_l1
debug_data['obj_type_l2'] = obj_type_l2
debug_data['obj_type_l3'] = obj_type_l3
debug_data['obj_id'] = obj_id
#debug_data['object_id_int'] = object_id_int
#debug_data['object_id_rand'] = object_id_rand
log.debug(debug_data)
if obj_type_l1 and obj_type_l2 and obj_type_l3:
obj_name = f'{obj_type_l1}_{obj_type_l2}_{obj_type_l3}'
if obj_name in obj_type_li:
table_name = obj_type_li[obj_name]
else:
return mk_resp(data=False, status_code=400)
elif obj_type_l1 and obj_type_l2:
obj_name = f'{obj_type_l1}_{obj_type_l2}'
if obj_name in obj_type_li:
table_name = obj_type_li[obj_name]
else:
return mk_resp(data=False, status_code=400)
elif obj_type_l1:
obj_name = f'{obj_type_l1}'
if obj_name in obj_type_li:
table_name = obj_type_li[obj_name]
else:
return mk_resp(data=False, status_code=400)
else:
log.warning('We should not be here')
return mk_resp(data=False, status_code=400)
# NOTE: Add a check for the object ID... assuming it is a random ID string for now.
sql_result = sql_select(table_name=table_name, record_id_random=obj_id)
log.debug(sql_result)
resp_data = User_Base(**sql_result).dict(by_alias=by_alias, exclude_unset=exclude_unset)
return mk_resp(data=resp_data)#, details=debug_data)