Skip to content

Commit

Permalink
Merge PR #413 into 12.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
OCA-git-bot committed Feb 28, 2020
2 parents 36ea7ac + 0cbd84b commit a92dbde
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 0 deletions.
67 changes: 67 additions & 0 deletions product_code_mandatory/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.. image:: https://img.shields.io/badge/license-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

======================
Product Code Mandatory
======================

This module sets the field internal reference (default_code) of the product
as required.

Usage
=====

* Unable to save a product with an empty or blank internal reference.
* When creating more than one product variant from the template, a variant will be created
with a default value for default_code field.
* A pre_init_hook process is initiated when there exist records without an internal reference(default_code).
A default value is generated to populate empty field as a temporary value.

.. 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/12.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/product-attribute/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Antonio Yamuta <ayamuta@opensourceintegrators.com>
* Sudhir Arya <sudhir@erpharbor.com>

Funders
-------

The development of this module has been financially supported by:

* Open Source Integrators <http://www.opensourceintegrators.com>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
13 changes: 13 additions & 0 deletions product_code_mandatory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from . import models


def pre_init_product_code(cr):
cr.execute("""UPDATE product_template
SET default_code = 'DEFAULT' || nextval('ir_default_id_seq')
WHERE default_code is NULL
OR LENGTH(default_code) = 0""")
cr.execute("""UPDATE product_product
SET default_code = 'DEFAULT' || nextval('ir_default_id_seq')
WHERE default_code is NULL
OR LENGTH(default_code) = 0""")
return True
21 changes: 21 additions & 0 deletions product_code_mandatory/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2018 - TODAY, Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Product Internal Reference as Required",
"summary": "Set Product Internal Reference as a required field",
"version": "12.0.1.0.0",
"license": "AGPL-3",
"author": "Open Source Integrators, Odoo Community Association (OCA)",
"category": "Product",
"website": "https://github.com/OCA/product-attribute",
"depends": [
"product",
],
"data": [
"data/product_code_seq.xml",
"views/product_view.xml",
],
"pre_init_hook": 'pre_init_product_code',
"installable": True,
}
12 changes: 12 additions & 0 deletions product_code_mandatory/data/product_code_seq.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<odoo noupdate="1">

<record id="product_default_code_seq" model="ir.sequence">
<field name="name">Product Default Code Mandatory</field>
<field name="code">product.default.code</field>
<field name="prefix">DEFAULT-</field>
<field name="padding">4</field>
<field name="company_id" eval="False"/>
</record>

</odoo>
20 changes: 20 additions & 0 deletions product_code_mandatory/i18n/product_code_mandatory.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * product_code_mandatory
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 11.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: product_code_mandatory
#: model:ir.model,name:product_code_mandatory.model_product_product
msgid "Product"
msgstr ""

1 change: 1 addition & 0 deletions product_code_mandatory/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import product
14 changes: 14 additions & 0 deletions product_code_mandatory/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (C) 2018 - TODAY, Open Source Integrators License AGPL-3.0
# or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ProductProduct(models.Model):
_inherit = 'product.product'

def _get_default_code(self):
return self.env['ir.sequence'].next_by_code('product.default.code')

default_code = fields.Char('Internal Reference', index=True,
default=_get_default_code)
2 changes: 2 additions & 0 deletions product_code_mandatory/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Antonio Yamuta <ayamuta@opensourceintegrators.com>
* Sudhir Arya <sudhir@erpharbor.com>
2 changes: 2 additions & 0 deletions product_code_mandatory/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module sets the field internal reference (default_code) of the product
as required.
9 changes: 9 additions & 0 deletions product_code_mandatory/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* Unable to save a product with an empty or blank internal reference.
* When creating more than one product variant from the template, a variant will be created
with a default value for default_code field.
* A pre_init_hook process is initiated when there exist records without an internal reference(default_code).
A default value is generated to populate empty field as a temporary value.

.. 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/12.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions product_code_mandatory/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_product_code
17 changes: 17 additions & 0 deletions product_code_mandatory/tests/test_product_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase


class TestProductCode(TransactionCase):

def setUp(self):
super(TestProductCode, self).setUp()
self.product_model = self.env['product.product']
self.product = self.product_model.create({
'name': 'Test Product Code',
})

def test_product_code(self):
"""Check Product Code"""
self.assertTrue(self.product.default_code, 'Product code is not set.')
27 changes: 27 additions & 0 deletions product_code_mandatory/views/product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="product_template_only_form_code_mandatory_view" model="ir.ui.view">
<field name="name">product.template.only.form.code.mandatory.view</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="arch" type="xml">
<field name="default_code" position="attributes">
<attribute name="attrs">{'invisible': [('product_variant_count', '&gt;', 1)],
'required': [('product_variant_count', '=', 1)]}</attribute>
</field>
</field>
</record>

<record id="product_normal_form_code_mandatory_view" model="ir.ui.view">
<field name="name">product.normal.form.code.mandatory.view</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<field name="default_code" position="attributes">
<attribute name="required">True</attribute>
</field>
</field>
</record>

</odoo>

0 comments on commit a92dbde

Please # to comment.