Skip to content

Commit

Permalink
[IMP] barcodes_generator_product: black, isort, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
chafique-delli committed Dec 11, 2020
1 parent d001d27 commit 20ca903
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 96 deletions.
39 changes: 18 additions & 21 deletions barcodes_generator_product/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
'name': 'Generate Barcodes for Products',
'summary': 'Generate Barcodes for Products (Templates and Variants)',
'version': '12.0.1.0.0',
'category': 'Tools',
'author':
'GRAP,'
'La Louve,'
'Odoo Community Association (OCA)',
'website': 'https://www.odoo-community.org',
'license': 'AGPL-3',
'depends': [
'barcodes_generator_abstract',
'product',
"name": "Generate Barcodes for Products",
"summary": "Generate Barcodes for Products (Templates and Variants)",
"version": "12.0.1.0.0",
"category": "Tools",
"author": "GRAP," "La Louve," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-barcode",
"license": "AGPL-3",
"depends": [
"barcodes_generator_abstract",
"product",
],
'data': [
'views/view_product_product.xml',
'views/view_product_template.xml',
"data": [
"views/view_product_product.xml",
"views/view_product_template.xml",
],
'demo': [
'demo/res_users.xml',
'demo/barcode_rule.xml',
'demo/product.xml',
'demo/function.xml',
"demo": [
"demo/res_users.xml",
"demo/barcode_rule.xml",
"demo/product.xml",
"demo/function.xml",
],
}
10 changes: 6 additions & 4 deletions barcodes_generator_product/demo/barcode_rule.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2016-Today: GRAP (http://www.grap.coop)
Copyright (C) 2016-Today La Louve (http://www.lalouve.net)
@author Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->

<odoo>

<!-- Note: type should be 'price' or 'weight' but this keys are define in point_of_sale module
<!-- Note: type should be 'price' or 'weight' but this keys are define in point_of_sale module
Not a big deal, this is just demo data, used for tests.
-->
<record id="product_generated_barcode" model="barcode.rule">
<field name="name">Product Rule (Generated Barcode)</field>
<field name="barcode_nomenclature_id" ref="barcodes.default_barcode_nomenclature"/>
<field
name="barcode_nomenclature_id"
ref="barcodes.default_barcode_nomenclature"
/>

<field name="type">product</field>
<field name="sequence">999</field>
Expand Down
20 changes: 16 additions & 4 deletions barcodes_generator_product/demo/function.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2016-Today: GRAP (http://www.grap.coop)
Copyright (C) 2016-Today La Louve (http://www.lalouve.net)
Expand All @@ -7,10 +7,22 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>

<function model="product.template" name="generate_barcode" eval="[ref('product_template_mono_variant')]"/>
<function
model="product.template"
name="generate_barcode"
eval="[ref('product_template_mono_variant')]"
/>

<function model="product.product" name="generate_barcode" eval="[ref('product_product_variant_1')]"/>
<function
model="product.product"
name="generate_barcode"
eval="[ref('product_product_variant_1')]"
/>

<function model="product.product" name="generate_barcode" eval="[ref('product_product_variant_2')]"/>
<function
model="product.product"
name="generate_barcode"
eval="[ref('product_product_variant_2')]"
/>

</odoo>
36 changes: 25 additions & 11 deletions barcodes_generator_product/demo/product.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2016-Today: GRAP (http://www.grap.coop)
Copyright (C) 2016-Today La Louve (http://www.lalouve.net)
@author Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->

<odoo>

<record id="product_template_mono_variant" model="product.template">
Expand All @@ -21,25 +20,40 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<record id="product_product_variant_1" model="product.product">
<field name="barcode_rule_id" ref="product_generated_barcode" />
<field name="barcode_base">10001</field>
<field name="product_tmpl_id" ref="product_template_multi_variant"/>
<field name="attribute_value_ids" eval="[(6,0,[ref('product.product_attribute_value_3')])]"/>
<field name="product_tmpl_id" ref="product_template_multi_variant" />
<field
name="attribute_value_ids"
eval="[(6,0,[ref('product.product_attribute_value_3')])]"
/>
</record>

<record id="product_product_variant_2" model="product.product">
<field name="barcode_rule_id" ref="product_generated_barcode" />
<field name="barcode_base">10002</field>
<field name="product_tmpl_id" ref="product_template_multi_variant"/>
<field name="attribute_value_ids" eval="[(6,0,[ref('product.product_attribute_value_4')])]"/>
<field name="product_tmpl_id" ref="product_template_multi_variant" />
<field
name="attribute_value_ids"
eval="[(6,0,[ref('product.product_attribute_value_4')])]"
/>
</record>

<record id="product_template_multi_variant_attribute_line" model="product.template.attribute.line">
<field name="product_tmpl_id" ref="product_template_multi_variant"/>
<field name="attribute_id" ref="product.product_attribute_2"/>
<field name="value_ids" eval="[(6,0,[ref('product.product_attribute_value_3'), ref('product.product_attribute_value_4')])]"/>
<record
id="product_template_multi_variant_attribute_line"
model="product.template.attribute.line"
>
<field name="product_tmpl_id" ref="product_template_multi_variant" />
<field name="attribute_id" ref="product.product_attribute_2" />
<field
name="value_ids"
eval="[(6,0,[ref('product.product_attribute_value_3'), ref('product.product_attribute_value_4')])]"
/>
</record>

<record id="product_template_multi_variant" model="product.template">
<field name="attribute_line_ids" eval="[(6,0,[ref('product_template_multi_variant_attribute_line')])]"/>
<field
name="attribute_line_ids"
eval="[(6,0,[ref('product_template_multi_variant_attribute_line')])]"
/>
</record>

</odoo>
10 changes: 6 additions & 4 deletions barcodes_generator_product/demo/res_users.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2016-Today: GRAP (http://www.grap.coop)
Copyright (C) 2016-Today La Louve (http://www.lalouve.net)
@author Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->

<odoo>

<record id="base.user_root" model="res.users">
<field name="groups_id" eval="[
<field
name="groups_id"
eval="[
(4, ref('product.group_product_variant')),
]" />
]"
/>
</record>

</odoo>
5 changes: 2 additions & 3 deletions barcodes_generator_product/models/barcode_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class BarcodeRule(models.Model):
_inherit = 'barcode.rule'
_inherit = "barcode.rule"

generate_model = fields.Selection(
selection_add=[('product.product', 'Products')])
generate_model = fields.Selection(selection_add=[("product.product", "Products")])
4 changes: 2 additions & 2 deletions barcodes_generator_product/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@


class ProductProduct(models.Model):
_name = 'product.product'
_inherit = ['product.product', 'barcode.generate.mixin']
_name = "product.product"
_inherit = ["product.product", "barcode.generate.mixin"]
29 changes: 18 additions & 11 deletions barcodes_generator_product/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@

from odoo import api, fields, models

from odoo.addons.barcodes_generator_abstract.models.barcode_rule\
import _GENERATE_TYPE
from odoo.addons.barcodes_generator_abstract.models.barcode_rule import _GENERATE_TYPE


class ProductTemplate(models.Model):
_inherit = 'product.template'
_inherit = "product.template"

# Related to display product product information if is_product_variant
barcode_rule_id = fields.Many2one(
related='product_variant_ids.barcode_rule_id',
string='Barcode Rule', readonly=False, comodel_name='barcode.rule')
related="product_variant_ids.barcode_rule_id",
string="Barcode Rule",
readonly=False,
comodel_name="barcode.rule",
)

barcode_base = fields.Integer(
related='product_variant_ids.barcode_base',
readonly=False, string='Barcode Base')
related="product_variant_ids.barcode_base",
readonly=False,
string="Barcode Base",
)

generate_type = fields.Selection(
string='Generate Type', selection=_GENERATE_TYPE, readonly=True,
related='product_variant_ids.barcode_rule_id.generate_type')
string="Generate Type",
selection=_GENERATE_TYPE,
readonly=True,
related="product_variant_ids.barcode_rule_id.generate_type",
)

# View Section
@api.multi
Expand All @@ -35,7 +42,7 @@ def generate_barcode(self):
self.ensure_one()
self.product_variant_ids.generate_barcode()

@api.onchange('barcode_rule_id')
@api.onchange("barcode_rule_id")
def onchange_barcode_rule_id(self):
self.generate_type = self.barcode_rule_id.generate_type

Expand All @@ -48,7 +55,7 @@ def create(self, vals):
# these fields should be moved to product as lead to confusion
# (Ref. product module feature in Odoo Core)
related_vals = {}
for field in ['barcode_rule_id', 'barcode_base']:
for field in ["barcode_rule_id", "barcode_base"]:
if vals.get(field, False):
related_vals[field] = vals[field]
if related_vals:
Expand Down
34 changes: 22 additions & 12 deletions barcodes_generator_product/tests/test_barcodes_generator_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ class Tests(TransactionCase):

def setUp(self):
super(Tests, self).setUp()
self.template_obj = self.env['product.template']
self.product_obj = self.env['product.product']
self.template_obj = self.env["product.template"]
self.product_obj = self.env["product.product"]

# Test Section
def test_01_manual_generation_template(self):
self.template_mono = self.template_obj.browse(self.ref(
'barcodes_generator_product.product_template_mono_variant'))
self.template_mono = self.template_obj.browse(
self.ref("barcodes_generator_product.product_template_mono_variant")
)
self.assertEqual(
self.template_mono.barcode, "2000050000003",
self.template_mono.barcode,
"2000050000003",
"Incorrect Manual Barcode Generation for non varianted Template."
" Pattern : %s - Base : %s" % (
" Pattern : %s - Base : %s"
% (
self.template_mono.barcode_rule_id.pattern,
self.template_mono.barcode_base))
self.template_mono.barcode_base,
),
)

def test_02_manual_generation_product(self):
self.product_variant_1 = self.product_obj.browse(self.ref(
'barcodes_generator_product.product_product_variant_1'))
self.product_variant_1 = self.product_obj.browse(
self.ref("barcodes_generator_product.product_product_variant_1")
)
self.assertEqual(
self.product_variant_1.barcode, "2010001000006",
self.product_variant_1.barcode,
"2010001000006",
"Incorrect Manual Barcode Generation for varianted Product."
" Pattern : %s - Base : %s" % (
" Pattern : %s - Base : %s"
% (
self.product_variant_1.barcode_rule_id.pattern,
self.product_variant_1.barcode_base))
self.product_variant_1.barcode_base,
),
)
43 changes: 31 additions & 12 deletions barcodes_generator_product/views/view_product_product.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2016-Today: GRAP (http://www.grap.coop)
Copyright (C) 2016-Today GRAP (http://www.lalouve.net)
@author Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->

<odoo>

<record id="view_product_product_form" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="inherit_id" ref="product.product_normal_form_view" />
<field name="arch" type="xml">
<field name="barcode" position="before">
<field name="barcode_rule_id"
<field
name="barcode_rule_id"
domain="[('generate_model', '=', 'product.product')]"
groups="barcodes_generator_abstract.generate_barcode" colspan="2"/>
groups="barcodes_generator_abstract.generate_barcode"
colspan="2"
/>
<field name="generate_type" invisible="1" />
<field name="barcode_base" attrs="{
<field
name="barcode_base"
attrs="{
'invisible': [('barcode_rule_id', '=', False)],
'readonly': [('generate_type', '!=', 'manual')]}"
groups="barcodes_generator_abstract.generate_barcode" colspan="2"/>
<button name="generate_base" type="object" string="Generate Base (Using Sequence)" attrs="{'invisible': ['|',
groups="barcodes_generator_abstract.generate_barcode"
colspan="2"
/>
<button
name="generate_base"
type="object"
string="Generate Base (Using Sequence)"
attrs="{'invisible': ['|',
('generate_type', '!=', 'sequence'),
('barcode_base', '!=', 0)]}"
groups="barcodes_generator_abstract.generate_barcode" colspan="2"/>
groups="barcodes_generator_abstract.generate_barcode"
colspan="2"
/>
</field>

<field name="barcode" position="attributes">
<attribute name="attrs">{'readonly': [('generate_type', '=', 'sequence')]}</attribute>
<attribute
name="attrs"
>{'readonly': [('generate_type', '=', 'sequence')]}</attribute>
</field>

<field name="barcode" position="after">
<button name="generate_barcode" type="object" string="Generate Barcode (Using Barcode Rule)"
<button
name="generate_barcode"
type="object"
string="Generate Barcode (Using Barcode Rule)"
attrs="{'invisible': ['|',
('barcode_rule_id', '=', False),
('barcode_base', '=', 0)]}"
groups="barcodes_generator_abstract.generate_barcode" colspan="2"/>
groups="barcodes_generator_abstract.generate_barcode"
colspan="2"
/>
</field>
</field>
</record>
Expand Down
Loading

0 comments on commit 20ca903

Please sign in to comment.