mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
Migrate ir_report.py to new API
This commit is contained in:
committed by
Sylvain GARANCHER
parent
d02f5e616c
commit
f46ad66960
@@ -5,8 +5,7 @@
|
|||||||
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
|
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
|
||||||
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
|
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
|
||||||
# Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
|
# Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
|
||||||
# Copyright (C) 2013 Camptocamp (<http://www.camptocamp.com>)
|
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
|
||||||
# All Rights Reserved
|
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -22,30 +21,54 @@
|
|||||||
# 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 os
|
|
||||||
import base64
|
import base64
|
||||||
from tempfile import mkstemp
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from tempfile import mkstemp
|
||||||
|
|
||||||
import cups
|
import cups
|
||||||
|
|
||||||
from openerp.osv import orm, fields
|
from openerp import models, fields, api
|
||||||
|
|
||||||
|
_logger = logging.getLogger('base_report_to_printer')
|
||||||
|
|
||||||
|
|
||||||
class report_xml(orm.Model):
|
class ReportXml(models.Model):
|
||||||
"""
|
"""
|
||||||
Reports
|
Reports
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def set_print_options(self, cr, uid, report_id, format, context=None):
|
_inherit = 'ir.actions.report.xml'
|
||||||
"""
|
|
||||||
Hook to set print options
|
property_printing_action = fields.Many2one(
|
||||||
"""
|
comodel_name='printing.action',
|
||||||
|
string='Action',
|
||||||
|
company_dependent=True,
|
||||||
|
)
|
||||||
|
printing_printer_id = fields.Many2one(
|
||||||
|
comodel_name='printing.printer',
|
||||||
|
string='Printer'
|
||||||
|
)
|
||||||
|
printing_action_ids = fields.One2many(
|
||||||
|
comodel_name='printing.report.xml.action',
|
||||||
|
inverse_name='report_id',
|
||||||
|
string='Actions',
|
||||||
|
help='This field allows configuring action and printer on a per '
|
||||||
|
'user basis'
|
||||||
|
)
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def set_print_options(self, format):
|
||||||
|
""" Hook to set print options """
|
||||||
options = {}
|
options = {}
|
||||||
if format == 'raw':
|
if format == 'raw':
|
||||||
options['raw'] = True
|
options['raw'] = True
|
||||||
return options
|
return options
|
||||||
|
|
||||||
def print_direct(self, cr, uid, report_id, result, format, printer, context=None):
|
@api.multi
|
||||||
|
def print_direct(self, result, format, printer):
|
||||||
|
self.ensure_one()
|
||||||
fd, file_name = mkstemp()
|
fd, file_name = mkstemp()
|
||||||
try:
|
try:
|
||||||
os.write(fd, base64.decodestring(result))
|
os.write(fd, base64.decodestring(result))
|
||||||
@@ -59,74 +82,56 @@ class report_xml(orm.Model):
|
|||||||
printer_system_name = printer.system_name
|
printer_system_name = printer.system_name
|
||||||
connection = cups.Connection()
|
connection = cups.Connection()
|
||||||
|
|
||||||
options = self.set_print_options(cr, uid, report_id, format, context=context)
|
options = self.set_print_options(format)
|
||||||
|
|
||||||
connection.printFile(printer_system_name, file_name, file_name, options=options)
|
connection.printFile(printer_system_name,
|
||||||
logger = logging.getLogger('base_report_to_printer')
|
file_name,
|
||||||
logger.info("Printing job : '%s'" % file_name)
|
file_name,
|
||||||
|
options=options)
|
||||||
|
_logger.info("Printing job: '%s'" % file_name)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
_inherit = 'ir.actions.report.xml'
|
@api.multi
|
||||||
_columns = {
|
def behaviour(self):
|
||||||
'property_printing_action': fields.property(
|
|
||||||
#'ir.actions.report.xml',
|
|
||||||
'printing.action',
|
|
||||||
type='many2one',
|
|
||||||
relation='printing.action',
|
|
||||||
string='Action',
|
|
||||||
view_load=True,
|
|
||||||
method=True,
|
|
||||||
),
|
|
||||||
'printing_printer_id': fields.many2one('printing.printer', 'Printer'),
|
|
||||||
'printing_action_ids': fields.one2many(
|
|
||||||
'printing.report.xml.action', 'report_id', 'Actions',
|
|
||||||
help='This field allows configuring action and printer on a per '
|
|
||||||
'user basis'),
|
|
||||||
}
|
|
||||||
|
|
||||||
def behaviour(self, cr, uid, ids, context=None):
|
|
||||||
result = {}
|
result = {}
|
||||||
printer_obj = self.pool['printing.printer']
|
printer_obj = self.env['printing.printer']
|
||||||
printing_act_obj = self.pool['printing.report.xml.action']
|
printing_act_obj = self.env['printing.report.xml.action']
|
||||||
# Set hardcoded default action
|
# Set hardcoded default action
|
||||||
default_action = 'client'
|
default_action = 'client'
|
||||||
# Retrieve system wide printer
|
# Retrieve system wide printer
|
||||||
default_printer = printer_obj.get_default(cr, uid, context=context)
|
default_printer = printer_obj.get_default()
|
||||||
if default_printer:
|
|
||||||
default_printer = printer_obj.browse(cr, uid, default_printer, context=context)
|
|
||||||
|
|
||||||
# Retrieve user default values
|
# Retrieve user default values
|
||||||
user = self.pool['res.users'].browse(cr, uid, uid, context=context)
|
user = self.env.user
|
||||||
if user.printing_action:
|
if user.printing_action:
|
||||||
default_action = user.printing_action
|
default_action = user.printing_action
|
||||||
if user.printing_printer_id:
|
if user.printing_printer_id:
|
||||||
default_printer = user.printing_printer_id
|
default_printer = user.printing_printer_id
|
||||||
|
|
||||||
for report in self.browse(cr, uid, ids, context):
|
for report in self:
|
||||||
action = default_action
|
action = default_action
|
||||||
printer = default_printer
|
printer = default_printer
|
||||||
|
|
||||||
# Retrieve report default values
|
# Retrieve report default values
|
||||||
if (report.property_printing_action
|
report_action = report.property_printing_action
|
||||||
and report.property_printing_action.type != 'user_default'):
|
if report_action and report_action.type != 'user_default':
|
||||||
action = report.property_printing_action.type
|
action = report_action.type
|
||||||
if report.printing_printer_id:
|
if report.printing_printer_id:
|
||||||
printer = report.printing_printer_id
|
printer = report.printing_printer_id
|
||||||
|
|
||||||
# Retrieve report-user specific values
|
# Retrieve report-user specific values
|
||||||
act_ids = printing_act_obj.search(
|
print_action = printing_act_obj.search(
|
||||||
cr, uid,
|
|
||||||
[('report_id', '=', report.id),
|
[('report_id', '=', report.id),
|
||||||
('user_id', '=', uid),
|
('user_id', '=', self.env.uid),
|
||||||
('action', '!=', 'user_default')], context=context)
|
('action', '!=', 'user_default')],
|
||||||
if act_ids:
|
limit=1)
|
||||||
user_action = printing_act_obj.behaviour(cr, uid, act_ids[0], context)
|
if print_action:
|
||||||
|
user_action = print_action.behaviour()
|
||||||
action = user_action['action']
|
action = user_action['action']
|
||||||
if user_action['printer']:
|
if user_action['printer']:
|
||||||
printer = user_action['printer']
|
printer = user_action['printer']
|
||||||
|
|
||||||
result[report.id] = {
|
result[report.id] = {'action': action,
|
||||||
'action': action,
|
|
||||||
'printer': printer,
|
'printer': printer,
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user