[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) <qle@odoo.com>
This commit is contained in:
Julien Segers (juse)
2024-02-08 15:07:58 +00:00
parent 67521df040
commit b2379f12d7
2 changed files with 8 additions and 3 deletions
@@ -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'])
@@ -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