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

@@ -23,14 +23,15 @@
##############################################################################
import logging
import os
from tempfile import mkstemp
import cups
from openerp import models, fields, api
from openerp import models, fields, api, _
from openerp.exceptions import Warning
from openerp.tools.config import config
_logger = logging.getLogger(__name__)
CUPS_HOST = config.get('cups_host', 'localhost')
CUPS_PORT = int(config.get('cups_port', 631)) # config.get returns a string
class PrintingPrinter(models.Model):
@@ -63,7 +64,7 @@ class PrintingPrinter(models.Model):
def update_printers_status(self):
printer_recs = self.search([])
try:
connection = cups.Connection()
connection = cups.Connection(CUPS_HOST, CUPS_PORT)
printers = connection.getPrinters()
except:
printer_recs.write({'status': 'server-error'})
@@ -127,10 +128,23 @@ class PrintingPrinter(models.Model):
os.write(fd, content)
finally:
os.close(fd)
connection = cups.Connection()
try:
_logger.debug(
'Starting to connect to CUPS on %s:%s'
% (CUPS_HOST, CUPS_PORT))
connection = cups.Connection(CUPS_HOST, CUPS_PORT)
_logger.debug('Connection to CUPS successfull')
except:
raise Warning(
_("Failed to connect to the CUPS server on %s:%s. "
"Check that the CUPS server is running and that "
"you can reach it from the Odoo server.")
% (CUPS_HOST, CUPS_PORT))
options = self.print_options(report, format)
_logger.debug('Sending job to CUPS printer %s' % self.system_name)
connection.printFile(self.system_name,
file_name,
file_name,