214 lines
10 KiB
Python
214 lines
10 KiB
Python
import datetime, pytz, time
|
|
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, common_route_params, Common_Route_Params, common_route_params_no_account_id, Common_Route_Params_No_Account_ID
|
|
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.event_device_methods import create_update_event_device_obj, get_event_device_rec_list, load_event_device_obj
|
|
|
|
from app.models.common_field_schema import default_num_bytes
|
|
from app.models.event_device_models import Event_Device_Base
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# ### BEGIN ### API Event Device Routers ### post_event_device_obj() ###
|
|
# Updated 2022-03-09
|
|
@router.post('/event/device', response_model=Resp_Body_Base)
|
|
async def post_event_device_obj(
|
|
event_device_obj: Event_Device_Base,
|
|
event_id: str = Query(None, min_length=11, max_length=22),
|
|
event_location_id: str = Query(None, min_length=11, max_length=22),
|
|
|
|
inc_event_cfg: bool = False,
|
|
inc_event_location: bool = False,
|
|
return_obj: bool = True,
|
|
|
|
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 event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
|
# elif event_id is None: pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The event ID was invalid or not found.')
|
|
|
|
if event_location_id := redis_lookup_id_random(record_id_random=event_location_id, table_name='event_location'): pass
|
|
elif event_location_id is None: pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The event location ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if event_device_id := create_update_event_device_obj(
|
|
event_device_dict_obj = event_device_obj,
|
|
event_id = event_id,
|
|
event_location_id = event_location_id,
|
|
): pass
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Not created. Something failed while processing the data. Check the field names and data types.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
event_device_obj = load_event_device_obj(
|
|
event_device_id = event_device_id,
|
|
inc_event_cfg = inc_event_cfg,
|
|
inc_event_location = inc_event_location,
|
|
)
|
|
data = event_device_obj
|
|
else:
|
|
event_device_id_random = get_id_random(record_id=event_device_id, table_name='event_device')
|
|
data = {}
|
|
data['event_device_id'] = event_device_id
|
|
data['event_device_id_random'] = event_device_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Event Device Routers ### post_event_device_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Event Device Routers ### patch_event_device_obj() ###
|
|
# Updated 2022-03-09
|
|
@router.patch('/event/device/{event_device_id}', response_model=Resp_Body_Base)
|
|
async def patch_event_device_obj(
|
|
event_device_obj: Event_Device_Base,
|
|
event_device_id: str = Path(min_length=11, max_length=22),
|
|
event_id: str = Query(None, min_length=11, max_length=22),
|
|
event_location_id: str = Query(None, min_length=11, max_length=22),
|
|
|
|
inc_event_cfg: bool = False,
|
|
inc_event_location: bool = False,
|
|
return_obj: Optional[bool] = True,
|
|
|
|
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
|
|
event_device_id_random = event_device_id # This is used later for the response data
|
|
if event_device_id := redis_lookup_id_random(record_id_random=event_device_id, table_name='event_device'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The event device ID was invalid or not found.')
|
|
|
|
if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
|
elif event_id is None: pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The event ID was invalid or not found.')
|
|
|
|
if event_location_id := redis_lookup_id_random(record_id_random=event_location_id, table_name='event_location'): pass
|
|
elif event_location_id is None: pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The event location ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if event_device_up_result := create_update_event_device_obj(
|
|
event_device_dict_obj = event_device_obj,
|
|
event_device_id = event_device_id,
|
|
event_id = event_id,
|
|
event_location_id = event_location_id,
|
|
): pass
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Not updated. Something failed while processing the data. Check the field names and data types.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
event_device_obj = load_event_device_obj(
|
|
event_device_id = event_device_id,
|
|
inc_event_cfg = inc_event_cfg,
|
|
inc_event_location = inc_event_location,
|
|
)
|
|
data = event_device_obj
|
|
else:
|
|
data = {}
|
|
data['event_device_id'] = event_device_id
|
|
data['event_device_id_random'] = event_device_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Event Device Routers ### patch_event_device_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Event Device ### get_event_device_obj() ###
|
|
# Updated 2022-03-09
|
|
@router.get('/event/device/{event_device_id}', response_model=Resp_Body_Base)
|
|
async def get_event_device_obj(
|
|
event_device_id: str = Path(min_length=11, max_length=22),
|
|
|
|
inc_event_cfg: bool = False,
|
|
inc_event_location: bool = False,
|
|
|
|
# commons: Common_Route_Params = Depends(common_route_params),
|
|
commons: Common_Route_Params_No_Account_ID = Depends(common_route_params_no_account_id),
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
if event_device_id := redis_lookup_id_random(record_id_random=event_device_id, table_name='event_device'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response)
|
|
|
|
if event_device_rec_result := load_event_device_obj(
|
|
event_device_id = event_device_id,
|
|
limit = commons.limit,
|
|
enabled = commons.enabled,
|
|
inc_event_cfg = inc_event_cfg,
|
|
inc_event_location = inc_event_location,
|
|
):
|
|
log.info('Loading successful. Returning result')
|
|
return mk_resp(data=event_device_rec_result, response=commons.response)
|
|
elif isinstance(event_device_rec_result, list) or event_device_rec_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 Event Device ### get_event_device_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Event Device ### get_event_obj_device_list() ###
|
|
# Updated 2022-03-09
|
|
@router.get('/event/{event_id}/device/list', response_model=Resp_Body_Base)
|
|
async def get_event_obj_device_list(
|
|
event_id: str = Path(min_length=11, max_length=22),
|
|
inc_event_cfg: bool = False,
|
|
inc_event_location: 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)
|
|
|
|
# Updated 2022-03-09
|
|
if event_device_rec_list_result := get_event_device_rec_list(
|
|
for_type = 'event',
|
|
for_id = event_id,
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
offset = commons.offset,
|
|
):
|
|
event_device_result_list = []
|
|
for event_device_rec in event_device_rec_list_result:
|
|
if load_event_device_result := load_event_device_obj(
|
|
event_device_id = event_device_rec.get('event_device_id', None),
|
|
enabled = commons.enabled,
|
|
inc_event_cfg = inc_event_cfg,
|
|
inc_event_location = inc_event_location,
|
|
):
|
|
event_device_result_list.append(load_event_device_result)
|
|
else:
|
|
event_device_result_list.append(None)
|
|
response_data = event_device_result_list
|
|
return mk_resp(data=response_data, response=commons.response)
|
|
elif isinstance(event_device_rec_list_result, list) or event_device_rec_list_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 Event Device ### get_event_obj_device_list() ###
|