From 690db5e8dcbf9f49d9f3d2f8a95606b7b8c09637 Mon Sep 17 00:00:00 2001 From: Carlos Serra-Toro Date: Fri, 29 Jan 2021 13:40:35 +0100 Subject: [PATCH] [FIX] stock_buffer_sales_analysis: attending the code review Several changes were done because of the code review: * The category of the module is now "Warehouse", in alignment with the categories user in OCA (https://odoo-community.org/shop) * The list of fields in the depends() for the method _compute_last_inventory_date() missed several fields, that are now listed as dependencies. * A new unit test has been added to account for the concern of the functional needing a compute_sudo in its definition, just in case a non privileged stock user was stuck into permission problems. --- .../__manifest__.py | 2 +- .../models/stock_location.py | 7 ++- .../tests/test_stock_location.py | 47 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/stock_location_last_inventory_date/__manifest__.py b/stock_location_last_inventory_date/__manifest__.py index 51ee8ff81..9422a8e19 100644 --- a/stock_location_last_inventory_date/__manifest__.py +++ b/stock_location_last_inventory_date/__manifest__.py @@ -5,7 +5,7 @@ "summary": "Show the last inventory date for a leaf location", "version": "13.0.1.0.0", "development_status": "Alpha", - "category": "Warehouse Management", + "category": "Warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse", "author": "Camptocamp, Odoo Community Association (OCA)", "license": "AGPL-3", diff --git a/stock_location_last_inventory_date/models/stock_location.py b/stock_location_last_inventory_date/models/stock_location.py index 9e6dbde8f..a086b8612 100644 --- a/stock_location_last_inventory_date/models/stock_location.py +++ b/stock_location_last_inventory_date/models/stock_location.py @@ -29,10 +29,13 @@ class StockLocation(models.Model): ) @api.depends( + "usage", + "child_ids", "validated_inventory_ids", "validated_inventory_ids.date", - "validated_inventory_ids.usage", - "validated_inventory_ids.child_ids", + "validated_inventory_ids.state", + "validated_inventory_ids.location_ids.usage", + "validated_inventory_ids.location_ids.child_ids", ) def _compute_last_inventory_date(self): """Store date of the last inventory for each leaf location""" diff --git a/stock_location_last_inventory_date/tests/test_stock_location.py b/stock_location_last_inventory_date/tests/test_stock_location.py index 4cd3f452f..11ecdbbff 100644 --- a/stock_location_last_inventory_date/tests/test_stock_location.py +++ b/stock_location_last_inventory_date/tests/test_stock_location.py @@ -1,5 +1,7 @@ # Copyright 2021 Camptocamp SA # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import fields +from odoo.exceptions import AccessError from odoo.tests import SavepointCase @@ -11,6 +13,51 @@ class TestStockLocation(SavepointCase): cls.leaf_location = cls.env.ref("stock.location_refrigerator_small") cls.top_location = cls.leaf_location.location_id + def _create_user(self, name, groups): + return ( + self.env["res.users"] + .with_context({"no_reset_password": True}) + .create( + { + "name": name.capitalize(), + "password": "password", + "login": name, + "email": "{}@test.com".format(name.lower()), + "groups_id": [(6, 0, groups.ids)], + "company_ids": [(6, 0, self.env["res.company"].search([]).ids)], + } + ) + ) + + def test_leaf_location_non_privileged_user(self): + stock_user = self._create_user( + "Stock Normal", self.env.ref("stock.group_stock_user") + ) + stock_manager = self._create_user( + "Stock Admin", self.env.ref("stock.group_stock_manager") + ) + inventory = self.env["stock.inventory"].create( + { + "name": "Inventory Adjustment", + "product_ids": [(4, self.product.id)], + "location_ids": [(4, self.leaf_location.id)], + } + ) + inventory.with_user(stock_user).action_start() + inventory.with_user(stock_manager).action_validate() + self.assertEqual( + self.leaf_location.with_user(stock_user).validated_inventory_ids.ids, + [inventory.id], + ) + self.assertEqual( + self.leaf_location.with_user(stock_user).last_inventory_date, inventory.date + ) + try: + # Triggers the computation indirectly, `date` is in the depends. + inventory.with_user(stock_user).date = fields.Datetime.now() + except AccessError: + self.fail("A non-privileged user could not trigger the recomputation.") + def test_leaf_location(self): self.assertFalse(self.leaf_location.child_ids) self.assertFalse(self.leaf_location.validated_inventory_ids)