247 lines
10 KiB
Python
247 lines
10 KiB
Python
import datetime, json
|
|
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, get_id_random, 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.grant_methods import create_update_grant_obj, load_grant_obj, get_grant_rec_list
|
|
# from app.methods.grant_methods import create_update_grant_obj, load_grant_obj, get_grant_rec_list, remove_grant_obj
|
|
|
|
from app.models.grant_models import Grant_In
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# # ### BEGIN ### API Grant ### post_grant_obj() ###
|
|
# # Updated 2023-03-22
|
|
# @router.post('/grant', response_model=Resp_Body_Base)
|
|
# async def post_grant_obj(
|
|
# grant_obj: Grant_In,
|
|
# # account_id: str = Query(..., min_length=11, max_length=22),
|
|
# event_id: str = Query(..., min_length=11, max_length=22),
|
|
|
|
# return_obj: bool = False,
|
|
# inc_event_abstract: bool = False,
|
|
|
|
# commons: Common_Route_Params = Depends(common_route_params),
|
|
# ):
|
|
# log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
# log.debug(locals())
|
|
|
|
# if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
|
# else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The Event ID was invalid or not found.')
|
|
|
|
# # There should probably be a check for the event ID before calling the create function?
|
|
# if create_grant_obj_result := create_update_grant_obj(
|
|
# grant_obj = grant_obj,
|
|
# account_id = commons.account_id,
|
|
# event_id = event_id,
|
|
|
|
# return_outline = False,
|
|
# ): pass
|
|
# else: return mk_resp(data=False, status_code=400, response=commons.response, status_message='The grant was not created. Check the field names and data types.')
|
|
|
|
# if isinstance(create_grant_obj_result, int):
|
|
# grant_id = create_grant_obj_result
|
|
# if return_obj:
|
|
# if load_grant_obj_result := load_grant_obj(
|
|
# grant_id = grant_id,
|
|
# inc_event_file_list = inc_event_file_list,
|
|
# inc_event_person = inc_event_person,
|
|
# inc_event_presentation_list = inc_event_presentation_list,
|
|
# inc_event_presenter_list = inc_event_presenter_list,
|
|
# ):
|
|
# data = load_grant_obj_result
|
|
# else:
|
|
# data = False
|
|
# else:
|
|
# grant_id = grant_id
|
|
# grant_id_random = get_id_random(record_id=grant_id, table_name='grant')
|
|
# data = {}
|
|
# data['grant_id'] = grant_id
|
|
# data['grant_id_random'] = grant_id_random
|
|
# return mk_resp(data=data, response=commons.response, status_message='The grant was created.')
|
|
# else:
|
|
# return mk_resp(data=False, status_code=400, response=commons.response, status_message='The result from trying to create an grant was unexpected.')
|
|
# # ### END ### API Grant ### post_grant_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Grant ### patch_grant_obj() ###
|
|
# Updated 2023-06-28
|
|
@router.patch('/grant/{grant_id}', response_model=Resp_Body_Base)
|
|
async def patch_grant_obj(
|
|
grant_obj: Grant_In,
|
|
grant_id: str = Query(..., min_length=11, max_length=22),
|
|
|
|
return_obj: bool = True,
|
|
inc_event_abstract: bool = False,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if grant_id := redis_lookup_id_random(record_id_random=grant_id, table_name='grant'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The Grant ID was invalid or not found.')
|
|
|
|
if update_grant_obj_result := create_update_grant_obj(
|
|
grant_obj = grant_obj,
|
|
grant_id = grant_id,
|
|
return_outline = False,
|
|
): pass
|
|
else: return mk_resp(data=False, status_code=400, response=commons.response, status_message='The grant was not created. Check the field names and data types.')
|
|
|
|
if update_grant_obj_result:
|
|
if return_obj:
|
|
if load_grant_obj_result := load_grant_obj(
|
|
grant_id = grant_id,
|
|
inc_event_abstract = inc_event_abstract,
|
|
):
|
|
data = load_grant_obj_result
|
|
else:
|
|
data = False
|
|
else:
|
|
grant_id_random = get_id_random(record_id=grant_id, table_name='grant')
|
|
data = {}
|
|
data['grant_id'] = grant_id
|
|
data['grant_id_random'] = grant_id_random
|
|
return mk_resp(data=data, response=commons.response, status_message='The grant was created or updated.')
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='The result from trying to create an grant was unexpected.')
|
|
# ### END ### API Grant ### patch_grant_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Grant ### get_grant_obj() ###
|
|
# Updated 2023-03-22
|
|
@router.get('/grant/{grant_id}', response_model=Resp_Body_Base)
|
|
async def get_grant_obj(
|
|
grant_id: str = Query(..., min_length=11, max_length=22),
|
|
|
|
inc_event_abstract: bool = False,
|
|
|
|
hidden: str = 'not_hidden', # hidden, not_hidden, all
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# time.sleep(.5)
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
grant_id_random = grant_id # This is used later for the response data
|
|
if grant_id := redis_lookup_id_random(record_id_random=grant_id, table_name='grant'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The Grant ID was invalid or not found.')
|
|
|
|
if grant_obj_result := load_grant_obj(
|
|
grant_id = grant_id,
|
|
|
|
enabled = commons.enabled,
|
|
hidden = hidden, # hidden, not_hidden, all
|
|
|
|
inc_event_abstract = inc_event_abstract,
|
|
):
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(grant_obj_result)
|
|
return mk_resp(data=grant_obj_result, response=commons.response) # Success
|
|
elif isinstance(grant_obj_result, list) or grant_obj_result is None: # Empty list or None
|
|
log.info('No results')
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
# ### END ### API Grant ### get_grant_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Grant ### get_event_id_grant_obj_li() ###
|
|
# Updated 2023-06-23
|
|
@router.get('/event/{event_id}/grant/list', response_model=Resp_Body_Base)
|
|
async def get_event_id_grant_obj_li(
|
|
event_id: str = Query(..., min_length=11, max_length=22),
|
|
|
|
inc_event_abstract: bool = False,
|
|
|
|
hidden: str = 'not_hidden', # hidden, not_hidden, all
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response)
|
|
|
|
# Updated 2023-06-23
|
|
if grant_rec_list_result := get_grant_rec_list(
|
|
event_id = event_id,
|
|
|
|
hidden = hidden, # hidden, not_hidden, all
|
|
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
offset = commons.offset,
|
|
):
|
|
grant_result_list = []
|
|
for grant_rec in grant_rec_list_result:
|
|
if load_grant_result := load_grant_obj(
|
|
grant_id = grant_rec.get('grant_id', None),
|
|
|
|
hidden = hidden,
|
|
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
inc_event_abstract = inc_event_abstract,
|
|
):
|
|
grant_result_list.append(load_grant_result)
|
|
else:
|
|
grant_result_list.append(None)
|
|
response_data = grant_result_list
|
|
elif isinstance(grant_rec_list_result, list) or grant_rec_list_result is None: # Empty list or None
|
|
log.info('No results')
|
|
return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
|
|
return mk_resp(data=response_data, response=commons.response)
|
|
# ### END ### API Grant ### get_event_id_grant_obj_li() ###
|
|
|
|
|
|
# # ### BEGIN ### API Grant ### delete_grant_obj() ###
|
|
# # Updated 2023-03-22
|
|
# @router.delete('/grant/{grant_id}', response_model=Resp_Body_Base)
|
|
# def delete_grant_obj(
|
|
# grant_id: str = Query(..., min_length=11, max_length=22),
|
|
|
|
# method: str = None, # None, delete, disable, hide
|
|
|
|
# commons: Common_Route_Params = Depends(common_route_params),
|
|
# ):
|
|
# log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
# log.debug(locals())
|
|
|
|
# # ### SECTION ### Secondary data validation
|
|
# if grant_id := redis_lookup_id_random(record_id_random=grant_id, table_name='grant'): pass
|
|
# else: return mk_resp(data=None, status_code=404, response=response, status_message='The Grant ID was invalid or not found.')
|
|
|
|
# if grant_obj_result := remove_grant_obj(
|
|
# grant_id = grant_id,
|
|
# method = method,
|
|
# ):
|
|
# log.info('Delete successful. Returning True')
|
|
# return mk_resp(data=True, response=response) # Success
|
|
# elif grant_obj_result is None: # None
|
|
# log.info('No results')
|
|
# return mk_resp(data=None, status_code=404, response=response) # Not Found
|
|
# else:
|
|
# log.warning('Likely bad request')
|
|
# return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
|
# # ### END ### API Grant ### delete_grant_obj() ###
|