diff --git a/base_user_role/__manifest__.py b/base_user_role/__manifest__.py index d4f107c9..fdab11b8 100644 --- a/base_user_role/__manifest__.py +++ b/base_user_role/__manifest__.py @@ -3,7 +3,7 @@ { "name": "User roles", - "version": "12.0.2.0.1", + "version": "12.0.2.1.0", "category": "Tools", "author": "ABF OSIELL, Odoo Community Association (OCA)", "license": "AGPL-3", diff --git a/base_user_role/data/ir_module_category.xml b/base_user_role/data/ir_module_category.xml index 77884e74..89502064 100644 --- a/base_user_role/data/ir_module_category.xml +++ b/base_user_role/data/ir_module_category.xml @@ -3,8 +3,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> - - User roles - + + User roles + 200 + diff --git a/base_user_role/migrations/12.0.2.1.0/post-migration.py b/base_user_role/migrations/12.0.2.1.0/post-migration.py new file mode 100644 index 00000000..75a79797 --- /dev/null +++ b/base_user_role/migrations/12.0.2.1.0/post-migration.py @@ -0,0 +1,21 @@ +# Copyright (C) 2021 - Today: GRAP (http://www.grap.coop) +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging +from odoo import api, SUPERUSER_ID + +_logger = logging.getLogger(__name__) + + +def _affect_group_to_category(env): + _logger.info("Set Category 'User roles' to groups related to roles") + roles = env["res.users.role"].search([]) + groups = roles.mapped("group_id").filtered(lambda x: not x.category_id) + user_role_category = env.ref("base_user_role.ir_module_category_role") + groups.write({"category_id": user_role_category.id}) + + +def migrate(cr, version): + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + _affect_group_to_category(env) diff --git a/base_user_role/models/role.py b/base_user_role/models/role.py index 9cb6b5de..f537d340 100644 --- a/base_user_role/models/role.py +++ b/base_user_role/models/role.py @@ -31,16 +31,18 @@ class ResUsersRole(models.Model): string="Users list", compute="_compute_user_ids", ) + # TODO, remove in next version as it is not used in the whole module + # kept here for legacy reason group_category_id = fields.Many2one( + comodel_name="ir.module.category", related="group_id.category_id", - default=lambda cls: cls.env.ref( - "base_user_role.ir_module_category_role" - ).id, string="Associated category", - help="Associated group's category", ) comment = fields.Html(string="Internal Notes") + def _default_category_id(self): + return self.env.ref("base_user_role.ir_module_category_role") + @api.multi @api.depends("line_ids.user_id") def _compute_user_ids(self): @@ -49,6 +51,8 @@ class ResUsersRole(models.Model): @api.model def create(self, vals): + if "category_id" not in vals and "group_id" not in vals: + vals.update({"category_id": self._default_category_id().id}) new_record = super(ResUsersRole, self).create(vals) new_record.update_users() return new_record diff --git a/base_user_role/tests/test_user_role.py b/base_user_role/tests/test_user_role.py index 9187424c..51ebe137 100644 --- a/base_user_role/tests/test_user_role.py +++ b/base_user_role/tests/test_user_role.py @@ -297,3 +297,11 @@ class TestUserRole(SavepointCase): role_group_ids = sorted(set(role_group_ids)) # Check that user have groups implied by role 2 self.assertEqual(user_group_ids, role_group_ids) + + def test_user_role_category(self): + # Check that groups created by role has the correct + # default category + self.assertEqual( + self.env.ref("base_user_role.ir_module_category_role").id, + self.role1_id.category_id.id + )