diff --git a/base_report_to_printer/__init__.py b/base_report_to_printer/__init__.py index 9f6441d..50da173 100644 --- a/base_report_to_printer/__init__.py +++ b/base_report_to_printer/__init__.py @@ -23,6 +23,7 @@ # ############################################################################## from . import printing +from . import report from . import report_xml_action from . import report_service from . import users diff --git a/base_report_to_printer/__openerp__.py b/base_report_to_printer/__openerp__.py index c7fadbb..792b506 100644 --- a/base_report_to_printer/__openerp__.py +++ b/base_report_to_printer/__openerp__.py @@ -30,6 +30,7 @@ 'website': 'http://www.agilebg.com', 'license': 'AGPL-3', "depends": ['base', + 'report', ], 'data': [ 'security/security.xml', diff --git a/base_report_to_printer/ir_report.py b/base_report_to_printer/ir_report.py index 33b3c64..4b61054 100644 --- a/base_report_to_printer/ir_report.py +++ b/base_report_to_printer/ir_report.py @@ -21,13 +21,7 @@ # along with this program. If not, see . # ############################################################################## -import base64 import logging -import os - -from tempfile import mkstemp - -import cups from openerp import models, fields, api @@ -58,39 +52,6 @@ class ReportXml(models.Model): 'user basis' ) - @api.multi - def set_print_options(self, format): - """ Hook to set print options """ - options = {} - if format == 'raw': - options['raw'] = True - return options - - @api.multi - def print_direct(self, result, format, printer): - self.ensure_one() - fd, file_name = mkstemp() - try: - os.write(fd, base64.decodestring(result)) - finally: - os.close(fd) - printer_system_name = '' - if printer: - if isinstance(printer, (basestring)): - printer_system_name = printer - else: - printer_system_name = printer.system_name - connection = cups.Connection() - - options = self.set_print_options(format) - - connection.printFile(printer_system_name, - file_name, - file_name, - options=options) - _logger.info("Printing job: '%s'" % file_name) - return True - @api.multi def behaviour(self): result = {} diff --git a/base_report_to_printer/printing.py b/base_report_to_printer/printing.py index efa3087..82ae034 100644 --- a/base_report_to_printer/printing.py +++ b/base_report_to_printer/printing.py @@ -22,9 +22,11 @@ # ############################################################################## import logging +import os from contextlib import contextmanager from datetime import datetime +from tempfile import mkstemp from threading import Thread import cups @@ -221,6 +223,37 @@ class PrintingPrinter(models.Model): location = fields.Char(readonly=True) uri = fields.Char(readonly=True) + def print_options(self, format): + """ Hook to set print options """ + options = {} + if format == 'raw': + options['raw'] = True + return options + + @api.multi + def print_document(self, content, format): + """ Print a file + + Format could be pdf, qweb-pdf, raw, ... + + """ + self.ensure_one() + fd, file_name = mkstemp() + try: + os.write(fd, content) + finally: + os.close(fd) + connection = cups.Connection() + + options = self.print_options(format) + + connection.printFile(self.system_name, + file_name, + file_name, + options=options) + _logger.info("Printing job: '%s'" % file_name) + return True + @api.model def start_printer_update(self): polling_obj = self.env['printing.printer.polling'] diff --git a/base_report_to_printer/report.py b/base_report_to_printer/report.py new file mode 100644 index 0000000..493ae08 --- /dev/null +++ b/base_report_to_printer/report.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author: Guewen Baconnier +# Copyright 2014 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp import models + + +class Report(models.Model): + _inherit = 'report' + + def get_pdf(self, cr, uid, ids, report_name, html=None, + data=None, context=None): + result = super(Report, self).get_pdf(cr, uid, ids, report_name, + html=html, data=data, + context=context) + report = self._get_report_from_name(cr, uid, report_name) + data = report.behaviour()[report.id] + action = data['action'] + printer = data['printer'] + if action != 'client' and result: + printer.print_document(result, report.report_type) + return result