Files
OSIT-AE-API-FastAPI/app/routers/activity_log.py

183 lines
6.8 KiB
Python

import datetime
#from datetime import datetime, time, timedelta
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query, Response, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from app.lib_general import log, logging
#from ..log import *
from app.config import settings
from app.db_sql import *
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.activity_log_methods import get_activity_log_rec_list, load_activity_log_obj
from app.models.activity_log_models import Activity_Log_Base
from app.models.response_models import *
router = APIRouter()
@router.post('/activity_log', response_model=Resp_Body_Base)
async def post_activity_log_obj(
activity_log_obj: Activity_Log_Base,
x_account_id: str = Header(...),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'activity_log'
activity_log_obj_data_dict = activity_log_obj.dict(by_alias=False, exclude_unset=True)
result = post_obj_template(
obj_type = obj_type,
data = activity_log_obj_data_dict,
return_obj = True,
by_alias = True,
exclude_unset = True,
)
return result
@router.patch('/activity_log/{activity_log_id}', response_model=Resp_Body_Base)
async def patch_activity_log_obj(
activity_log_obj: Activity_Log_Base,
activity_log_id: str = Path(..., 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,
response: Response = Response,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'activity_log'
activity_log_obj_data_dict = activity_log_obj.dict(by_alias=False, exclude_unset=True)
activity_log_obj_data_dict['id'] = redis_lookup_id_random(record_id_random=activity_log_id, table_name=obj_type)
activity_log_obj_data_dict['id_random'] = activity_log_id
result = patch_obj_template(
obj_type = obj_type,
data = activity_log_obj_data_dict,
obj_id = activity_log_id,
return_obj = True,
by_alias = True,
exclude_unset = True,
)
return result
@router.get('/activity_log/list', response_model=Resp_Body_Base)
async def get_activity_log_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,
response: Response = Response,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'activity_log'
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('/activity_log/{obj_id}', response_model=Resp_Body_Base)
async def get_activity_log_obj(
obj_id: str = Path(min_length=1, max_length=22),
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'activity_log'
result = get_obj_template(
obj_type=obj_type,
obj_id=obj_id,
by_alias=True,
exclude_unset=True,
)
return result
# ### BEGIN ### API Activity Log ### get_account_obj_activity_log_list() ###
# Updated 2021-09-21
@router.get('/account/{account_id}/activity_log/list', response_model=Resp_Body_Base)
async def get_account_obj_activity_log_list(
account_id: str = Path(min_length=11, 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
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
):
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=response)
# Updated 2021-09-21
if activity_log_rec_list_result := get_activity_log_rec_list(
account_id = account_id,
enabled = enabled,
limit = limit,
):
activity_log_result_list = []
for activity_log_rec in activity_log_rec_list_result:
if load_activity_log_result := load_activity_log_obj(
activity_log_id = activity_log_rec.get('activity_log_id', None),
enabled = enabled,
limit = limit,
by_alias = by_alias,
exclude_unset = exclude_unset,
# model_as_dict = model_as_dict,
):
activity_log_result_list.append(load_activity_log_result)
else:
activity_log_result_list.append(None)
response_data = activity_log_result_list
elif isinstance(activity_log_rec_list_result, list) or activity_log_rec_list_result is None: # Empty list or None
log.info('No results')
return mk_resp(data=False, 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
return mk_resp(data=response_data, response=response)
# ### END ### API Activity Log ### get_account_obj_activity_log_list() ###
@router.delete('/activity_log/{obj_id}', response_model=Resp_Body_Base)
async def delete_activity_log_obj(
obj_id: str = Path(min_length=1, max_length=22),
x_account_id: str = Header(...),
response: Response = Response,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'activity_log'
result = delete_obj_template(
obj_type=obj_type,
obj_id=obj_id,
)
return result