Skip to content

Commit 6217a00

Browse files
committed
fix(spp_user_roles): prevent validation error when assigning groups to a new role
1 parent 98a45a9 commit 6217a00

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

spp_user_roles/models/role.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44

5-
from odoo import fields, models
5+
from odoo import api, fields, models
66

77
_logger = logging.getLogger(__name__)
88

@@ -12,6 +12,29 @@ class ResUsersRoleCustomSPP(models.Model):
1212

1313
role_type = fields.Selection([("local", "Local"), ("global", "Global")], default="global")
1414

15+
@api.model_create_multi
16+
def create(self, vals_list):
17+
# Workaround: same Odoo cache-clearing bug as in base_user_role's write()
18+
# override. When res.groups fields are set via _inherits on create(),
19+
# implied_ids gets dropped. Extract group fields and write them to
20+
# group_id directly after creation, mirroring the write() fix.
21+
groups_vals_list = []
22+
group_fields = set(self.env["res.groups"]._fields) - {"name"}
23+
for vals in vals_list:
24+
group_vals = {}
25+
for field in group_fields:
26+
if field in vals:
27+
group_vals[field] = vals.pop(field)
28+
groups_vals_list.append(group_vals)
29+
30+
new_records = super().create(vals_list)
31+
32+
for record, group_vals in zip(new_records, groups_vals_list):
33+
if group_vals:
34+
record.group_id.write(group_vals)
35+
36+
return new_records
37+
1538
def action_update_users(self):
1639
"""
1740
Call the update_users function to force the update of associated users in the role.

spp_user_roles/views/role.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020
<xpath expr="//field[@name='name']" position="after">
2121
<field name="role_type" />
2222
</xpath>
23-
<xpath expr="//field[@name='implied_ids']" position="attributes">
24-
<attribute name="domain">[('role_id', '=', False)]</attribute>
25-
<attribute name="widget">many2many</attribute>
23+
<xpath expr="//field[@name='implied_ids']" position="replace">
24+
<field
25+
name="implied_ids"
26+
nolabel="1"
27+
domain="[('role_id', '=', False)]"
28+
>
29+
<list create="0">
30+
<field name="full_name" string="Group" />
31+
</list>
32+
</field>
2633
</xpath>
2734
<xpath
2835
expr="//field[@name='line_ids']/list/field[@name='user_id']"

0 commit comments

Comments
 (0)