Skip to content

Commit

Permalink
Merge branch '11.0-refactor-options' into 11.0-printer-tray
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
gdgellatly committed Dec 6, 2017
2 parents 21371f1 + b63eef0 commit cc8e61d
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 153 deletions.
59 changes: 39 additions & 20 deletions base_report_to_printer/models/ir_actions_report.py
Original file line number Diff line number Diff line change
@@ -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>)
Expand Down Expand Up @@ -55,21 +56,35 @@ def print_action_for_report_name(self, report_name):
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([
Expand All @@ -78,26 +93,29 @@ def behaviour(self):
('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):
Expand All @@ -123,10 +141,11 @@ def render_qweb_pdf(self, docids, data=None):
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
74 changes: 39 additions & 35 deletions base_report_to_printer/models/printing_printer.py
Original file line number Diff line number Diff line change
@@ -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>)
Expand Down Expand Up @@ -117,36 +118,7 @@ def _prepare_update_from_cups(self, cups_connection, cups_printer):
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, ...
Expand All @@ -160,16 +132,48 @@ def print_document(self, report, content, format, copies=1):
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'
Expand Down
119 changes: 103 additions & 16 deletions base_report_to_printer/tests/test_ir_actions_report.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc.
# Copyright 2016 SYLEAM
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
Expand Down Expand Up @@ -43,6 +44,12 @@ def new_printer(self):
'uri': 'URI',
})

def new_tray(self, vals=None, defaults=None):
values = dict(defaults)
if vals is not None:
values.update(vals)
return self.env['printing.tray'].create(values)

def test_print_action_for_report_name_gets_report(self):
""" It should get report by name """
with mock.patch.object(self.Model, '_get_report_from_name') as mk:
Expand Down Expand Up @@ -86,7 +93,9 @@ def test_behaviour_default_values(self):
self.assertEqual(report.behaviour(), {
'action': 'client',
'printer': self.env['printing.printer'],
})
'tray': False,
},
)

def test_behaviour_user_values(self):
""" It should return the action and printer from user """
Expand All @@ -96,7 +105,9 @@ def test_behaviour_user_values(self):
self.assertEqual(report.behaviour(), {
'action': 'client',
'printer': self.env.user.printing_printer_id,
})
'tray': False,
},
)

def test_behaviour_report_values(self):
""" It should return the action and printer from report """
Expand All @@ -105,19 +116,23 @@ def test_behaviour_report_values(self):
report.property_printing_action_id = self.new_action()
report.printing_printer_id = self.new_printer()
self.assertEqual(report.behaviour(), {
'action': report.property_printing_action_id.action_type,
'printer': report.printing_printer_id,
})
'action': report.property_printing_action_id.action_type,
'printer': report.printing_printer_id,
'tray': False,
},
)

def test_behaviour_user_action(self):
""" It should return the action and printer from user action"""
report = self.Model.search([], limit=1)
self.env.user.printing_action = 'client'
report.property_printing_action_id.action_type = 'user_default'
self.assertEqual(report.behaviour(), {
'action': 'client',
'printer': report.printing_printer_id,
})
'action': 'client',
'printer': report.printing_printer_id,
'tray': False,
},
)

def test_behaviour_printing_action_on_wrong_user(self):
""" It should return the action and printer ignoring printing action
Expand All @@ -131,7 +146,9 @@ def test_behaviour_printing_action_on_wrong_user(self):
self.assertEqual(report.behaviour(), {
'action': 'client',
'printer': report.printing_printer_id,
})
'tray': False,
},
)

def test_behaviour_printing_action_on_wrong_report(self):
""" It should return the action and printer ignoring printing action
Expand All @@ -146,7 +163,9 @@ def test_behaviour_printing_action_on_wrong_report(self):
self.assertEqual(report.behaviour(), {
'action': 'client',
'printer': report.printing_printer_id,
})
'tray': False,
},
)

def test_behaviour_printing_action_with_no_printer(self):
""" It should return the action from printing action and printer from other
Expand All @@ -157,9 +176,11 @@ def test_behaviour_printing_action_with_no_printer(self):
printing_action.user_id = self.env.user
printing_action.report_id = report
self.assertEqual(report.behaviour(), {
'action': printing_action.action,
'printer': report.printing_printer_id,
})
'action': printing_action.action,
'printer': report.printing_printer_id,
'tray': False,
},
)

def test_behaviour_printing_action_with_printer(self):
""" It should return the action and printer from printing action """
Expand All @@ -171,7 +192,9 @@ def test_behaviour_printing_action_with_printer(self):
self.assertEqual(report.behaviour(), {
'action': printing_action.action,
'printer': printing_action.printer_id,
})
'tray': False,
},
)

def test_behaviour_printing_action_user_defaults(self):
""" It should return the action and printer from user with printing action
Expand All @@ -182,9 +205,73 @@ def test_behaviour_printing_action_user_defaults(self):
printing_action.user_id = self.env.user
printing_action.action = 'user_default'
self.assertEqual(report.behaviour(), {
'action': 'client',
'printer': report.printing_printer_id,
'action': 'client',
'printer': report.printing_printer_id,
'tray': False,
},
)

def test_print_tray_behaviour(self):
"""
It should return the correct tray
"""
report = self.env['ir.actions.report'].search([], limit=1)
action = self.env['printing.report.xml.action'].create({
'user_id': self.env.user.id,
'report_id': report.id,
'action': 'server',
})
printer = self.new_printer()
tray_vals = {
'name': 'Tray',
'system_name': 'Tray',
'printer_id': printer.id,
}
user_tray = self.new_tray({'system_name': 'User tray'}, tray_vals)
report_tray = self.new_tray({'system_name': 'Report tray'}, tray_vals)
action_tray = self.new_tray({'system_name': 'Action tray'}, tray_vals)

# No report passed
self.env.user.printer_tray_id = False
options = printer.print_options()
self.assertFalse('InputSlot' in options)

# No tray defined
self.env.user.printer_tray_id = False
report.printer_tray_id = False
action.printer_tray_id = False
options = report.behaviour()
self.assertTrue('tray' in options)

# Only user tray is defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = False
action.printer_tray_id = False
self.assertEqual('User tray', report.behaviour()['tray'])

# Only report tray is defined
self.env.user.printer_tray_id = False
report.printer_tray_id = report_tray
action.printer_tray_id = False
self.assertEqual('Report tray', report.behaviour()['tray'])

# Only action tray is defined
self.env.user.printer_tray_id = False
report.printer_tray_id = False
action.printer_tray_id = action_tray
self.assertEqual('Action tray', report.behaviour()['tray'])

# User and report tray defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = report_tray
action.printer_tray_id = False
self.assertEqual('Report tray', report.behaviour()['tray'])

# All trays are defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = report_tray
action.printer_tray_id = action_tray
self.assertEqual('Action tray', report.behaviour()['tray'])

def test_onchange_printer_tray_id_empty(self):
action = self.env['ir.actions.report'].new(
Expand Down
Loading

0 comments on commit cc8e61d

Please sign in to comment.