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 Oct 2, 2024
1 parent a4251d1 commit 15776da
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
17 changes: 16 additions & 1 deletion odoo_module_migrate/migration_scripts/migrate_170_180.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def replace_chatter_self_closing(match):
reg_xpath_chatter: replacement_xpath,
reg_chatter_with_position_self_closing: replace_chatter_self_closing,
}

for file in files_to_process:
try:
tools._replace_in_file(
Expand All @@ -83,9 +82,25 @@ def replace_chatter_self_closing(match):
except Exception as e:
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,
replace_chatter_blocks,
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 15776da

Please sign in to comment.