Improve code after reviews

* performance (less queries executed)
* disable tracking in tests (faster)
* use with_user instead of sudo
* the double for loop in _compute_route_ids actually generates one more
  query as the simple one in this commit (thanks to the cache)
* extract a method
* the parent locations can be found using 'parent_of' which will use the
  parent_path under the hood (example: 1/7/8 will return locations 1, 7,
  8)

[UPD] Update stock_orderpoint_route.pot
This commit is contained in:
Guewen Baconnier
2020-03-17 11:44:58 +01:00
committed by Dũng (Trần Đình)
parent 2d09392b16
commit a53fd653c3
3 changed files with 57 additions and 28 deletions

View File

@@ -0,0 +1,29 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * stock_orderpoint_route
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 13.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: stock_orderpoint_route
#: model:ir.model.fields,field_description:stock_orderpoint_route.field_stock_warehouse_orderpoint__route_ids
msgid "Allowed routes"
msgstr ""
#. module: stock_orderpoint_route
#: model:ir.model,name:stock_orderpoint_route.model_stock_warehouse_orderpoint
msgid "Minimum Inventory Rule"
msgstr ""
#. module: stock_orderpoint_route
#: model:ir.model.fields,field_description:stock_orderpoint_route.field_stock_warehouse_orderpoint__route_id
msgid "Route"
msgstr ""

View File

@@ -20,33 +20,32 @@ class StockWarehouseOrderpoint(models.Model):
@api.depends("product_id", "warehouse_id", "warehouse_id.route_ids", "location_id")
def _compute_route_ids(self):
route_obj = self.env["stock.location.route"]
for wh in self.mapped("warehouse_id"):
wh_routes = wh.route_ids
for record in self.filtered(lambda r: r.warehouse_id == wh):
routes = route_obj.browse()
if record.product_id:
routes += record.product_id.mapped(
"route_ids"
) | record.product_id.mapped("categ_id").mapped("total_route_ids")
if record.warehouse_id:
routes |= wh_routes
parents = record.get_parents()
record.route_ids = routes.filtered(
lambda route: any(
p.location_id in parents
for p in route.rule_ids.filtered(
lambda rule: rule.action in ("pull", "pull_push")
).mapped("location_src_id")
)
)
for record in self:
wh_routes = record.warehouse_id.route_ids
routes = route_obj.browse()
if record.product_id:
routes += record.product_id.mapped(
"route_ids"
) | record.product_id.mapped("categ_id").mapped("total_route_ids")
if record.warehouse_id:
routes |= wh_routes
parents = record.get_parents()
record.route_ids = self._get_location_routes_of_parents(routes, parents)
def _get_location_routes_of_parents(self, routes, parents):
return routes.filtered(
lambda route: any(
p.location_id in parents
for p in route.rule_ids.filtered(
lambda rule: rule.action in ("pull", "pull_push")
).mapped("location_src_id")
)
)
def get_parents(self):
location = self.location_id
result = location
while location.location_id:
location = location.location_id
result |= location
return result
return self.env["stock.location"].search(
[("id", "parent_of", self.location_id.id)]
)
def _prepare_procurement_values(self, product_qty, date=False, group=False):
res = super()._prepare_procurement_values(product_qty, date=date, group=group)

View File

@@ -8,6 +8,7 @@ class TestStockOrderpointRoute(common.SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
# common models
cls.orderpoint_model = cls.env["stock.warehouse.orderpoint"]
@@ -99,7 +100,7 @@ class TestStockOrderpointRoute(common.SavepointCase):
def _create_user(cls, name, group_ids, company_ids):
return (
cls.env["res.users"]
.with_context({"no_reset_password": True})
.with_context(no_reset_password=True)
.create(
{
"name": name,
@@ -136,7 +137,7 @@ class TestStockOrderpointRoute(common.SavepointCase):
"location_id": self.warehouse.lot_stock_id.id,
}
orderpoint = self.orderpoint_model.sudo(self.stock_manager).create(vals)
orderpoint = self.orderpoint_model.with_user(self.stock_manager).create(vals)
self.assertIn(self.route, orderpoint.route_ids)
self.assertIn(self.route2, orderpoint.route_ids)
orderpoint.route_id = self.route.id
@@ -161,7 +162,7 @@ class TestStockOrderpointRoute(common.SavepointCase):
"location_id": self.warehouse.lot_stock_id.id,
}
orderpoint = self.orderpoint_model.sudo(self.stock_manager).create(vals)
orderpoint = self.orderpoint_model.with_user(self.stock_manager).create(vals)
self.assertIn(self.route, orderpoint.route_ids)
self.assertIn(self.route2, orderpoint.route_ids)
orderpoint.route_id = self.route2.id