[REF] Refactor the way print options are defined and sent.

This commit is contained in:
Graeme Gellatly
2017-10-08 19:41:57 +13:00
parent f70720a410
commit 4375c7bc2f
2 changed files with 79 additions and 80 deletions

View File

@@ -48,7 +48,7 @@ class IrActionsReport(models.Model):
report = self._get_report_from_name(report_name) report = self._get_report_from_name(report_name)
if not report: if not report:
return {} return {}
result = report.behaviour()[report] result = report.behaviour()
serializable_result = { serializable_result = {
'action': result['action'], 'action': result['action'],
'printer_name': result['printer'].name, 'printer_name': result['printer'].name,
@@ -56,64 +56,63 @@ class IrActionsReport(models.Model):
return serializable_result return serializable_result
@api.multi @api.multi
def behaviour(self): def _get_user_default_print_behaviour(self):
result = {}
printer_obj = self.env['printing.printer'] printer_obj = self.env['printing.printer']
printing_act_obj = self.env['printing.report.xml.action']
# Set hardcoded default action
default_action = 'client'
# Retrieve system wide printer
default_printer = printer_obj.get_default()
# Retrieve user default values
user = self.env.user user = self.env.user
if user.printing_action: return dict(
default_action = user.printing_action action=user.printing_action or 'client',
if user.printing_printer_id: printer=user.printing_printer_id or printer_obj.get_default(),
default_printer = user.printing_printer_id tray=str(user.printer_tray_id.system_name) if
user.printer_tray_id else False
)
for report in self: @api.multi
action = default_action def _get_report_default_print_behaviour(self):
printer = default_printer result = {}
report_action = self.property_printing_action_id
if report_action and report_action.action_type != 'user_default':
result['action'] = report_action.action_type
if 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
# Retrieve report default values @api.multi
report_action = report.property_printing_action_id def behaviour(self):
if report_action and report_action.action_type != 'user_default': self.ensure_one()
action = report_action.action_type printing_act_obj = self.env['printing.report.xml.action']
if report.printing_printer_id:
printer = report.printing_printer_id
# Retrieve report-user specific values result = self._get_user_default_print_behaviour()
print_action = printing_act_obj.search([ result.update(self._get_report_default_print_behaviour())
('report_id', '=', report.id),
('user_id', '=', self.env.uid),
('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']
result[report] = { # Retrieve report-user specific values
'action': action, print_action = printing_act_obj.search([
'printer': printer, ('report_id', '=', self.id),
} ('user_id', '=', self.env.uid),
('action', '!=', 'user_default'),
], limit=1)
if print_action:
result.update(print_action.behaviour())
return result return result
@api.multi @api.multi
def print_document(self, record_ids, data=None): def print_document(self, record_ids, data=None):
""" Print a document, do not return the document file """ """ 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( must_skip_send_to_printer=True).render_qweb_pdf(
record_ids, data=data) record_ids, data=data)
behaviour = self.behaviour()[self] behaviour = self.behaviour()
printer = behaviour['printer'] printer = behaviour.pop('printer', None)
if not printer: if not printer:
raise exceptions.Warning( raise exceptions.Warning(
_('No printer configured to print this report.') _('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 @api.multi
def _can_print_report(self, behaviour, printer, document): def _can_print_report(self, behaviour, printer, document):
@@ -138,11 +137,12 @@ class IrActionsReport(models.Model):
document, doc_format = super(IrActionsReport, self).render_qweb_pdf( document, doc_format = super(IrActionsReport, self).render_qweb_pdf(
docids, data=data) docids, data=data)
behaviour = self.behaviour()[self] behaviour = self.behaviour()
printer = behaviour['printer'] printer = behaviour.pop('printer', None)
can_print_report = self._can_print_report(behaviour, printer, document) can_print_report = self._can_print_report(behaviour, printer, document)
if can_print_report: 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 return document, doc_format

View File

@@ -118,36 +118,7 @@ class PrintingPrinter(models.Model):
return vals return vals
@api.multi @api.multi
def print_options(self, report=None, format=None, copies=1): def print_document(self, report, content, **print_opts):
""" 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):
""" Print a file """ Print a file
Format could be pdf, qweb-pdf, raw, ... Format could be pdf, qweb-pdf, raw, ...
@@ -161,16 +132,44 @@ class PrintingPrinter(models.Model):
os.close(fd) os.close(fd)
return self.print_file( 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 @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)}
@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 = {}
for option, value in print_opts.items():
try:
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 """ """ Print a file """
self.ensure_one() self.ensure_one()
connection = self.server_id._open_connection(raise_on_error=True) connection = self.server_id._open_connection(raise_on_error=True)
options = self.print_options( options = self.print_options(report=report, **print_opts)
report=report, format=format, copies=copies)
_logger.debug( _logger.debug(
'Sending job to CUPS printer %s on %s' 'Sending job to CUPS printer %s on %s'