diff --git a/framework/python/src/api/api.py b/framework/python/src/api/api.py index e001a4a16..f3c77e6d9 100644 --- a/framework/python/src/api/api.py +++ b/framework/python/src/api/api.py @@ -449,8 +449,21 @@ async def save_device(self, request: Request, response: Response): response.status_code = status.HTTP_400_BAD_REQUEST return self._generate_msg(False, "Invalid request received") + # Check if device with same MAC exists device = self._session.get_device(device_json.get(DEVICE_MAC_ADDR_KEY)) + if device is not None: + + response.status_code = status.HTTP_409_CONFLICT + return self._generate_msg( + False, "A device with that MAC address already exists") + + # Check if device with same manufacturer and model exists + device = self._session.get_device_by_make_and_model( + device_json.get(DEVICE_MANUFACTURER_KEY), + device_json.get(DEVICE_MODEL_KEY) + ) + if device is None: # Create new device @@ -468,7 +481,7 @@ async def save_device(self, request: Request, response: Response): response.status_code = status.HTTP_409_CONFLICT return self._generate_msg( - False, "A device with that " + "MAC address already exists") + False, "A device with that manufacturer and model already exists") return device.to_config_json() @@ -543,14 +556,17 @@ async def get_report(self, response: Response, device_name, timestamp): device = self._session.get_device_by_name(device_name) # 1.3 file path - file_path = os.path.join(DEVICES_PATH, device_name, "reports", timestamp,'test', - device.mac_addr.replace(':',''), + file_path = os.path.join( + DEVICES_PATH, + device_name, + "reports", + timestamp,"test", + device.mac_addr.replace(":",""), "report.pdf") if not os.path.isfile(file_path): # pre 1.3 file path file_path = os.path.join(DEVICES_PATH, device_name, "reports", timestamp, "report.pdf") - LOGGER.debug(f"Received get report request for {device_name} / {timestamp}") if os.path.isfile(file_path): diff --git a/framework/python/src/common/session.py b/framework/python/src/common/session.py index f39fd9148..b5c8e336a 100644 --- a/framework/python/src/common/session.py +++ b/framework/python/src/common/session.py @@ -268,6 +268,11 @@ def get_device_by_name(self, device_name): return device return None + def get_device_by_make_and_model(self, make, model): + for device in self._device_repository: + if device.manufacturer == make and device.model == model: + return device + def get_device_repository(self): return self._device_repository