1

I am using odoo version 16, earlier I am using odoo version 11 and i want to upgrade my code from v11 to v16.

I am stuck at one method where I want to make some fields read-only from defined configured fields(many2many field in setting) for a specific group (except manager group). This code worked for v11 but not working on v16.

So, anyone can help me out with this problem?

My code is below:

class HrEmployeeResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    editable_fields = fields.Many2many('ir.model.fields', 'editable_fields_rel', 'model_id', 'id',
                                       string='Editable Field', domain="[('model_id', '=', 'hr.employee')]")

class CustomEmployee(models.Model):
    _inherit = 'hr.employee'

     def fields_view_get(self, view_id=None, view_type="form", toolbar=False, submenu=False):
        res = super(CustomEmployee, self).fields_view_get(view_id=view_id,
                                                          view_type=view_type, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(res['arch'])
 
        ###### Make fields invisible for others###########
        config_fields = self.env['ir.default'].get('res.config.settings', 'editable_fields')
        all_editable_fields = self.env['ir.model.fields'].browse(config_fields)
        rec_list = []
        for rec in all_editable_fields:
            rec_list.append(rec.name)
 
       ###### Make fields Readonly for others ###########
        if not self.env.user.has_group('hr.group_hr_manager'):
            for field in res['fields']:
                if field not in rec_list:
                    for node in doc.xpath("//field[@name='%s']" % field):
                        node.set('readonly', '1')
                        node_values = node.get('modifiers')
                        modifiers = json.loads(node_values)
                        modifiers["readonly"] = True
                        node.set('modifiers', json.dumps(modifiers))
            res['arch'] = etree.tostring(doc)
        ####################
        return res

Thanks in advance.

Pawan Kumar Sharma
  • 1,168
  • 8
  • 30
  • I don't think your fields relation is related to model `ir.default`. Can you look up which other tables (foreign keys) your table `editable_fields_rel` is related to? I would guess `model_id` is a foreign key to `id` of `ir_model_fields` and `id` is a foreign key to `id` of `res_company`. – CZoellner Apr 13 '23 at 12:31

1 Answers1

1

In Odoo 16 fields_view_get becomes get_view.

To make fields read-only override the _get_view function which returns the arch as an etree node

Example:

class CustomEmployee(models.Model):
    _inherit = 'hr.employee'

    @api.model
    def _get_view_cache_key(self, view_id=None, view_type='form', **options):
        """The override of _get_view making the fields readonly for HR managers 
        makes the view cache dependent on the fact the user has the group HR manager or not"""
        key = super()._get_view_cache_key(view_id, view_type, **options)
        key = key + (self.env.user.has_group('hr.group_hr_manager'),)
        return key

    @api.model
    def _get_view(self, view_id=None, view_type='form', **options):
        arch, view = super()._get_view(view_id, view_type, **options)
        if not self.env.user.has_group('hr.group_hr_manager'):
            rec_list = [...]
            for field in arch.xpath("//field"):
                if field.get('name') not in rec_list:
                    field.set('readonly', '1')
        return arch, view
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Hi Kenly, I try the same method, and it's working fine. But the problem is that it works according to the first form loading the user group after restarting the server. Eg. If I log in first with the manager group user then the officer group user it working reflect is manager condition and if I log in first with the officer group user then the manager group user then it's working related to the officer user condition. Please let me know if you have any solution that works according to individual users as condition defined in the method. Thanks. – Pawan Kumar Sharma Jun 05 '23 at 12:24
  • The override of `_get_view` to make fields readonly for HR managers makes the **view cache** dependent on the fact the user has the group HR manager or not – Kenly Jun 07 '23 at 10:33