Skip to content

Commit

Permalink
[15.0][FIX] change role_line_ids default value to override create
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloYam committed Aug 21, 2024
1 parent 165946f commit 89bd8cd
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions base_user_role/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ResUsers(models.Model):
comodel_name="res.users.role.line",
inverse_name="user_id",
string="Role lines",
default=lambda self: self._default_role_lines(),
groups="base.group_erp_manager",
)
role_ids = fields.One2many(
Expand All @@ -21,30 +20,30 @@ class ResUsers(models.Model):
groups="base.group_erp_manager",
)

@api.model
def _default_role_lines(self):
default_user = self.env.ref("base.default_user", raise_if_not_found=False)
default_values = []
if default_user:
for role_line in default_user.with_context(active_test=False).role_line_ids:
default_values.append(
{
"role_id": role_line.role_id.id,
"date_from": role_line.date_from,
"date_to": role_line.date_to,
"is_enabled": role_line.is_enabled,
}
)
return default_values

@api.depends("role_line_ids.role_id")
def _compute_role_ids(self):
for user in self:
user.role_ids = user.role_line_ids.mapped("role_id")

def _set_roles_from_default_user(self, record):
default_user = self.env.ref("base.default_user", raise_if_not_found=False)
role_line_model = self.env['res.users.role.line']
if not default_user:
return

Check warning on line 33 in base_user_role/models/user.py

View check run for this annotation

Codecov / codecov/patch

base_user_role/models/user.py#L33

Added line #L33 was not covered by tests
role_lines = default_user.with_context(active_test=False).role_line_ids
for user in record:
for role_line in role_lines:
role_line_model.create({
'role_id': role_line.role_id.id,
'is_enabled': role_line.is_enabled,
'user_id': user.id
})

@api.model
def create(self, vals):
new_record = super(ResUsers, self).create(vals)
self._set_roles_from_default_user(new_record)
new_record.set_groups_from_roles()
return new_record

Expand Down

0 comments on commit 89bd8cd

Please sign in to comment.