From 380f070d227505829a0cef634c253fc37aab7c4f Mon Sep 17 00:00:00 2001 From: Franco Leyes Date: Wed, 18 Sep 2024 17:44:33 -0300 Subject: [PATCH] [ADD] changes in user_has_groups --- .../migration_scripts/migrate_170_180.py | 18 ++++++++++++++- .../module_170_180/models/res_partner.py | 22 +++++++++++++++++++ .../module_170/models/res_partner.py | 22 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/odoo_module_migrate/migration_scripts/migrate_170_180.py b/odoo_module_migrate/migration_scripts/migrate_170_180.py index 05c3e5a6..fe0449d7 100644 --- a/odoo_module_migrate/migration_scripts/migrate_170_180.py +++ b/odoo_module_migrate/migration_scripts/migrate_170_180.py @@ -51,5 +51,21 @@ def replace_tree_with_list_in_views( logger.error(f"Error processing file {file}: {str(e)}") +def replace_user_has_groups( + logger, module_path, module_name, manifest_path, migration_steps, tools +): + files_to_process = tools.get_files(module_path, (".py")) + replaces = { + r"self\.user_has_groups\(\s*(['\"])([\w\.]+)\1\s*\)": r"self.env.user.has_group(\1\2\1)", + r"self\.user_has_groups\(\s*(['\"])([^'\"]*[,!][^'\"]*?)\1\s*\)": r"self.env.user.has_groups(\1\2\1)", + } + + for file in files_to_process: + try: + tools._replace_in_file(file, replaces) + except Exception as e: + logger.error(f"Error processing file {file}: {str(e)}") + + class MigrationScript(BaseMigrationScript): - _GLOBAL_FUNCTIONS = [replace_tree_with_list_in_views] + _GLOBAL_FUNCTIONS = [replace_tree_with_list_in_views, replace_user_has_groups] diff --git a/tests/data_result/module_170_180/models/res_partner.py b/tests/data_result/module_170_180/models/res_partner.py index cd71a68b..21b4ba9d 100644 --- a/tests/data_result/module_170_180/models/res_partner.py +++ b/tests/data_result/module_170_180/models/res_partner.py @@ -8,3 +8,25 @@ class ResPartner(models.Model): def example_method(self): self.env.ref('module_name.tree_view').write({'view_mode': 'list'}) + + def example_method_has_group(self): + if self.env.user.has_group('base.group_user'): + pass + + if self.env.user.has_group("base.group_user"): + pass + + if self.env.user.has_groups("!base.group_user"): + pass + + if self.env.user.has_groups('base.group_user,base.group_manager'): + pass + + if self.env.user.has_groups("base.group_user,base.group_manager,base.group"): + pass + + if self.env.user.has_groups('base.group_user,!base.group_manager'): + pass + + if self.env.user.has_groups("base.group_user,!base.group_manager,!base.group"): + pass diff --git a/tests/data_template/module_170/models/res_partner.py b/tests/data_template/module_170/models/res_partner.py index cdd96377..36281105 100644 --- a/tests/data_template/module_170/models/res_partner.py +++ b/tests/data_template/module_170/models/res_partner.py @@ -8,3 +8,25 @@ class ResPartner(models.Model): def example_method(self): self.env.ref('module_name.tree_view').write({'view_mode': 'tree'}) + + def example_method_has_group(self): + if self.user_has_groups('base.group_user'): + pass + + if self.user_has_groups("base.group_user"): + pass + + if self.user_has_groups("!base.group_user"): + pass + + if self.user_has_groups('base.group_user,base.group_manager'): + pass + + if self.user_has_groups("base.group_user,base.group_manager,base.group"): + pass + + if self.user_has_groups('base.group_user,!base.group_manager'): + pass + + if self.user_has_groups("base.group_user,!base.group_manager,!base.group"): + pass