[MIG] Migrate printer_tray to v10.0

This commit is contained in:
Sylvain GARANCHER
2016-11-16 17:19:31 +01:00
parent 90c6447a20
commit 2522577067
13 changed files with 195 additions and 33 deletions

View File

@@ -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 .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot :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 Known issues / Roadmap
====================== ======================

View File

@@ -4,7 +4,7 @@
{ {
'name': 'Report to printer - Paper tray selection', 'name': 'Report to printer - Paper tray selection',
'version': '9.0.1.0.0', 'version': '10.0.1.0.0',
'category': 'Printer', 'category': 'Printer',
'author': "Camptocamp, Odoo Community Association (OCA)", 'author': "Camptocamp, Odoo Community Association (OCA)",
'maintainer': 'Camptocamp', 'maintainer': 'Camptocamp',

View File

@@ -2,7 +2,7 @@
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>) # Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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): class IrActionsReportXml(models.Model):

View File

@@ -6,7 +6,7 @@ import errno
import logging import logging
import os import os
from openerp import models, fields, api from odoo import api, fields, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -40,30 +40,33 @@ class PrintingPrinter(models.Model):
try: try:
os.unlink(ppd_path) os.unlink(ppd_path)
except OSError as err: except OSError as err:
if err.errno == errno.ENOENT: # ENOENT means No such file or directory
pass # The file has already been deleted, we can continue the update
if err.errno != errno.ENOENT:
raise raise
if not option: if not option:
return vals return vals
vals_trays = [] vals['tray_ids'] = []
cups_trays = {
tray_names = set(tray.system_name for tray in self.tray_ids) tray_option['choice']: tray_option['text']
for tray_option in option.choices: 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))
cups_trays = set(tray_option['choice'] for tray_option # Add new trays
in option.choices) vals['tray_ids'].extend([
for tray in self.tray_ids: (0, 0, {'name': text, 'system_name': choice})
if tray.system_name not in cups_trays: for choice, text in cups_trays.items()
vals_trays.append((2, tray.id)) if choice not in self.tray_ids.mapped('system_name')
])
# 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 return vals
@api.multi @api.multi
@@ -72,13 +75,13 @@ class PrintingPrinter(models.Model):
printing_act_obj = self.env['printing.report.xml.action'] printing_act_obj = self.env['printing.report.xml.action']
options = super(PrintingPrinter, self).print_options(report, format) options = super(PrintingPrinter, self).print_options(report, format)
# Retrieve user default values
tray = self.env.user.printer_tray_id
if report is not None: if report is not None:
# Retrieve report default values # Retrieve report default values
if report.printer_tray_id: if report.printer_tray_id:
tray = 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 # Retrieve report-user specific values
action = printing_act_obj.search([ action = printing_act_obj.search([

View File

@@ -2,7 +2,7 @@
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>) # Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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): class PrintingReportXMLAction(models.Model):

View File

@@ -2,7 +2,7 @@
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>) # Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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): class PrinterTray(models.Model):

View File

@@ -2,7 +2,7 @@
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>) # Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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): class ResUsers(models.Model):

View File

@@ -2,5 +2,8 @@
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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_printer
from . import test_printing_report_xml_action
from . import test_printing_tray from . import test_printing_tray
from . import test_res_users

View File

@@ -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)

View File

@@ -2,13 +2,14 @@
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import errno
import mock import mock
import tempfile import tempfile
from openerp.tests.common import TransactionCase from odoo.tests.common import TransactionCase
model = 'openerp.addons.base_report_to_printer.models.printing_printer' model = 'odoo.addons.base_report_to_printer.models.printing_printer'
server_model = 'openerp.addons.base_report_to_printer.models.printing_server' server_model = 'odoo.addons.base_report_to_printer.models.printing_server'
ppd_header = '*PPD-Adobe: "4.3"' ppd_header = '*PPD-Adobe: "4.3"'
ppd_input_slot_header = """ ppd_input_slot_header = """
@@ -205,6 +206,47 @@ class TestPrintingPrinter(TransactionCase):
vals = self.printer._prepare_update_from_cups(connection, cups_printer) vals = self.printer._prepare_update_from_cups(connection, cups_printer)
self.assertFalse('tray_ids' in vals) 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) @mock.patch('%s.cups' % server_model)
def test_prepare_update_from_cups(self, cups): def test_prepare_update_from_cups(self, cups):
""" """

View File

@@ -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)

View File

@@ -2,10 +2,10 @@
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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): class TestPrintingTray(TransactionCase):

View File

@@ -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)