mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
[FIX] Bug #19 avoid crash when using 'raw' option
[Usability] Auto-add Administrator user to the Print group Make XML code more readable base_report_to_printer: add support for remote CUPS server (not just localhost) More logging and better error handling Add CUPS_HOST in more debug logs
This commit is contained in:
committed by
Carlos Roca
parent
84d395cf7c
commit
74ce12bf3f
@@ -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'})
|
||||
@@ -111,7 +112,7 @@ class PrintingPrinter(models.Model):
|
||||
""" Hook to set print options """
|
||||
options = {}
|
||||
if format == 'raw':
|
||||
options['raw'] = True
|
||||
options['raw'] = 'True'
|
||||
return options
|
||||
|
||||
@api.multi
|
||||
@@ -127,15 +128,30 @@ 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 on %s'
|
||||
% (self.system_name, CUPS_HOST))
|
||||
connection.printFile(self.system_name,
|
||||
file_name,
|
||||
file_name,
|
||||
options=options)
|
||||
_logger.info("Printing job: '%s'" % file_name)
|
||||
_logger.info("Printing job: '%s' on %s" % (file_name, CUPS_HOST))
|
||||
return True
|
||||
|
||||
@api.multi
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
<data noupdate="1">
|
||||
<record id="res_groups_printingprintoperator0" model="res.groups">
|
||||
<field name="name">Printing / Print Operator</field>
|
||||
<field name="users" eval="[(4, ref('base.user_root'))]"/>
|
||||
</record>
|
||||
<record id="ir_model_access_printingprintergroup1" model="ir.model.access">
|
||||
<field name="name">printing_printer group</field>
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user