Skip to content

Commit

Permalink
[ADD] changes in user_has_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
lef-adhoc committed Sep 24, 2024
1 parent 20d98f8 commit 380f070
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
18 changes: 17 additions & 1 deletion odoo_module_migrate/migration_scripts/migrate_170_180.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
22 changes: 22 additions & 0 deletions tests/data_result/module_170_180/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions tests/data_template/module_170/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 380f070

Please sign in to comment.