diff --git a/printer_tray/README.rst b/printer_tray/README.rst index 81464d2..58e2d6a 100644 --- a/printer_tray/README.rst +++ b/printer_tray/README.rst @@ -47,7 +47,7 @@ default tray setup on the CUPS server is used. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/144/9.0 + :target: https://runbot.odoo-community.org/runbot/144/10.0 Known issues / Roadmap ====================== diff --git a/printer_tray/__manifest__.py b/printer_tray/__manifest__.py index de65b0a..9e5e5be 100644 --- a/printer_tray/__manifest__.py +++ b/printer_tray/__manifest__.py @@ -4,7 +4,7 @@ { 'name': 'Report to printer - Paper tray selection', - 'version': '9.0.1.0.0', + 'version': '10.0.1.0.0', 'category': 'Printer', 'author': "Camptocamp, Odoo Community Association (OCA)", 'maintainer': 'Camptocamp', diff --git a/printer_tray/models/ir_actions_report_xml.py b/printer_tray/models/ir_actions_report_xml.py index d4d85cb..042b197 100644 --- a/printer_tray/models/ir_actions_report_xml.py +++ b/printer_tray/models/ir_actions_report_xml.py @@ -2,7 +2,7 @@ # Copyright (C) 2013-2014 Camptocamp () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields, api +from odoo import api, fields, models class IrActionsReportXml(models.Model): diff --git a/printer_tray/models/printing_printer.py b/printer_tray/models/printing_printer.py index 437de14..77cf5cd 100644 --- a/printer_tray/models/printing_printer.py +++ b/printer_tray/models/printing_printer.py @@ -6,7 +6,7 @@ import errno import logging import os -from openerp import models, fields, api +from odoo import api, fields, models _logger = logging.getLogger(__name__) @@ -40,30 +40,33 @@ class PrintingPrinter(models.Model): try: os.unlink(ppd_path) except OSError as err: - if err.errno == errno.ENOENT: - pass - raise + # ENOENT means No such file or directory + # The file has already been deleted, we can continue the update + if err.errno != errno.ENOENT: + raise if not option: return vals - vals_trays = [] + vals['tray_ids'] = [] + cups_trays = { + tray_option['choice']: tray_option['text'] + for tray_option in option.choices + } - tray_names = set(tray.system_name for tray in self.tray_ids) - for tray_option in option.choices: - if tray_option['choice'] not in tray_names: - tray_vals = { - 'name': tray_option['text'], - 'system_name': tray_option['choice'], - } - vals_trays.append((0, 0, tray_vals)) + # Add new trays + vals['tray_ids'].extend([ + (0, 0, {'name': text, 'system_name': choice}) + for choice, text in cups_trays.items() + if choice not in self.tray_ids.mapped('system_name') + ]) - cups_trays = set(tray_option['choice'] for tray_option - in option.choices) - for tray in self.tray_ids: - if tray.system_name not in cups_trays: - vals_trays.append((2, tray.id)) + # Remove deleted trays + vals['tray_ids'].extend([ + (2, tray.id) + for tray in self.tray_ids.filtered( + lambda record: record.system_name not in cups_trays.keys()) + ]) - vals['tray_ids'] = vals_trays return vals @api.multi @@ -72,13 +75,13 @@ class PrintingPrinter(models.Model): printing_act_obj = self.env['printing.report.xml.action'] options = super(PrintingPrinter, self).print_options(report, format) - # Retrieve user default values - tray = self.env.user.printer_tray_id - if report is not None: # Retrieve report default values 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([ diff --git a/printer_tray/models/printing_report_xml_action.py b/printer_tray/models/printing_report_xml_action.py index 5cf268e..0716093 100644 --- a/printer_tray/models/printing_report_xml_action.py +++ b/printer_tray/models/printing_report_xml_action.py @@ -2,7 +2,7 @@ # Copyright (C) 2013-2014 Camptocamp () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields, api +from odoo import api, fields, models class PrintingReportXMLAction(models.Model): diff --git a/printer_tray/models/printing_tray.py b/printer_tray/models/printing_tray.py index ef6f245..6f045ba 100644 --- a/printer_tray/models/printing_tray.py +++ b/printer_tray/models/printing_tray.py @@ -2,7 +2,7 @@ # Copyright (C) 2013-2014 Camptocamp () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields +from odoo import fields, models class PrinterTray(models.Model): diff --git a/printer_tray/models/res_users.py b/printer_tray/models/res_users.py index 6069819..bd8732d 100644 --- a/printer_tray/models/res_users.py +++ b/printer_tray/models/res_users.py @@ -2,7 +2,7 @@ # Copyright (C) 2013-2014 Camptocamp () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields, api +from odoo import api, fields, models class ResUsers(models.Model): diff --git a/printer_tray/tests/__init__.py b/printer_tray/tests/__init__.py index 7e74318..481d9b7 100644 --- a/printer_tray/tests/__init__.py +++ b/printer_tray/tests/__init__.py @@ -2,5 +2,8 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import test_ir_actions_report_xml from . import test_printing_printer +from . import test_printing_report_xml_action from . import test_printing_tray +from . import test_res_users diff --git a/printer_tray/tests/test_ir_actions_report_xml.py b/printer_tray/tests/test_ir_actions_report_xml.py new file mode 100644 index 0000000..37daf12 --- /dev/null +++ b/printer_tray/tests/test_ir_actions_report_xml.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 SYLEAM Info Services +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestIrActionsReportXml(TransactionCase): + def test_onchange_printer_tray_id_empty(self): + action = self.env['ir.actions.report.xml'].new( + {'printer_tray_id': False}) + action.onchange_printing_printer_id() + self.assertFalse(action.printer_tray_id) + + def test_onchange_printer_tray_id_not_empty(self): + server = self.env['printing.server'].create({}) + printer = self.env['printing.printer'].create({ + 'name': 'Printer', + 'server_id': server.id, + 'system_name': 'Sys Name', + 'default': True, + 'status': 'unknown', + 'status_message': 'Msg', + 'model': 'res.users', + 'location': 'Location', + 'uri': 'URI', + }) + tray = self.env['printing.tray'].create({ + 'name': 'Tray', + 'system_name': 'TrayName', + 'printer_id': printer.id, + }) + + action = self.env['ir.actions.report.xml'].new( + {'printer_tray_id': tray.id}) + self.assertEqual(action.printer_tray_id, tray) + action.onchange_printing_printer_id() + self.assertFalse(action.printer_tray_id) diff --git a/printer_tray/tests/test_printing_printer.py b/printer_tray/tests/test_printing_printer.py index 445649f..25826db 100644 --- a/printer_tray/tests/test_printing_printer.py +++ b/printer_tray/tests/test_printing_printer.py @@ -2,13 +2,14 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import errno import mock import tempfile -from openerp.tests.common import TransactionCase +from odoo.tests.common import TransactionCase -model = 'openerp.addons.base_report_to_printer.models.printing_printer' -server_model = 'openerp.addons.base_report_to_printer.models.printing_server' +model = 'odoo.addons.base_report_to_printer.models.printing_printer' +server_model = 'odoo.addons.base_report_to_printer.models.printing_server' ppd_header = '*PPD-Adobe: "4.3"' ppd_input_slot_header = """ @@ -205,6 +206,47 @@ class TestPrintingPrinter(TransactionCase): vals = self.printer._prepare_update_from_cups(connection, cups_printer) self.assertFalse('tray_ids' in vals) + @mock.patch('%s.cups' % server_model) + @mock.patch('os.unlink') + def test_prepare_update_from_cups_unlink_error(self, os_unlink, cups): + """ + When OSError other than ENOENT is encountered, the exception is raised + """ + # Break os.unlink + os_unlink.side_effect = OSError(errno.EIO, 'Error') + + self.mock_cups_ppd(cups) + + connection = cups.Connection() + cups_printer = connection.getPrinters() + + with self.assertRaises(OSError): + self.printer._prepare_update_from_cups(connection, cups_printer) + + @mock.patch('%s.cups' % server_model) + @mock.patch('os.unlink') + def test_prepare_update_from_cups_unlink_error_enoent( + self, os_unlink, cups): + """ + When a ENOENT error is encountered, the file has already been unlinked + + This is not an issue, as we were trying to delete the file. + The update can continue. + """ + # Break os.unlink + os_unlink.side_effect = OSError(errno.ENOENT, 'Error') + + self.mock_cups_ppd(cups) + + connection = cups.Connection() + cups_printer = connection.getPrinters() + + vals = self.printer._prepare_update_from_cups(connection, cups_printer) + self.assertEqual(vals['tray_ids'], [(0, 0, { + 'name': 'Auto (Default)', + 'system_name': 'Auto', + })]) + @mock.patch('%s.cups' % server_model) def test_prepare_update_from_cups(self, cups): """ diff --git a/printer_tray/tests/test_printing_report_xml_action.py b/printer_tray/tests/test_printing_report_xml_action.py new file mode 100644 index 0000000..055e0c9 --- /dev/null +++ b/printer_tray/tests/test_printing_report_xml_action.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 SYLEAM Info Services +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestPrintingReportXmlAction(TransactionCase): + def test_onchange_printer_tray_id_empty(self): + action = self.env['printing.report.xml.action'].new( + {'printer_tray_id': False}) + action.onchange_printer_id() + self.assertFalse(action.printer_tray_id) + + def test_onchange_printer_tray_id_not_empty(self): + server = self.env['printing.server'].create({}) + printer = self.env['printing.printer'].create({ + 'name': 'Printer', + 'server_id': server.id, + 'system_name': 'Sys Name', + 'default': True, + 'status': 'unknown', + 'status_message': 'Msg', + 'model': 'res.users', + 'location': 'Location', + 'uri': 'URI', + }) + tray = self.env['printing.tray'].create({ + 'name': 'Tray', + 'system_name': 'TrayName', + 'printer_id': printer.id, + }) + + action = self.env['printing.report.xml.action'].new( + {'printer_tray_id': tray.id}) + self.assertEqual(action.printer_tray_id, tray) + action.onchange_printer_id() + self.assertFalse(action.printer_tray_id) diff --git a/printer_tray/tests/test_printing_tray.py b/printer_tray/tests/test_printing_tray.py index 893a888..753aa48 100644 --- a/printer_tray/tests/test_printing_tray.py +++ b/printer_tray/tests/test_printing_tray.py @@ -2,10 +2,10 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp.tests.common import TransactionCase +from odoo.tests.common import TransactionCase -model = 'openerp.addons.base_report_to_printer.models.printing_server' +model = 'odoo.addons.base_report_to_printer.models.printing_server' class TestPrintingTray(TransactionCase): diff --git a/printer_tray/tests/test_res_users.py b/printer_tray/tests/test_res_users.py new file mode 100644 index 0000000..93978fc --- /dev/null +++ b/printer_tray/tests/test_res_users.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 SYLEAM Info Services +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestResUsers(TransactionCase): + def test_onchange_printer_tray_id_empty(self): + user = self.env['res.users'].new( + {'printer_tray_id': False}) + user.onchange_printing_printer_id() + self.assertFalse(user.printer_tray_id) + + def test_onchange_printer_tray_id_not_empty(self): + server = self.env['printing.server'].create({}) + printer = self.env['printing.printer'].create({ + 'name': 'Printer', + 'server_id': server.id, + 'system_name': 'Sys Name', + 'default': True, + 'status': 'unknown', + 'status_message': 'Msg', + 'model': 'res.users', + 'location': 'Location', + 'uri': 'URI', + }) + tray = self.env['printing.tray'].create({ + 'name': 'Tray', + 'system_name': 'TrayName', + 'printer_id': printer.id, + }) + + user = self.env['res.users'].new( + {'printer_tray_id': tray.id}) + self.assertEqual(user.printer_tray_id, tray) + user.onchange_printing_printer_id() + self.assertFalse(user.printer_tray_id)