base_report_to_printer: add support for remote CUPS server (not just localhost)

More logging and better error handling
This commit is contained in:
Alexis de Lattre
2015-01-08 21:24:00 +01:00
committed by Sylvain GARANCHER
parent 656939d38e
commit 35dff1b11c
2 changed files with 38 additions and 9 deletions

View File

@@ -22,8 +22,14 @@
##############################################################################
import cups
from openerp.exceptions import Warning
from openerp import models, api, _
from openerp.tools.config import config
import logging
from openerp import models, api
_logger = logging.getLogger(__name__)
CUPS_HOST = config.get('cups_host', 'localhost')
CUPS_PORT = int(config.get('cups_port', 631))
class PrintingPrinterUpdateWizard(models.TransientModel):
@@ -35,16 +41,22 @@ class PrintingPrinterUpdateWizard(models.TransientModel):
# Update Printers
printer_obj = self.env['printing.printer']
try:
connection = cups.Connection()
_logger.info('Trying to get list of printers')
connection = cups.Connection(CUPS_HOST, CUPS_PORT)
printers = connection.getPrinters()
_logger.info('Printers found: %s' % ','.join(printers.keys()))
except:
return {}
raise Warning(
_('Could not get the list of printers from the CUPS server '
'(%s:%s)') % (CUPS_HOST, CUPS_PORT))
printer_recs = printer_obj.search(
[('system_name', 'in', printers.keys())]
)
for printer in printer_recs:
del printers[printer.system_name]
_logger.info(
'Printer %s was already created' % printer.system_name)
for name, printer in printers.iteritems():
values = {
@@ -55,6 +67,9 @@ class PrintingPrinterUpdateWizard(models.TransientModel):
'uri': printer.get('device-uri', False),
}
self.env['printing.printer'].create(values)
_logger.info(
'Created new printer %s with URI %s'
% (values['name'], values['uri']))
return {
'name': 'Printers',