From 7daeb19c0cec22e5f2d812ec7a15bd3bb09871a1 Mon Sep 17 00:00:00 2001 From: "Atchuthan, Sodexis" Date: Wed, 18 May 2016 18:45:33 +0530 Subject: [PATCH 1/4] [MIG][9.0] product_sequence --- product_sequence/README.rst | 19 +++++++++++-- product_sequence/__init__.py | 25 +++-------------- product_sequence/__openerp__.py | 28 ++++--------------- product_sequence/data/product_sequence.xml | 17 ++++++------ product_sequence/demo/product_product.xml | 32 ++++++++++++---------- product_sequence/models/__init__.py | 25 +++-------------- product_sequence/models/product_product.py | 28 ++++--------------- 7 files changed, 61 insertions(+), 113 deletions(-) diff --git a/product_sequence/README.rst b/product_sequence/README.rst index d5b963c3564..7d4eab477da 100644 --- a/product_sequence/README.rst +++ b/product_sequence/README.rst @@ -1,5 +1,10 @@ -A module that adds sequence to the product. -=========================================== +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +================ +Product Sequence +================ This module allows to associate a sequence to the product reference. The reference (default code) is unique (SQL constraint) and required. @@ -13,6 +18,15 @@ they already have a unique reference (or no reference) set. Products with a def Otherwise the setting of the unique constraint will fail and the module will fail to install. +Usage +===== + +#. Go to ... + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/135/9.0 + Bug Tracker =========== @@ -29,6 +43,7 @@ Contributors * Angel Moya * Graeme Gellatly +* Sodexis Maintainer ---------- diff --git a/product_sequence/__init__.py b/product_sequence/__init__.py index 6627d18f655..edd3525aa00 100644 --- a/product_sequence/__init__.py +++ b/product_sequence/__init__.py @@ -1,24 +1,7 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (c) 2015 Domatix (http://domatix.com) -# Angel Moua -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# © 2004 Tiny SPRL +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import models from .models.product_product import update_null_and_slash_codes diff --git a/product_sequence/__openerp__.py b/product_sequence/__openerp__.py index 5e0d12f3fd4..0f3faf7c3ac 100644 --- a/product_sequence/__openerp__.py +++ b/product_sequence/__openerp__.py @@ -1,29 +1,11 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (c) 2011 Zikzakmedia S.L. (http://zikzakmedia.com) -# All Rights Reserved. -# $Id$ -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# © 2004 Tiny SPRL +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { 'name': 'Product Sequence', - 'version': '8.0.0.1.0', + 'version': '9.0.1.0.0', 'author': "Zikzakmedia SL,Odoo Community Association (OCA)", 'website': 'http://www.zikzakmedia.com', 'license': 'AGPL-3', @@ -39,5 +21,5 @@ ], 'pre_init_hook': 'update_null_and_slash_codes', 'auto_install': False, - 'installable': False, + 'installable': True, } diff --git a/product_sequence/data/product_sequence.xml b/product_sequence/data/product_sequence.xml index f6f4ad48377..d95dd288fb5 100644 --- a/product_sequence/data/product_sequence.xml +++ b/product_sequence/data/product_sequence.xml @@ -1,12 +1,13 @@ - - - - - Product - product.product - + + + Product product.product @@ -14,4 +15,4 @@ PR/ - + diff --git a/product_sequence/demo/product_product.xml b/product_sequence/demo/product_product.xml index a570eb617a0..9b451ce06e4 100644 --- a/product_sequence/demo/product_product.xml +++ b/product_sequence/demo/product_product.xml @@ -1,16 +1,18 @@ - - - - SERVICE - - - - OSM - - - - OSA - - - + + + + SERVICE + + + OSM + + + OSA + + \ No newline at end of file diff --git a/product_sequence/models/__init__.py b/product_sequence/models/__init__.py index 4804aac3f0a..50419910d23 100644 --- a/product_sequence/models/__init__.py +++ b/product_sequence/models/__init__.py @@ -1,23 +1,6 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (c) 2015 Domatix (http://domatix.com) -# Angel Moya -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# © 2004 Tiny SPRL +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import product_product diff --git a/product_sequence/models/product_product.py b/product_sequence/models/product_product.py index f8fb8b5dcc7..b3536beef30 100644 --- a/product_sequence/models/product_product.py +++ b/product_sequence/models/product_product.py @@ -1,26 +1,9 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# © 2004 Tiny SPRL +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields, api -from openerp.tools.translate import _ +from openerp import models, fields, api, _ def update_null_and_slash_codes(cr): # pragma: no cover @@ -42,7 +25,7 @@ class ProductProduct(models.Model): default_code = fields.Char( string='Reference', size=64, - select=True, + index=True, required=True, default='/') @@ -76,5 +59,4 @@ def copy(self, default=None): default.update({ 'default_code': self.default_code + _('-copy'), }) - return super(ProductProduct, self).copy(default) From 55933fd4e725f8d89d40148601a4218cf98c944e Mon Sep 17 00:00:00 2001 From: "Atchuthan, Sodexis" Date: Wed, 31 Aug 2016 16:35:15 +0530 Subject: [PATCH 2/4] [FIX] replaced sequence.get() with next_by_id() --- product_sequence/__openerp__.py | 2 +- product_sequence/models/product_product.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/product_sequence/__openerp__.py b/product_sequence/__openerp__.py index 0f3faf7c3ac..5f6086aba4f 100644 --- a/product_sequence/__openerp__.py +++ b/product_sequence/__openerp__.py @@ -6,7 +6,7 @@ { 'name': 'Product Sequence', 'version': '9.0.1.0.0', - 'author': "Zikzakmedia SL,Odoo Community Association (OCA)", + 'author': "Zikzakmedia SL,Sodexis,Odoo Community Association (OCA)", 'website': 'http://www.zikzakmedia.com', 'license': 'AGPL-3', 'category': 'Generic Modules/Inventory Control', diff --git a/product_sequence/models/product_product.py b/product_sequence/models/product_product.py index b3536beef30..7b0d689b751 100644 --- a/product_sequence/models/product_product.py +++ b/product_sequence/models/product_product.py @@ -38,16 +38,16 @@ class ProductProduct(models.Model): @api.model def create(self, vals): if 'default_code' not in vals or vals['default_code'] == '/': - vals['default_code'] = self.env['ir.sequence'].get( - 'product.product') + sequence = self.env.ref('product_sequence.seq_product_auto') + vals['default_code'] = sequence.next_by_id() return super(ProductProduct, self).create(vals) @api.multi def write(self, vals): for product in self: if product.default_code in [False, '/']: - vals['default_code'] = self.env['ir.sequence'].get( - 'product.product') + sequence = self.env.ref('product_sequence.seq_product_auto') + vals['default_code'] = sequence.next_by_id() super(ProductProduct, product).write(vals) return True From 9d20322a8ad0cd5e9ce73f17337bde80e5a1afc3 Mon Sep 17 00:00:00 2001 From: "Atchuthan, Sodexis" Date: Wed, 31 Aug 2016 20:04:23 +0530 Subject: [PATCH 3/4] [ADD] test file and resolved travis issues --- product_sequence/README.rst | 2 -- product_sequence/__init__.py | 2 +- product_sequence/__openerp__.py | 2 +- product_sequence/demo/product_product.xml | 3 ++- product_sequence/hooks.py | 17 +++++++++++++ product_sequence/models/product_product.py | 15 +----------- product_sequence/tests/__init__.py | 5 ++++ .../tests/test_product_sequence.py | 24 +++++++++++++++++++ 8 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 product_sequence/hooks.py create mode 100644 product_sequence/tests/__init__.py create mode 100644 product_sequence/tests/test_product_sequence.py diff --git a/product_sequence/README.rst b/product_sequence/README.rst index 7d4eab477da..6bae50163f4 100644 --- a/product_sequence/README.rst +++ b/product_sequence/README.rst @@ -21,8 +21,6 @@ Otherwise the setting of the unique constraint will fail and the module will fai Usage ===== -#. Go to ... - .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot :target: https://runbot.odoo-community.org/runbot/135/9.0 diff --git a/product_sequence/__init__.py b/product_sequence/__init__.py index edd3525aa00..eee5f214b0b 100644 --- a/product_sequence/__init__.py +++ b/product_sequence/__init__.py @@ -4,4 +4,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import models -from .models.product_product import update_null_and_slash_codes +from .hooks import pre_init_hook diff --git a/product_sequence/__openerp__.py b/product_sequence/__openerp__.py index 5f6086aba4f..606fec0807c 100644 --- a/product_sequence/__openerp__.py +++ b/product_sequence/__openerp__.py @@ -19,7 +19,7 @@ 'demo': [ 'demo/product_product.xml' ], - 'pre_init_hook': 'update_null_and_slash_codes', + 'pre_init_hook': 'pre_init_hook', 'auto_install': False, 'installable': True, } diff --git a/product_sequence/demo/product_product.xml b/product_sequence/demo/product_product.xml index 9b451ce06e4..0bb9a49a352 100644 --- a/product_sequence/demo/product_product.xml +++ b/product_sequence/demo/product_product.xml @@ -5,6 +5,7 @@ Copyright (C) 2016 Sodexis The licence is in the file __openerp__.py --> + SERVICE @@ -15,4 +16,4 @@ OSA - \ No newline at end of file + diff --git a/product_sequence/hooks.py b/product_sequence/hooks.py new file mode 100644 index 00000000000..ab9394c0612 --- /dev/null +++ b/product_sequence/hooks.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# © 2004 Tiny SPRL +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +def pre_init_hook(cr): + """ + Updates existing codes matching the default '/' or + empty. Primarily this ensures installation does not + fail for demo data. + :param cr: database cursor + :return: void + """ + cr.execute("UPDATE product_product " + "SET default_code = '!!mig!!' || id " + "WHERE default_code IS NULL OR default_code = '/';") diff --git a/product_sequence/models/product_product.py b/product_sequence/models/product_product.py index 7b0d689b751..9cfe4c7ecf6 100644 --- a/product_sequence/models/product_product.py +++ b/product_sequence/models/product_product.py @@ -6,19 +6,6 @@ from openerp import models, fields, api, _ -def update_null_and_slash_codes(cr): # pragma: no cover - """ - Updates existing codes matching the default '/' or - empty. Primarily this ensures installation does not - fail for demo data. - :param cr: database cursor - :return: void - """ - cr.execute("UPDATE product_product " - "SET default_code = '!!mig!!' || id " - "WHERE default_code IS NULL OR default_code = '/';") - - class ProductProduct(models.Model): _inherit = 'product.product' @@ -51,7 +38,7 @@ def write(self, vals): super(ProductProduct, product).write(vals) return True - @api.one + @api.multi def copy(self, default=None): if default is None: default = {} diff --git a/product_sequence/tests/__init__.py b/product_sequence/tests/__init__.py new file mode 100644 index 00000000000..18a8bc67678 --- /dev/null +++ b/product_sequence/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_product_sequence diff --git a/product_sequence/tests/test_product_sequence.py b/product_sequence/tests/test_product_sequence.py new file mode 100644 index 00000000000..d225f3273af --- /dev/null +++ b/product_sequence/tests/test_product_sequence.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# © 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests.common import TransactionCase + + +class TestProductSequence(TransactionCase): + """Tests for creating product with and without Product Sequence""" + + def setUp(self): + super(TestProductSequence, self).setUp() + self.product_product = self.env['product.product'] + + def test_product_create_with_default_code(self): + product_id = self.product_product.create(dict( + name="Apple", + default_code='PROD01' + )) + self.assertEqual(product_id.default_code, 'PROD01') + + def test_product_create_without_default_code(self): + product_id_1 = self.product_product.create(dict(name="Orange")) + self.assertRegexpMatches(str(product_id_1.default_code), r'PR/*') From eca71a617b8cedfb77b70bee9643da2331f903eb Mon Sep 17 00:00:00 2001 From: "Atchuthan, Sodexis" Date: Fri, 30 Sep 2016 15:56:51 +0530 Subject: [PATCH 4/4] [FIX] Added test file with pre_init_hook --- .../tests/test_product_sequence.py | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/product_sequence/tests/test_product_sequence.py b/product_sequence/tests/test_product_sequence.py index d225f3273af..a823c45cc00 100644 --- a/product_sequence/tests/test_product_sequence.py +++ b/product_sequence/tests/test_product_sequence.py @@ -3,6 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from openerp.tests.common import TransactionCase +from ..hooks import pre_init_hook class TestProductSequence(TransactionCase): @@ -13,12 +14,36 @@ def setUp(self): self.product_product = self.env['product.product'] def test_product_create_with_default_code(self): - product_id = self.product_product.create(dict( + product = self.product_product.create(dict( name="Apple", default_code='PROD01' )) - self.assertEqual(product_id.default_code, 'PROD01') + self.assertEqual(product.default_code, 'PROD01') def test_product_create_without_default_code(self): - product_id_1 = self.product_product.create(dict(name="Orange")) - self.assertRegexpMatches(str(product_id_1.default_code), r'PR/*') + product_1 = self.product_product.create(dict( + name="Orange", + default_code='/')) + self.assertRegexpMatches(str(product_1.default_code), r'PR/*') + + def test_product_copy(self): + product_2 = self.product_product.create(dict( + name="Apple", + default_code='PROD02' + )) + copy_product_2 = product_2.copy() + self.assertEqual(copy_product_2.default_code, 'PROD02-copy') + + def test_pre_init_hook(self): + product_3 = self.product_product.create(dict( + name="Apple", + default_code='PROD03' + )) + self.cr.execute( + "update product_product set default_code='/' where id=%s" + % (product_3.id,)) + product_3.invalidate_cache() + self.assertEqual(product_3.default_code, '/') + pre_init_hook(self.cr) + product_3.invalidate_cache() + self.assertEqual(product_3.default_code, '!!mig!!%s' % (product_3.id,))