diff --git a/app/methods/event_badge_methods.py b/app/methods/event_badge_methods.py index db19db3..0129777 100644 --- a/app/methods/event_badge_methods.py +++ b/app/methods/event_badge_methods.py @@ -176,4 +176,4 @@ def create_update_event_badge_obj_v4( else: log.debug(f'Returning the Event Badge ID: {event_badge_id}') return event_badge_id -# ### END ### API Event Badge Methods ### create_update_event_badge_obj_v4() ### \ No newline at end of file +# ### END ### API Event Badge Methods ### create_update_event_badge_obj_v4() ### diff --git a/app/methods/event_device_methods.py b/app/methods/event_device_methods.py index 828a465..ab44be9 100644 --- a/app/methods/event_device_methods.py +++ b/app/methods/event_device_methods.py @@ -29,7 +29,7 @@ def load_event_device_obj( inc_event_cfg: bool = False, inc_event_location: bool = False, ) -> Event_Device_Base|dict|bool: - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if event_device_rec := sql_select(table_name='v_event_device', record_id=event_device_id): pass @@ -83,7 +83,7 @@ def get_event_device_rec_list( limit: int = 100, offset: int = 0, ) -> list|bool: - log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass @@ -124,3 +124,100 @@ def get_event_device_rec_list( return event_device_rec_li # ### END ### API Event Device Methods ### get_event_device_rec_list() ### + + +# ### BEGIN ### API Event Device Methods ### create_update_event_device_obj() ### +# Updated 2022-03-09 +def create_update_event_device_obj( + event_device_dict_obj: Event_Device_Base|dict, + event_device_id: int|str = None, + event_id: int|str = None, + event_location_id: int|str = None, + create_sub_obj: bool = False, + fail_any: bool = False, # Fail if any thing goes wrong for sub objects + return_outline: bool = False, + ) -> int|bool: + log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.debug(locals()) + + # ### SECTION ### Secondary data validation + log.info('Checking requirements...') + if event_device_id: + log.info(f'Event Device ID passed. Update existing Event Device. Event Device ID: {event_device_id}') + + if event_device_id := redis_lookup_id_random(record_id_random=event_device_id, table_name='event_device'): pass + else: + log.error('Event Device ID passed but is invalid. Failed requirement.') + return False + else: + log.info('No Event Device ID passed. Create new Event Device. Required: Event ID; Optional: Event Location ID') + + if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass + else: + log.error('Missing or invalid Event ID passed. Failed requirement.') + log.info(f'Event ID: {event_id}') + return False + + 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: + log.warning('Missing or invalid Event Location ID passed. Failed requirement.') + log.info(f'Event Location ID: {event_location_id}') + return False + + log.info('Create dictionary or Pydantic object') + log.debug(type(event_device_dict_obj)) + if isinstance(event_device_dict_obj, dict): + event_device_dict = event_device_dict_obj + if event_device_id: + event_device_dict['event_device_id'] = event_device_id + if event_id: + event_dict['event_id'] = event_id + if event_location_id: + event_location_dict['event_location_id'] = event_location_id + try: + event_device_obj = Event_Device_Base(**event_device_dict) + except ValidationError as e: + log.error(e.json()) + return False + else: + event_device_obj = event_device_dict_obj + if event_device_id: + # NOTE: Can't update the ID alias if it was never set. + event_device_obj.id = event_device_id + if event_id: + event_device_obj.event_id = event_id + if event_location_id: + event_device_obj.event_location_id = event_location_id + log.debug(event_device_obj) + + event_device_dict = event_device_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'event_cfg', 'event_location', 'account_id', 'account_id_random', 'created_on', 'updated_on'}) + + # ### SECTION ### Process data + if event_device_id: + if event_device_dict_up_result := sql_update(data=event_device_dict, table_name='event_device', rm_id_random=True): pass + else: + log.warning(f'Event Device not updated. Event Device ID: {event_device_id}') + log.debug(event_device_dict_up_result) + return False + log.debug(event_device_dict_up_result) + else: + if event_device_dict_in_result := sql_insert(data=event_device_dict, table_name='event_device', rm_id_random=True, id_random_length=8): pass + else: + log.warning(f'Event Device not created.') + log.debug(event_device_dict_in_result) + return False + log.debug(event_device_dict_in_result) + + event_device_id = event_device_dict_in_result + + event_device_outline = {} + event_device_outline['event_device_id'] = event_device_id + + if return_outline: + log.debug(f'Returning the Event Device Outline: {event_device_outline}') + return event_device_outline + else: + log.debug(f'Returning the Event Device ID: {event_device_id}') + return event_device_id +# ### END ### API Event Device Methods ### create_update_event_device_obj() ### diff --git a/app/methods/event_file_methods.py b/app/methods/event_file_methods.py index 1ef5d8e..46fce55 100644 --- a/app/methods/event_file_methods.py +++ b/app/methods/event_file_methods.py @@ -43,7 +43,7 @@ def load_event_file_obj( enabled: str = 'enabled', # enabled, disabled, all inc_hosted_file: bool = False, ) -> Event_File_Base|dict|bool: - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if event_file_id := redis_lookup_id_random(record_id_random=event_file_id, table_name='event_file'): pass diff --git a/app/routers/event_device.py b/app/routers/event_device.py index 9a3b2b0..0c5edfa 100644 --- a/app/routers/event_device.py +++ b/app/routers/event_device.py @@ -9,7 +9,7 @@ from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, 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 get_event_device_rec_list, load_event_device_obj +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 @@ -19,6 +19,117 @@ 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.DEBUG) # 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 = Query(..., 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.DEBUG) # 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) @@ -30,7 +141,7 @@ async def get_event_device_obj( commons: Common_Route_Params = Depends(common_route_params), ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) # ### SECTION ### Secondary data validation @@ -98,4 +209,4 @@ async def get_event_obj_device_list( 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() ### \ No newline at end of file +# ### END ### API Event Device ### get_event_obj_device_list() ###