Implements the print on the new 'report' model

This commit is contained in:
Guewen Baconnier
2014-11-17 10:41:35 +01:00
parent 838c6693f5
commit aa22f62bbb
5 changed files with 74 additions and 39 deletions

View File

@@ -23,6 +23,7 @@
# #
############################################################################## ##############################################################################
from . import printing from . import printing
from . import report
from . import report_xml_action from . import report_xml_action
from . import report_service from . import report_service
from . import users from . import users

View File

@@ -30,6 +30,7 @@
'website': 'http://www.agilebg.com', 'website': 'http://www.agilebg.com',
'license': 'AGPL-3', 'license': 'AGPL-3',
"depends": ['base', "depends": ['base',
'report',
], ],
'data': [ 'data': [
'security/security.xml', 'security/security.xml',

View File

@@ -21,13 +21,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
############################################################################## ##############################################################################
import base64
import logging import logging
import os
from tempfile import mkstemp
import cups
from openerp import models, fields, api from openerp import models, fields, api
@@ -58,39 +52,6 @@ class ReportXml(models.Model):
'user basis' '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 @api.multi
def behaviour(self): def behaviour(self):
result = {} result = {}

View File

@@ -22,9 +22,11 @@
# #
############################################################################## ##############################################################################
import logging import logging
import os
from contextlib import contextmanager from contextlib import contextmanager
from datetime import datetime from datetime import datetime
from tempfile import mkstemp
from threading import Thread from threading import Thread
import cups import cups
@@ -221,6 +223,37 @@ class PrintingPrinter(models.Model):
location = fields.Char(readonly=True) location = fields.Char(readonly=True)
uri = 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 @api.model
def start_printer_update(self): def start_printer_update(self):
polling_obj = self.env['printing.printer.polling'] polling_obj = self.env['printing.printer.polling']

View File

@@ -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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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