From b2379f12d715c5f07481d90f5659a88de0c0f4bf Mon Sep 17 00:00:00 2001 From: "Julien Segers (juse)" Date: Tue, 26 Dec 2023 16:52:26 +0100 Subject: [PATCH] [FIX] hw_drivers: inform user on printing failed Currently, the user does not get any feedback if the printer couldn't print. This can happen when the printer identifier is not the same on CUPS and on the IoT box due to this RegEx: https://github.com/odoo/odoo/blob/5b62e301b91a3f39857c2c7f18729e10796ef194/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface.py#L55 After this commit, an error message is displayed in the IoT box logs so the user is informed that the printing failed. Also, the '-' character has been added to the RegEx as it is a commonly used character in printer's identifier as CUPS doesn't allow spaces. opw-3458347 closes odoo/odoo#153148 X-original-commit: 722a7db800b66db2d0b735ced1b9f4074ddac611 Signed-off-by: Quentin Lejeune (qle) --- addons/hw_drivers/iot_handlers/drivers/PrinterDriver_L.py | 4 ++++ .../iot_handlers/interfaces/PrinterInterface_L.py | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/addons/hw_drivers/iot_handlers/drivers/PrinterDriver_L.py b/addons/hw_drivers/iot_handlers/drivers/PrinterDriver_L.py index 0ee38a68a03..c17b30c8e2a 100644 --- a/addons/hw_drivers/iot_handlers/drivers/PrinterDriver_L.py +++ b/addons/hw_drivers/iot_handlers/drivers/PrinterDriver_L.py @@ -164,6 +164,10 @@ class PrinterDriver(Driver): def print_raw(self, data): process = subprocess.Popen(["lp", "-d", self.device_identifier], stdin=subprocess.PIPE) process.communicate(data) + if process.returncode != 0: + # The stderr isn't meaningful so we don't log it ('No such file or directory') + _logger.error('Printing failed: printer with the identifier "%s" could not be found', + self.device_identifier) def print_receipt(self, data): receipt = b64decode(data['receipt']) diff --git a/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface_L.py b/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface_L.py index 4484f0d0c24..994a2d3d5cd 100644 --- a/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface_L.py +++ b/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface_L.py @@ -51,10 +51,11 @@ class PrinterInterface(Interface): return dict(self.printer_devices) def get_identifier(self, path): + allowed_characters = '[^a-zA-Z0-9_-]' if 'uuid=' in path: - identifier = sub('[^a-zA-Z0-9_]', '', path.split('uuid=')[1]) + identifier = sub(allowed_characters, '', path.split('uuid=')[1]) elif 'serial=' in path: - identifier = sub('[^a-zA-Z0-9_]', '', path.split('serial=')[1]) + identifier = sub(allowed_characters, '', path.split('serial=')[1]) else: - identifier = sub('[^a-zA-Z0-9_]', '', path) + identifier = sub(allowed_characters, '', path) return identifier