From 8785bc74770f1029aef16582c8bcf8aa63d90b31 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 3 Aug 2022 14:41:48 +0000 Subject: [PATCH] [FIX] stock: move group inside the view The goal being to get rid of the `groups_id` feature for backend views This one is a tricky case The `create` button should not be visible in the tree and form views of stock locations when the multi locations feature is not enabled. If you don't have the group, you do not see the menu bringing you to the locations list, but though it's still possible to reach the location form view through a many2one link on a location field missing the `groups="stock.group_stock_multi_locations"` e.g. on the stock move form. If you managed to reach a location form whatever the way, if you do not have the multi locations enabled, you shouldn't see the create button. As we would like to remove the `groups_id` feature of the `ir.ui.view`, we must find an alternative. This is the only case using `groups_id` to remove the create/edit button, so it's not like it's such a common way to achieve this goal. The right way to achieve this should be an ACL, preventing you to create a location at all (even through xmlrpc) if you do not have the multi-location feature enabled. Though, currently, there is an ACL giving the right to inventory managers to create locations, and this is unfortunately impossible in the ACLs model to make a combined group rule, like you need both the groups multi-locations and inventory manager to be allowed to create locations. Hence, as alternative, we enable/disable the view removing the create button from the list and form views when the multi-locations is enabled/disabled in the settings. Part-of: odoo/odoo#98551 --- addons/stock/models/res_config_settings.py | 16 ++++++++++++++++ addons/stock/views/stock_location_views.xml | 10 ++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/addons/stock/models/res_config_settings.py b/addons/stock/models/res_config_settings.py index e8ab3993e3a..860612070a8 100644 --- a/addons/stock/models/res_config_settings.py +++ b/addons/stock/models/res_config_settings.py @@ -103,11 +103,27 @@ class ResConfigSettings(models.TransientModel): if self.group_stock_multi_locations and not previous_group.get('group_stock_multi_locations'): # override active_test that is false in set_values warehouse_obj.with_context(active_test=True).search([]).int_type_id.active = True + # Disable the views removing the create button from the location list and form. + # Be resilient if the views have been deleted manually. + for view in ( + self.env.ref('stock.stock_location_view_tree2_editable', raise_if_not_found=False), + self.env.ref('stock.stock_location_view_form_editable', raise_if_not_found=False), + ): + if view: + view.active = False elif not self.group_stock_multi_locations and previous_group.get('group_stock_multi_locations'): warehouse_obj.search([ ('reception_steps', '=', 'one_step'), ('delivery_steps', '=', 'ship_only') ]).int_type_id.active = False + # Enable the views removing the create button from the location list and form. + # Be resilient if the views have been deleted manually. + for view in ( + self.env.ref('stock.stock_location_view_tree2_editable', raise_if_not_found=False), + self.env.ref('stock.stock_location_view_form_editable', raise_if_not_found=False), + ): + if view: + view.active = True if not was_operations_showed and self.env['stock.picking.type'].with_user(SUPERUSER_ID)._default_show_operations(): self.env['stock.picking.type'].with_context(active_test=False).sudo().search([ diff --git a/addons/stock/views/stock_location_views.xml b/addons/stock/views/stock_location_views.xml index 1ae9296d266..b6d63bcaf0a 100644 --- a/addons/stock/views/stock_location_views.xml +++ b/addons/stock/views/stock_location_views.xml @@ -14,7 +14,7 @@ stock.location.form stock.location -
+
@@ -61,10 +61,9 @@ stock.location.form.editable stock.location - - true + false @@ -91,7 +90,7 @@ stock.location - + @@ -105,10 +104,9 @@ stock.location.tree2.editable stock.location - - true + false