From 6d265c245c4e5b9ee39a2f7f3719c167575d7c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Thu, 12 Jan 2023 13:55:40 +0100 Subject: [PATCH] [IMP] stock_request: Add domain to some fields to prevent deprecate methods with domain return TT41080 --- .../models/stock_request_abstract.py | 49 +++++++-------- stock_request/models/stock_request_order.py | 18 +++--- stock_request/tests/test_stock_request.py | 62 +++++++------------ .../views/stock_request_order_views.xml | 5 ++ stock_request/views/stock_request_views.xml | 4 +- .../views/stock_request_kanban_views.xml | 3 + 6 files changed, 62 insertions(+), 79 deletions(-) diff --git a/stock_request/models/stock_request_abstract.py b/stock_request/models/stock_request_abstract.py index 4de85e6fe..9c7121704 100644 --- a/stock_request/models/stock_request_abstract.py +++ b/stock_request/models/stock_request_abstract.py @@ -37,12 +37,17 @@ class StockRequest(models.AbstractModel): name = fields.Char("Name", copy=False, required=True, readonly=True, default="/") warehouse_id = fields.Many2one( - "stock.warehouse", "Warehouse", ondelete="cascade", required=True + comodel_name="stock.warehouse", + string="Warehouse", + check_company=True, + ondelete="cascade", + required=True, ) location_id = fields.Many2one( - "stock.location", - "Location", - domain=[("usage", "in", ["internal", "transit"])], + comodel_name="stock.location", + string="Location", + domain="not allow_virtual_location and " + "[('usage', 'in', ['internal', 'transit'])] or []", ondelete="cascade", required=True, ) @@ -56,9 +61,11 @@ class StockRequest(models.AbstractModel): allow_virtual_location = fields.Boolean( related="company_id.stock_request_allow_virtual_loc", readonly=True ) + allowed_uom_categ_id = fields.Many2one(related="product_id.uom_id.category_id") product_uom_id = fields.Many2one( - "uom.uom", - "Product Unit of Measure", + comodel_name="uom.uom", + string="Product Unit of Measure", + domain="[('category_id', '=?', allowed_uom_categ_id)]", required=True, default=lambda self: self._context.get("product_uom_id", False), ) @@ -210,20 +217,18 @@ class StockRequest(models.AbstractModel): @api.onchange("warehouse_id") def onchange_warehouse_id(self): """Finds location id for changed warehouse.""" - res = {"domain": {}} if self._name == "stock.request" and self.order_id: # When the stock request is created from an order the wh and # location are taken from the order and we rely on it to change # all request associated. Thus, no need to apply # the onchange, as it could lead to inconsistencies. - return res + return if self.warehouse_id: loc_wh = self.location_id.get_warehouse() if self.warehouse_id != loc_wh: self.location_id = self.warehouse_id.lot_stock_id.id if self.warehouse_id.company_id != self.company_id: self.company_id = self.warehouse_id.company_id - return res @api.onchange("location_id") def onchange_location_id(self): @@ -233,33 +238,23 @@ class StockRequest(models.AbstractModel): self.warehouse_id = loc_wh self.with_context(no_change_childs=True).onchange_warehouse_id() - @api.onchange("allow_virtual_location") - def onchange_allow_virtual_location(self): - if self.allow_virtual_location: - return {"domain": {"location_id": []}} - @api.onchange("company_id") def onchange_company_id(self): - """Sets a default warehouse when the company is changed and limits - the user selection of warehouses.""" + """Sets a default warehouse when the company is changed.""" if self.company_id and ( not self.warehouse_id or self.warehouse_id.company_id != self.company_id ): self.warehouse_id = self.env["stock.warehouse"].search( - [("company_id", "=", self.company_id.id)], limit=1 + [ + "|", + ("company_id", "=", False), + ("company_id", "=", self.company_id.id), + ], + limit=1, ) self.onchange_warehouse_id() - return {"domain": {"warehouse_id": [("company_id", "=", self.company_id.id)]}} - @api.onchange("product_id") def onchange_product_id(self): - res = {"domain": {}} if self.product_id: - self.product_uom_id = self.product_id.uom_id.id - res["domain"]["product_uom_id"] = [ - ("category_id", "=", self.product_id.uom_id.category_id.id) - ] - return res - res["domain"]["product_uom_id"] = [] - return res + self.product_uom_id = self.product_id.uom_id diff --git a/stock_request/models/stock_request_order.py b/stock_request/models/stock_request_order.py index 4c23c40d1..040b64498 100644 --- a/stock_request/models/stock_request_order.py +++ b/stock_request/models/stock_request_order.py @@ -58,18 +58,20 @@ class StockRequestOrder(models.Model): default=lambda s: s._get_default_requested_by(), ) warehouse_id = fields.Many2one( - "stock.warehouse", - "Warehouse", + comodel_name="stock.warehouse", + string="Warehouse", + check_company=True, readonly=True, ondelete="cascade", required=True, states={"draft": [("readonly", False)]}, ) location_id = fields.Many2one( - "stock.location", - "Location", + comodel_name="stock.location", + string="Location", + domain="not allow_virtual_location and " + "[('usage', 'in', ['internal', 'transit'])] or []", readonly=True, - domain=[("usage", "in", ["internal", "transit"])], ondelete="cascade", required=True, states={"draft": [("readonly", False)]}, @@ -177,11 +179,6 @@ class StockRequestOrder(models.Model): self.with_context(no_change_childs=True).onchange_warehouse_id() self.change_childs() - @api.onchange("allow_virtual_location") - def onchange_allow_virtual_location(self): - if self.allow_virtual_location: - return {"domain": {"location_id": []}} - @api.onchange("warehouse_id") def onchange_warehouse_id(self): if self.warehouse_id: @@ -209,7 +206,6 @@ class StockRequestOrder(models.Model): ) self.with_context(no_change_childs=True).onchange_warehouse_id() self.change_childs() - return {"domain": {"warehouse_id": [("company_id", "=", self.company_id.id)]}} def change_childs(self): if not self._context.get("no_change_childs", False): diff --git a/stock_request/tests/test_stock_request.py b/stock_request/tests/test_stock_request.py index 6b9e5356d..a21bc02c8 100644 --- a/stock_request/tests/test_stock_request.py +++ b/stock_request/tests/test_stock_request.py @@ -5,12 +5,12 @@ from collections import Counter from datetime import datetime from odoo import exceptions, fields -from odoo.tests import common +from odoo.tests import common, new_test_user class TestStockRequest(common.TransactionCase): def setUp(self): - super(TestStockRequest, self).setUp() + super().setUp() # common models self.stock_request = self.env["stock.request"] @@ -20,9 +20,6 @@ class TestStockRequest(common.TransactionCase): self.stock_request_user_group = self.env.ref( "stock_request.group_stock_request_user" ) - self.stock_request_manager_group = self.env.ref( - "stock_request.group_stock_request_manager" - ) self.main_company = self.env.ref("base.main_company") self.warehouse = self.env.ref("stock.warehouse0") self.categ_unit = self.env.ref("uom.product_uom_categ_unit") @@ -40,15 +37,25 @@ class TestStockRequest(common.TransactionCase): self.wh2 = self.env["stock.warehouse"].search( [("company_id", "=", self.company_2.id)], limit=1 ) - self.stock_request_user = self._create_user( - "stock_request_user", - [self.stock_request_user_group.id], - [self.main_company.id, self.company_2.id], + ctx = { + "mail_create_nolog": True, + "mail_create_nosubscribe": True, + "mail_notrack": True, + "no_reset_password": True, + } + self.stock_request_user = new_test_user( + self.env, + login="stock_request_user", + groups="stock_request.group_stock_request_user", + company_ids=[(6, 0, [self.main_company.id, self.company_2.id])], + context=ctx, ) - self.stock_request_manager = self._create_user( - "stock_request_manager", - [self.stock_request_manager_group.id], - [self.main_company.id, self.company_2.id], + self.stock_request_manager = new_test_user( + self.env, + login="stock_request_manager", + groups="stock_request.group_stock_request_manager", + company_ids=[(6, 0, [self.main_company.id, self.company_2.id])], + context=ctx, ) self.product = self._create_product("SH", "Shoes", False) self.product_company_2 = self._create_product( @@ -135,22 +142,6 @@ class TestStockRequest(common.TransactionCase): "stock.no_auto_scheduler", "True" ) - def _create_user(self, name, group_ids, company_ids): - return ( - self.env["res.users"] - .with_context({"no_reset_password": True}) - .create( - { - "name": name, - "password": "demo", - "login": name, - "email": "@".join([name, "test.com"]), - "groups_id": [(6, 0, group_ids)], - "company_ids": [(6, 0, company_ids)], - } - ) - ) - def _create_product(self, default_code, name, company_id, **vals): return self.env["product.product"].create( dict( @@ -166,7 +157,7 @@ class TestStockRequest(common.TransactionCase): class TestStockRequestBase(TestStockRequest): def setUp(self): - super(TestStockRequestBase, self).setUp() + super().setUp() def test_defaults(self): vals = { @@ -290,20 +281,13 @@ class TestStockRequestBase(TestStockRequest): # Test onchange_product_id stock_request.product_id = product - res = stock_request.onchange_product_id() + stock_request.onchange_product_id() - self.assertEqual( - res["domain"]["product_uom_id"], - [("category_id", "=", product.uom_id.category_id.id)], - ) self.assertEqual( stock_request.product_uom_id, self.env.ref("uom.product_uom_kgm") ) stock_request.product_id = self.env["product.product"] - res = stock_request.onchange_product_id() - - self.assertEqual(res["domain"]["product_uom_id"], []) # Test onchange_warehouse_id wh2_2 = ( @@ -962,7 +946,6 @@ class TestStockRequestBase(TestStockRequest): stock_request = self.stock_request.with_user(self.stock_request_user).create( vals ) - stock_request.onchange_allow_virtual_location() self.assertTrue(stock_request.allow_virtual_location) vals = { "company_id": self.main_company.id, @@ -970,7 +953,6 @@ class TestStockRequestBase(TestStockRequest): "location_id": self.virtual_loc.id, } order = self.request_order.with_user(self.stock_request_user).create(vals) - order.onchange_allow_virtual_location() self.assertTrue(order.allow_virtual_location) def test_onchange_wh_no_effect_from_order(self): diff --git a/stock_request/views/stock_request_order_views.xml b/stock_request/views/stock_request_order_views.xml index fd3480065..9d100ceec 100644 --- a/stock_request/views/stock_request_order_views.xml +++ b/stock_request/views/stock_request_order_views.xml @@ -121,6 +121,7 @@ + + diff --git a/stock_request/views/stock_request_views.xml b/stock_request/views/stock_request_views.xml index bc8efd2f5..83a23983b 100644 --- a/stock_request/views/stock_request_views.xml +++ b/stock_request/views/stock_request_views.xml @@ -22,6 +22,7 @@ groups="stock.group_stock_multi_locations" /> + +
+ + +
+