From caa9edb4e8773833c6aed401930f3a8a980a9aad Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Mon, 17 Nov 2014 16:30:32 +0100 Subject: [PATCH] Extract a method so it will be easier to override in printer_tray --- base_report_to_printer/printing.py | 72 +++++++++++++++++------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/base_report_to_printer/printing.py b/base_report_to_printer/printing.py index e1be945..3a9d58c 100644 --- a/base_report_to_printer/printing.py +++ b/base_report_to_printer/printing.py @@ -179,42 +179,23 @@ class PrintingPrinterPolling(models.Model): with self.start_update() as locked: if not locked: return # could not obtain lock + + printer_recs = printer_obj.search([]) + try: connection = cups.Connection() printers = connection.getPrinters() - server_error = False except: - server_error = True - - mapping = { - 3: 'available', - 4: 'printing', - 5: 'error' - } - - # Skip update to avoid the thread being created again - printer_recs = printer_obj.search([]) - for printer in printer_recs: - vals = {} - if server_error: - status = 'server-error' - elif printer.system_name in printers: - info = printers[printer.system_name] - status = mapping.get(info['printer-state'], - 'unknown') - vals = { - 'model': info.get('printer-make-and-model', - False), - 'location': info.get('printer-location', - False), - 'uri': info.get('device-uri', - False), - } - else: - status = 'unavailable' - - vals['status'] = status - printer.write(vals) + printer_recs.write({'status': 'server-error'}) + else: + for printer in printer_recs: + cups_printer = printers.get(printer.system_name) + if cups_printer: + printer.update_from_cups(connection, + cups_printer) + else: + # not in cups list + printer.status = 'unavailable' self.env.cr.commit() except: @@ -250,6 +231,33 @@ class PrintingPrinter(models.Model): location = fields.Char(readonly=True) uri = fields.Char(string='URI', readonly=True) + @api.multi + def _prepare_update_from_cups(self, cups_connection, cups_printer): + mapping = { + 3: 'available', + 4: 'printing', + 5: 'error' + } + vals = { + 'model': cups_printer.get('printer-make-and-model', False), + 'location': cups_printer.get('printer-location', False), + 'uri': cups_printer.get('device-uri', False), + 'status': mapping.get(cups_printer['printer-state'], 'unknown'), + } + return vals + + @api.multi + def update_from_cups(self, cups_connection, cups_printer): + """ Update a printer from the information returned by cups. + + :param cups_connection: connection to CUPS, may be used when the + method is overriden (e.g. in printer_tray) + :param cups_printer: dict of information returned by CUPS for the + current printer + """ + vals = self._prepare_update_from_cups(cups_connection, cups_printer) + self.write(vals) + @api.multi def print_options(self, report, format): """ Hook to set print options """