254 lines
9.4 KiB
Python
254 lines
9.4 KiB
Python
import datetime
|
|
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.cont_edu_cert_methods import get_cont_edu_cert_rec_list, load_cont_edu_cert_obj
|
|
|
|
from app.models.cont_edu_cert_models import Cont_Edu_Cert_Base
|
|
from app.models.response_models import *
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post('/cont_edu/cert', response_model=Resp_Body_Base)
|
|
async def post_cont_edu_cert_obj(
|
|
obj: Cont_Edu_Cert_Base,
|
|
x_account_id: str = Header(...),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'cont_edu_cert'
|
|
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('/cont_edu/cert/{obj_id}', response_model=Resp_Body_Base)
|
|
async def patch_cont_edu_cert_obj(
|
|
obj: Cont_Edu_Cert_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 = 'cont_edu_cert'
|
|
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 Cont Edu Cert ### patch_cont_edu_cert_json() ###
|
|
@router.patch('/cont_edu/cert/{cont_edu_cert_id}/json', response_model=Resp_Body_Base)
|
|
async def patch_cont_edu_cert_json(
|
|
cont_edu_cert_obj: Cont_Edu_Cert_Base,
|
|
cont_edu_cert_id: str = Query(..., min_length=1, max_length=22),
|
|
create_missing_obj: 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.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if cont_edu_cert_id := redis_lookup_id_random(record_id_random=cont_edu_cert_id, table_name='cont_edu_cert'): pass
|
|
else:
|
|
return mk_resp(data=None, status_code=404)
|
|
|
|
if cont_edu_cert_obj_up_result := update_cont_edu_cert_obj(
|
|
cont_edu_cert_id=cont_edu_cert_id,
|
|
cont_edu_cert_obj_up=cont_edu_cert_obj,
|
|
create_missing_obj=create_missing_obj,
|
|
):
|
|
|
|
log.debug(cont_edu_cert_obj_up_result)
|
|
if return_obj:
|
|
cont_edu_cert_obj = load_cont_edu_cert_obj(cont_edu_cert_id=cont_edu_cert_id)
|
|
cont_edu_cert_dict = cont_edu_cert_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
|
return mk_resp(data=cont_edu_cert_dict)
|
|
else:
|
|
return mk_resp(data=cont_edu_cert_obj_up_result)
|
|
else:
|
|
return mk_resp(data=False, status_code=400) # Bad Request
|
|
# ### END ### API Cont Edu Cert ### patch_cont_edu_cert_json() ###
|
|
|
|
|
|
@router.get('/cont_edu/cert/list', response_model=Resp_Body_Base)
|
|
async def get_cont_edu_cert_obj_li(
|
|
for_obj_type: str = Query(None, min_length=2, max_length=50),
|
|
for_obj_id: str = Query(None, min_length=1, max_length=22),
|
|
group: str = Query(None, min_length=2, max_length=50),
|
|
x_account_id: str = Header(...),
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'cont_edu_cert'
|
|
|
|
if for_obj_type == 'event_exhibit' and for_obj_id:
|
|
#base_name = obj_type_li[obj_type]['base_name']
|
|
base_name = Cont_Edu_Cert_Base
|
|
|
|
for_obj_id_random = for_obj_id
|
|
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id_random, table_name=for_obj_type)
|
|
|
|
data = {}
|
|
data['for_obj_type'] = for_obj_type
|
|
data['for_obj_id'] = for_obj_id
|
|
data['for_obj_id_random'] = for_obj_id_random
|
|
data['group'] = group
|
|
|
|
if group:
|
|
sql = """
|
|
SELECT *
|
|
FROM `cont_edu_cert` AS cont_edu_cert
|
|
WHERE cont_edu_cert.for_type = :for_obj_type AND cont_edu_cert.for_id = :for_obj_id
|
|
AND cont_edu_cert.group = :group
|
|
"""
|
|
else:
|
|
sql = """
|
|
SELECT *
|
|
FROM `cont_edu_cert` AS cont_edu_cert
|
|
WHERE cont_edu_cert.for_type = :for_obj_type AND cont_edu_cert.for_id = :for_obj_id
|
|
"""
|
|
|
|
if sql_result := sql_select(data=data, sql=sql, as_list=True):
|
|
resp_data_li = []
|
|
for record in sql_result:
|
|
resp_data = base_name(**record).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
|
resp_data_li.append(resp_data)
|
|
|
|
return mk_resp(data=resp_data_li)
|
|
else:
|
|
log.debug(sql_result)
|
|
return mk_resp(data=False, status_code=404)
|
|
|
|
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('/cont_edu/cert/{obj_id}', response_model=Resp_Body_Base)
|
|
async def get_cont_edu_cert_obj(
|
|
obj_id: str = Query(..., 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 = 'cont_edu_cert'
|
|
result = get_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
# ### BEGIN ### API Cont Edu Cert ### get_account_obj_cont_edu_cert_list() ###
|
|
# Updated 2021-07-28
|
|
@router.get('/account/{account_id}/cont_edu/cert/list', response_model=Resp_Body_Base)
|
|
async def get_account_obj_cont_edu_cert_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_cont_edu_cert_person_list: 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-07-28
|
|
if cont_edu_cert_rec_list_result := get_cont_edu_cert_rec_list(
|
|
account_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
cont_edu_cert_result_list = []
|
|
for cont_edu_cert_rec in cont_edu_cert_rec_list_result:
|
|
if load_cont_edu_cert_result := load_cont_edu_cert_obj(
|
|
cont_edu_cert_id = cont_edu_cert_rec.get('cont_edu_cert_id', None),
|
|
limit = limit,
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
inc_cont_edu_cert_person_list = inc_cont_edu_cert_person_list,
|
|
):
|
|
cont_edu_cert_result_list.append(load_cont_edu_cert_result)
|
|
else:
|
|
cont_edu_cert_result_list.append(None)
|
|
response_data = cont_edu_cert_result_list
|
|
else:
|
|
return mk_resp(data=False, status_code=400) # Bad Request
|
|
|
|
return mk_resp(data=response_data)
|
|
# ### END ### API ### get_account_obj_cont_edu_cert_list() ###
|
|
|
|
|
|
@router.delete('/cont_edu/cert/{obj_id}', response_model=Resp_Body_Base)
|
|
async def delete_cont_edu_cert_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 = 'cont_edu_cert'
|
|
result = delete_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
)
|
|
return result |