From 254c4e8545b5eccb6a13ac01576c8d583a1e20d6 Mon Sep 17 00:00:00 2001 From: SodexisTeam Date: Mon, 24 Oct 2016 15:03:49 +0530 Subject: [PATCH] product_sequence (#156) --- product_sequence/README.rst | 17 ++++++- product_sequence/__init__.py | 27 ++-------- product_sequence/__manifest__.py | 32 +++--------- product_sequence/data/product_sequence.xml | 17 ++++--- product_sequence/demo/product_product.xml | 31 ++++++----- product_sequence/hooks.py | 17 +++++++ product_sequence/models/__init__.py | 25 ++------- product_sequence/models/product_product.py | 51 ++++--------------- product_sequence/tests/__init__.py | 5 ++ .../tests/test_product_sequence.py | 49 ++++++++++++++++++ 10 files changed, 138 insertions(+), 133 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 d5b963c35648..6bae50163f4f 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,13 @@ 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 +===== + +.. 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 +41,7 @@ Contributors * Angel Moya * Graeme Gellatly +* Sodexis Maintainer ---------- diff --git a/product_sequence/__init__.py b/product_sequence/__init__.py index 6627d18f655e..eee5f214b0bd 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 +from .hooks import pre_init_hook diff --git a/product_sequence/__manifest__.py b/product_sequence/__manifest__.py index 5e0d12f3fd49..606fec0807c8 100644 --- a/product_sequence/__manifest__.py +++ b/product_sequence/__manifest__.py @@ -1,30 +1,12 @@ # -*- 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', - 'author': "Zikzakmedia SL,Odoo Community Association (OCA)", + 'version': '9.0.1.0.0', + 'author': "Zikzakmedia SL,Sodexis,Odoo Community Association (OCA)", 'website': 'http://www.zikzakmedia.com', 'license': 'AGPL-3', 'category': 'Generic Modules/Inventory Control', @@ -37,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': False, + 'installable': True, } diff --git a/product_sequence/data/product_sequence.xml b/product_sequence/data/product_sequence.xml index f6f4ad48377e..d95dd288fb58 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 a570eb617a0a..0bb9a49a352a 100644 --- a/product_sequence/demo/product_product.xml +++ b/product_sequence/demo/product_product.xml @@ -1,16 +1,19 @@ - - - - SERVICE - + - - OSM - - - - OSA - - - + + + SERVICE + + + OSM + + + OSA + + diff --git a/product_sequence/hooks.py b/product_sequence/hooks.py new file mode 100644 index 000000000000..ab9394c0612b --- /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/__init__.py b/product_sequence/models/__init__.py index 4804aac3f0a8..50419910d239 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 f8fb8b5dcc7a..9cfe4c7ecf63 100644 --- a/product_sequence/models/product_product.py +++ b/product_sequence/models/product_product.py @@ -1,39 +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 _ - - -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 = '/';") +from openerp import models, fields, api, _ class ProductProduct(models.Model): @@ -42,7 +12,7 @@ class ProductProduct(models.Model): default_code = fields.Char( string='Reference', size=64, - select=True, + index=True, required=True, default='/') @@ -55,20 +25,20 @@ 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 - @api.one + @api.multi def copy(self, default=None): if default is None: default = {} @@ -76,5 +46,4 @@ def copy(self, default=None): default.update({ 'default_code': self.default_code + _('-copy'), }) - return super(ProductProduct, self).copy(default) diff --git a/product_sequence/tests/__init__.py b/product_sequence/tests/__init__.py new file mode 100644 index 000000000000..18a8bc676789 --- /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 000000000000..a823c45cc00b --- /dev/null +++ b/product_sequence/tests/test_product_sequence.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# © 2016 Sodexis +# 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): + """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 = self.product_product.create(dict( + name="Apple", + default_code='PROD01' + )) + self.assertEqual(product.default_code, 'PROD01') + + def test_product_create_without_default_code(self): + 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,))