[IMP] stock_putaway_product_form: black, isort, prettier

This commit is contained in:
Víctor Martínez
2020-10-27 17:18:12 +01:00
parent aa7844d721
commit 1dd2b20767
7 changed files with 128 additions and 141 deletions

View File

@@ -8,11 +8,7 @@
"author": "Akretion, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["stock"],
"external_dependencies": {
"python": [
"openupgradelib",
],
},
"external_dependencies": {"python": ["openupgradelib"]},
"data": ["views/product.xml"],
"demo": ["demo/putaway_strategies.xml"],
"maintainers": ["kevinkhao", "sebastienbeau"],

View File

@@ -1,36 +1,31 @@
<?xml version="1.0"?>
<?xml version="1.0" ?>
<odoo>
<!--Shows simplest case for categories -->
<record id="putaway_strat_1" model="product.putaway">
<field name="name">Putaway Strategy 1</field>
</record>
<record id="putaway_strat_1_line_1" model="stock.fixed.putaway.strat">
<field name="product_id" ref="product.product_product_11"/>
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_1"/>
<field name="fixed_location_id" ref="stock.stock_location_shop0"/>
<field name="product_id" ref="product.product_product_11" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_1" />
<field name="fixed_location_id" ref="stock.stock_location_shop0" />
</record>
<record id="putaway_strat_1_line_2" model="stock.fixed.putaway.strat">
<field name="category_id" ref="product.product_category_5"/>
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_1"/>
<field name="fixed_location_id" ref="stock.stock_location_shop0"/>
<field name="category_id" ref="product.product_category_5" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_1" />
<field name="fixed_location_id" ref="stock.stock_location_shop0" />
</record>
<!--Shows inherited case for categories-->
<record id="putaway_strat_2" model="product.putaway">
<field name="name">Putaway Strategy 2</field>
</record>
<record id="putaway_strat_2_line_1" model="stock.fixed.putaway.strat">
<field name="category_id" ref="product.product_category_1"/>
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_2"/>
<field name="fixed_location_id" ref="stock.stock_location_stock"/>
<field name="category_id" ref="product.product_category_1" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_2" />
<field name="fixed_location_id" ref="stock.stock_location_stock" />
</record>
<record id="putaway_strat_2_line_2" model="stock.fixed.putaway.strat">
<field name="category_id" ref="product.product_category_all"/>
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_2"/>
<field name="fixed_location_id" ref="stock.stock_location_stock"/>
<field name="category_id" ref="product.product_category_all" />
<field name="putaway_id" ref="stock_putaway_product_form.putaway_strat_2" />
<field name="fixed_location_id" ref="stock.stock_location_stock" />
</record>
</odoo>

View File

@@ -2,6 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging
from openupgradelib import openupgrade
_logger = logging.getLogger(__name__)
@@ -9,11 +10,12 @@ _logger = logging.getLogger(__name__)
def post_init_hook(cr, registry):
openupgrade.logged_query(
cr, """
cr,
"""
UPDATE stock_fixed_putaway_strat sfps
SET product_tmpl_id=pp.product_tmpl_id
FROM product_product pp
WHERE pp.id=sfps.product_id AND
sfps.product_tmpl_id <> pp.product_tmpl_id
"""
""",
)

View File

@@ -20,15 +20,11 @@ class ProductTemplate(models.Model):
def _find_closest_categ_match(self, categ, putaway_lines):
"""Returns the putaway line with the nearest product category"""
lines_match_categ = putaway_lines.filtered(
lambda r: r.category_id == categ
)
lines_match_categ = putaway_lines.filtered(lambda r: r.category_id == categ)
if lines_match_categ:
return lines_match_categ[0]
elif categ.parent_id:
return self._find_closest_categ_match(
categ.parent_id, putaway_lines
)
return self._find_closest_categ_match(categ.parent_id, putaway_lines)
else:
return self.env["stock.fixed.putaway.strat"]
@@ -50,9 +46,9 @@ class ProductTemplate(models.Model):
categ = rec.categ_id
categs = self._get_categ_and_parents(categ)
# get matching lines from our category or its parents
product_putaway_categ_lines = self.env[
"stock.fixed.putaway.strat"
].search([("category_id", "in", categs.ids)])
product_putaway_categ_lines = self.env["stock.fixed.putaway.strat"].search(
[("category_id", "in", categs.ids)]
)
# from these, get the matching putaway.strats and find
# the lowest-level category match
product_putaways = product_putaway_categ_lines.mapped("putaway_id")

View File

@@ -5,14 +5,15 @@ from odoo import api, fields, models
class PutAwayStrategy(models.Model):
_inherit = 'product.putaway'
_inherit = "product.putaway"
# Remove product domain to allow to select product templates
product_location_ids = fields.One2many(domain=[])
def _get_putaway_rule(self, product):
return super(PutAwayStrategy, self.with_context(
filter_putaway_rule=True))._get_putaway_rule(product)
return super(
PutAwayStrategy, self.with_context(filter_putaway_rule=True)
)._get_putaway_rule(product)
class FixedPutAwayStrategy(models.Model):
@@ -26,23 +27,25 @@ class FixedPutAwayStrategy(models.Model):
ondelete="cascade",
)
@api.depends('product_id')
@api.depends("product_id")
def _compute_product_tmpl_id(self):
for rec in self:
if rec.product_id:
rec.product_tmpl_id = rec.product_id.product_tmpl_id
else:
params = self.env.context.get('params', {})
if params.get('model', '') == 'product.template':
rec.product_tmpl_id = params.get('id', False)
params = self.env.context.get("params", {})
if params.get("model", "") == "product.template":
rec.product_tmpl_id = params.get("id", False)
def filtered(self, func):
res = super(FixedPutAwayStrategy, self).filtered(func)
if res or not self.env.context.get('filter_putaway_rule'):
if res or not self.env.context.get("filter_putaway_rule"):
return res
product = func.__closure__[0].cell_contents
if product._name != 'product.product':
if product._name != "product.product":
return res
return self.with_context(filter_putaway_rule=False).filtered(
lambda x: (x.product_tmpl_id == product.product_tmpl_id and
not x.product_id))
lambda x: (
x.product_tmpl_id == product.product_tmpl_id and not x.product_id
)
)

View File

@@ -13,114 +13,90 @@ class TestProductPutaway(TransactionCase):
ProductAttributeValue = self.env["product.attribute.value"]
TemplateAttributeLine = self.env["product.template.attribute.line"]
ref = self.env.ref
self.product_tmpl_chair = ref(
"product.product_product_11_product_template"
)
self.product_tmpl_chair = ref("product.product_product_11_product_template")
self.product_product_chair = ref("product.product_product_11")
self.category_services = ref("product.product_category_3")
self.putaway_line_1 = ref(
"stock_putaway_product_form.putaway_strat_1_line_1"
)
self.putaway_line_2 = ref(
"stock_putaway_product_form.putaway_strat_1_line_2"
)
self.putaway_line_3 = ref(
"stock_putaway_product_form.putaway_strat_2_line_1"
)
self.putaway_line_4 = ref(
"stock_putaway_product_form.putaway_strat_2_line_2"
)
self.putaway_line_1 = ref("stock_putaway_product_form.putaway_strat_1_line_1")
self.putaway_line_2 = ref("stock_putaway_product_form.putaway_strat_1_line_2")
self.putaway_line_3 = ref("stock_putaway_product_form.putaway_strat_2_line_1")
self.putaway_line_4 = ref("stock_putaway_product_form.putaway_strat_2_line_2")
# Add a product with variants
self.template = ProductTemplate.create({
'name': 'Product test',
'type': 'consu',
})
self.size_attribute = ProductAttribute.create({
'name': 'Test size',
'sequence': 1,
})
self.size_m = ProductAttributeValue.create({
'name': 'Size M',
'attribute_id': self.size_attribute.id,
'sequence': 1,
})
self.size_l = ProductAttributeValue.create({
'name': 'Size L',
'attribute_id': self.size_attribute.id,
'sequence': 2,
})
self.template_attribute_lines = TemplateAttributeLine.create({
'product_tmpl_id': self.template.id,
'attribute_id': self.size_attribute.id,
'value_ids': [(6, 0, [self.size_m.id, self.size_l.id])],
})
self.template = ProductTemplate.create(
{"name": "Product test", "type": "consu"}
)
self.size_attribute = ProductAttribute.create(
{"name": "Test size", "sequence": 1}
)
self.size_m = ProductAttributeValue.create(
{"name": "Size M", "attribute_id": self.size_attribute.id, "sequence": 1}
)
self.size_l = ProductAttributeValue.create(
{"name": "Size L", "attribute_id": self.size_attribute.id, "sequence": 2}
)
self.template_attribute_lines = TemplateAttributeLine.create(
{
"product_tmpl_id": self.template.id,
"attribute_id": self.size_attribute.id,
"value_ids": [(6, 0, [self.size_m.id, self.size_l.id])],
}
)
self.template.create_variant_ids()
def test_tmpl_has_putaways_from_products(self):
self.assertIn(
self.putaway_line_1,
self.product_tmpl_chair.product_tmpl_putaway_ids,
self.putaway_line_1, self.product_tmpl_chair.product_tmpl_putaway_ids,
)
self.putaway_line_1.product_id = self.env["product.product"]
self.assertNotIn(
self.putaway_line_1,
self.product_tmpl_chair.product_tmpl_putaway_ids,
self.putaway_line_1, self.product_tmpl_chair.product_tmpl_putaway_ids,
)
def test_tmpl_has_putaways_from_category_simple(self):
self.assertIn(
self.putaway_line_2,
self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_line_2, self.product_tmpl_chair.product_putaway_categ_ids,
)
self.product_tmpl_chair.categ_id = self.category_services
self.assertNotIn(
self.putaway_line_2,
self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_line_2, self.product_tmpl_chair.product_putaway_categ_ids,
)
def test_tmpl_has_putaways_from_category_parent(self):
# chair is under category: all/saleable/office
self.assertIn(
self.putaway_line_3,
self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_line_3, self.product_tmpl_chair.product_putaway_categ_ids,
)
self.assertNotIn(
self.putaway_line_4,
self.product_tmpl_chair.product_putaway_categ_ids,
self.putaway_line_4, self.product_tmpl_chair.product_putaway_categ_ids,
)
def test_apply_putaway(self):
# Create one strategy line for product template and other with a
# specific variant
location = self.env.ref('stock.stock_location_shop0')
location1 = location.copy({
'name': 'Location test 1',
'location_id': location.id
})
location2 = location.copy({
'name': 'Location test 2',
'location_id': location.id
})
location = self.env.ref("stock.stock_location_shop0")
location1 = location.copy(
{"name": "Location test 1", "location_id": location.id}
)
location2 = location.copy(
{"name": "Location test 2", "location_id": location.id}
)
variant1 = self.template.product_variant_ids[0]
variant2 = self.template.product_variant_ids[1]
putaway = self.putawayObj.create({'name': 'Putaway for test'})
putaway = self.putawayObj.create({"name": "Putaway for test"})
val_list = [
{
'putaway_id': putaway.id,
'product_tmpl_id': self.template.id,
'fixed_location_id': location1.id,
"putaway_id": putaway.id,
"product_tmpl_id": self.template.id,
"fixed_location_id": location1.id,
},
{
'putaway_id': putaway.id,
'product_id': variant2.id,
'fixed_location_id': location2.id,
"putaway_id": putaway.id,
"product_id": variant2.id,
"fixed_location_id": location2.id,
},
]
self.putawayLineObj.create(val_list)
location_applied = putaway._get_putaway_rule(
variant1).fixed_location_id
location_applied = putaway._get_putaway_rule(variant1).fixed_location_id
self.assertEqual(location_applied, location1)
location_applied = putaway._get_putaway_rule(
variant2).fixed_location_id
location_applied = putaway._get_putaway_rule(variant2).fixed_location_id
self.assertEqual(location_applied, location2)

View File

@@ -1,39 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl-3). -->
<odoo>
<record id="view_template_form_putaway" model="ir.ui.view">
<field name="name">product.template.product.form</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="groups_id" eval="[(4,ref('stock.group_stock_manager'))]"/>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="groups_id" eval="[(4,ref('stock.group_stock_manager'))]" />
<field name="arch" type="xml">
<xpath expr="//page[@name='inventory']" position="inside">
<div name="putaway" groups="stock.group_adv_location" string="Putaway strategies">
<field name="product_variant_ids" invisible="1"/>
<group name="putaway_products" string="Put Away Strategies by products"
attrs="{'invisible': [('is_product_variant', '=', True)]}">
<div
name="putaway"
groups="stock.group_adv_location"
string="Putaway strategies"
>
<field name="product_variant_ids" invisible="1" />
<group
name="putaway_products"
string="Put Away Strategies by products"
attrs="{'invisible': [('is_product_variant', '=', True)]}"
>
<group>
<p class="oe_grey">
The rules defined per product will be applied before the rules defined per product category.
<div>Keep empty product field to apply strategy to all variants.</div>
<div
>Keep empty product field to apply strategy to all variants.</div>
</p>
</group>
<field name="product_tmpl_putaway_ids" nolabel="1">
<tree editable="top">
<field name="product_id" domain="[('product_tmpl_id', '=', parent.id)]"/>
<field name="putaway_id"/>
<field name="fixed_location_id"/>
<field
name="product_id"
domain="[('product_tmpl_id', '=', parent.id)]"
/>
<field name="putaway_id" />
<field name="fixed_location_id" />
</tree>
</field>
</group>
<group name="putaway_categ" string="Put Away Strategies by category"
attrs="{'readonly': 1}">
<field name="product_putaway_categ_ids"
nolabel="1">
<group
name="putaway_categ"
string="Put Away Strategies by category"
attrs="{'readonly': 1}"
>
<field name="product_putaway_categ_ids" nolabel="1">
<tree>
<field name="category_id"/>
<field name="putaway_id" widget="selection"/>
<field name="fixed_location_id"/>
<field name="category_id" />
<field name="putaway_id" widget="selection" />
<field name="fixed_location_id" />
</tree>
</field>
</group>
@@ -41,20 +54,26 @@
</xpath>
</field>
</record>
<record id="view_product_form_putaway" model="ir.ui.view">
<field name="name">product.product.form.putaway</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="groups_id" eval="[(4,ref('stock.group_stock_manager'))]"/>
<field name="inherit_id" ref="product.product_normal_form_view" />
<field name="groups_id" eval="[(4,ref('stock.group_stock_manager'))]" />
<field name="arch" type="xml">
<xpath expr="//group[@name='putaway_categ']" position="before">
<group name="putaway_products" string="Put Away Strategies by products"
attrs="{'invisible': [('is_product_variant', '=', False)]}">
<field name="product_product_putaway_ids" groups="stock.group_adv_location" nolabel="1">
<group
name="putaway_products"
string="Put Away Strategies by products"
attrs="{'invisible': [('is_product_variant', '=', False)]}"
>
<field
name="product_product_putaway_ids"
groups="stock.group_adv_location"
nolabel="1"
>
<tree editable="bottom">
<field name="putaway_id"/>
<field name="fixed_location_id"/>
<field name="putaway_id" />
<field name="fixed_location_id" />
</tree>
</field>
</group>