Merge branch '11.0-refactor-options' into 11.0-printer-tray

# Conflicts:
#	base_report_to_printer/models/ir_actions_report.py
#	base_report_to_printer/models/printing_printer.py
#	base_report_to_printer/tests/test_ir_actions_report.py
#	base_report_to_printer/tests/test_printing_printer.py
#	base_report_to_printer/views/printing_report.xml
#	base_report_to_printer/views/res_users.xml
This commit is contained in:
Graeme Gellatly
2017-12-06 21:43:35 +13:00
6 changed files with 231 additions and 153 deletions

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com>
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
@@ -55,21 +56,35 @@ class IrActionsReport(models.Model):
return serializable_result
@api.multi
def behaviour(self):
self.ensure_one()
def _get_user_default_print_behaviour(self):
printer_obj = self.env['printing.printer']
printing_act_obj = self.env['printing.report.xml.action']
# Retrieve user defaults or system defaults
user = self.env.user
action = user.printing_action or 'client'
printer = user.printing_printer_id or printer_obj.get_default()
return dict(
action=user.printing_action or 'client',
printer=user.printing_printer_id or printer_obj.get_default(),
tray=str(user.printer_tray_id.system_name) if
user.printer_tray_id else False
)
# Retrieve report default values
@api.multi
def _get_report_default_print_behaviour(self):
result = {}
report_action = self.property_printing_action_id
if report_action and report_action.action_type != 'user_default':
action = report_action.action_type
result['action'] = report_action.action_type
if self.printing_printer_id:
printer = self.printing_printer_id
result['printer'] = self.printing_printer_id
if self.printer_tray_id:
result['tray'] = self.printer_tray_id.system_name
return result
@api.multi
def behaviour(self):
self.ensure_one()
printing_act_obj = self.env['printing.report.xml.action']
result = self._get_user_default_print_behaviour()
result.update(self._get_report_default_print_behaviour())
# Retrieve report-user specific values
print_action = printing_act_obj.search([
@@ -78,26 +93,29 @@ class IrActionsReport(models.Model):
('action', '!=', 'user_default'),
], limit=1)
if print_action:
user_action = print_action.behaviour()
action = user_action['action']
if user_action['printer']:
printer = user_action['printer']
return {'action': action, 'printer': printer}
# For some reason design takes report defaults over
# False action entries so we must allow for that here
result.update({k: v for k, v in
print_action.behaviour().items() if v})
return result
@api.multi
def print_document(self, record_ids, data=None):
""" Print a document, do not return the document file """
document = self.with_context(
document, doc_format = self.with_context(
must_skip_send_to_printer=True).render_qweb_pdf(
record_ids, data=data)
behaviour = self.behaviour()
printer = behaviour['printer']
printer = behaviour.pop('printer', None)
if not printer:
raise exceptions.Warning(
_('No printer configured to print this report.')
)
return printer.print_document(self, document, self.report_type)
# TODO should we use doc_format instead of report_type
return printer.print_document(self, document,
doc_format=self.report_type,
**behaviour)
@api.multi
def _can_print_report(self, behaviour, printer, document):
@@ -123,10 +141,11 @@ class IrActionsReport(models.Model):
docids, data=data)
behaviour = self.behaviour()
printer = behaviour['printer']
printer = behaviour.pop('printer', None)
can_print_report = self._can_print_report(behaviour, printer, document)
if can_print_report:
printer.print_document(self, document, self.report_type)
printer.print_document(self, document, doc_format=self.report_type,
**behaviour)
return document, doc_format

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com>
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
@@ -117,36 +118,7 @@ class PrintingPrinter(models.Model):
return vals
@api.multi
def print_options(self, report=None, format=None, copies=1):
""" Hook to set print options """
options = {}
if format == 'raw':
options['raw'] = 'True'
if copies > 1:
options['copies'] = str(copies)
if report is not None:
printing_act_obj = self.env['printing.report.xml.action']
if report.printer_tray_id:
tray = report.printer_tray_id
else:
# Retrieve user default values
tray = self.env.user.printer_tray_id
# Retrieve report-user specific values
action = printing_act_obj.search([
('report_id', '=', report.id),
('user_id', '=', self.env.uid),
('action', '!=', 'user_default'),
], limit=1)
if action.printer_tray_id:
tray = action.printer_tray_id
if tray:
options['InputSlot'] = str(tray.system_name)
return options
@api.multi
def print_document(self, report, content, format, copies=1):
def print_document(self, report, content, **print_opts):
""" Print a file
Format could be pdf, qweb-pdf, raw, ...
@@ -160,16 +132,48 @@ class PrintingPrinter(models.Model):
os.close(fd)
return self.print_file(
file_name, report=report, copies=copies, format=format)
file_name, report=report, **print_opts)
@staticmethod
def _set_option_doc_format(report, value):
return {'raw': 'True'} if value == 'raw' else {}
# Backwards compatibility of builtin used as kwarg
_set_option_format = _set_option_doc_format
@api.multi
def print_file(self, file_name, report=None, copies=1, format=None):
def _set_option_tray(self, report, value):
"""Note we use self here as some older PPD use tray
rather than InputSlot so we may need to query printer in override"""
return {'InputSlot': str(value)} if value else {}
@staticmethod
def _set_option_noop(report, value):
return {}
_set_option_action = _set_option_noop
_set_option_printer = _set_option_noop
@api.multi
def print_options(self, report=None, **print_opts):
options = {}
if not report:
return options
for option, value in print_opts.items():
try:
options.update(getattr(
self, '_set_option_%s' % option)(report, value))
except AttributeError:
options[option] = str(value)
return options
@api.multi
def print_file(self, file_name, report=None, **print_opts):
""" Print a file """
self.ensure_one()
connection = self.server_id._open_connection(raise_on_error=True)
options = self.print_options(
report=report, format=format, copies=copies)
options = self.print_options(report=report, **print_opts)
_logger.debug(
'Sending job to CUPS printer %s on %s'