Skip to content

Commit

Permalink
product_sequence (OCA#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
SodexisTeam authored and Artem Kostyuk committed Jul 23, 2018
1 parent 733f4f4 commit a7a306b
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 133 deletions.
17 changes: 15 additions & 2 deletions product_sequence/README.rst
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
===========

Expand All @@ -29,6 +41,7 @@ Contributors

* Angel Moya <[email protected]>
* Graeme Gellatly <[email protected]>
* Sodexis <[email protected]>

Maintainer
----------
Expand Down
27 changes: 5 additions & 22 deletions product_sequence/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (c) 2015 Domatix (http://domatix.com)
# Angel Moua <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# -*- 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
32 changes: 7 additions & 25 deletions product_sequence/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 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',
Expand All @@ -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,
}
17 changes: 9 additions & 8 deletions product_sequence/data/product_sequence.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<openerp>
<data noupdate="1">

<record id="seq_product_auto_type" model="ir.sequence.type">
<field name="name">Product</field>
<field name="code">product.product</field>
</record>
<!--
product_sequence for Odoo
Copyright (C) 2004 Tiny SPRL
Copyright (C) 2016 Sodexis
The licence is in the file __openerp__.py
-->

<odoo>
<data noupdate="1">
<record id="seq_product_auto" model="ir.sequence">
<field name="name">Product</field>
<field name="code">product.product</field>
<field eval="5" name="padding"/>
<field name="prefix">PR/</field>
</record>
</data>
</openerp>
</odoo>
31 changes: 17 additions & 14 deletions product_sequence/demo/product_product.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<record id="product.product_product_consultant" model="product.product">
<field name="default_code">SERVICE</field>
</record>
<!--
product_sequence for Odoo
Copyright (C) 2004 Tiny SPRL
Copyright (C) 2016 Sodexis
The licence is in the file __openerp__.py
-->

<record id="product.product_product_1" model="product.product">
<field name="default_code">OSM</field>
</record>

<record id="product.product_product_2" model="product.product">
<field name="default_code">OSA</field>
</record>
</data>
</openerp>
<odoo>
<record id="product.product_product_0" model="product.product">
<field name="default_code">SERVICE</field>
</record>
<record id="product.product_product_1" model="product.product">
<field name="default_code">OSM</field>
</record>
<record id="product.product_product_2" model="product.product">
<field name="default_code">OSA</field>
</record>
</odoo>
17 changes: 17 additions & 0 deletions product_sequence/hooks.py
Original file line number Diff line number Diff line change
@@ -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 = '/';")
25 changes: 4 additions & 21 deletions product_sequence/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (c) 2015 Domatix (http://domatix.com)
# Angel Moya <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# -*- coding: utf-8 -*-
# © 2004 Tiny SPRL
# © 2016 Sodexis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import product_product
51 changes: 10 additions & 41 deletions product_sequence/models/product_product.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 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):
Expand All @@ -42,7 +12,7 @@ class ProductProduct(models.Model):
default_code = fields.Char(
string='Reference',
size=64,
select=True,
index=True,
required=True,
default='/')

Expand All @@ -55,26 +25,25 @@ 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 = {}
if self.default_code:
default.update({
'default_code': self.default_code + _('-copy'),
})

return super(ProductProduct, self).copy(default)
5 changes: 5 additions & 0 deletions product_sequence/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions product_sequence/tests/test_product_sequence.py
Original file line number Diff line number Diff line change
@@ -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,))

0 comments on commit a7a306b

Please sign in to comment.