mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
[REF] Refactor the way print options are defined and sent.
This commit is contained in:
@@ -48,7 +48,7 @@ class IrActionsReport(models.Model):
|
||||
report = self._get_report_from_name(report_name)
|
||||
if not report:
|
||||
return {}
|
||||
result = report.behaviour()[report]
|
||||
result = report.behaviour()
|
||||
serializable_result = {
|
||||
'action': result['action'],
|
||||
'printer_name': result['printer'].name,
|
||||
@@ -56,64 +56,63 @@ class IrActionsReport(models.Model):
|
||||
return serializable_result
|
||||
|
||||
@api.multi
|
||||
def behaviour(self):
|
||||
result = {}
|
||||
def _get_user_default_print_behaviour(self):
|
||||
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
|
||||
if user.printing_action:
|
||||
default_action = user.printing_action
|
||||
if user.printing_printer_id:
|
||||
default_printer = user.printing_printer_id
|
||||
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
|
||||
)
|
||||
|
||||
for report in self:
|
||||
action = default_action
|
||||
printer = default_printer
|
||||
@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':
|
||||
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
|
||||
report_action = report.property_printing_action_id
|
||||
if report_action and report_action.action_type != 'user_default':
|
||||
action = report_action.action_type
|
||||
if report.printing_printer_id:
|
||||
printer = report.printing_printer_id
|
||||
@api.multi
|
||||
def behaviour(self):
|
||||
self.ensure_one()
|
||||
printing_act_obj = self.env['printing.report.xml.action']
|
||||
|
||||
# Retrieve report-user specific values
|
||||
print_action = printing_act_obj.search([
|
||||
('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 = self._get_user_default_print_behaviour()
|
||||
result.update(self._get_report_default_print_behaviour())
|
||||
|
||||
result[report] = {
|
||||
'action': action,
|
||||
'printer': printer,
|
||||
}
|
||||
# Retrieve report-user specific values
|
||||
print_action = printing_act_obj.search([
|
||||
('report_id', '=', self.id),
|
||||
('user_id', '=', self.env.uid),
|
||||
('action', '!=', 'user_default'),
|
||||
], limit=1)
|
||||
if print_action:
|
||||
result.update(print_action.behaviour())
|
||||
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()[self]
|
||||
printer = behaviour['printer']
|
||||
behaviour = self.behaviour()
|
||||
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):
|
||||
@@ -138,11 +137,12 @@ class IrActionsReport(models.Model):
|
||||
document, doc_format = super(IrActionsReport, self).render_qweb_pdf(
|
||||
docids, data=data)
|
||||
|
||||
behaviour = self.behaviour()[self]
|
||||
printer = behaviour['printer']
|
||||
behaviour = self.behaviour()
|
||||
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
|
||||
|
||||
@@ -118,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, ...
|
||||
@@ -161,16 +132,44 @@ 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)}
|
||||
|
||||
@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 """
|
||||
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'
|
||||
|
||||
Reference in New Issue
Block a user