iam supreeth
iam supreeth

Reputation: 497

How to make one field(field_3) 'User' and 'Admin' editable in Openerp-7

I have created a custom header menu called 'HM-1'

And i have created a Menu called 'Menu 1' Inside 'Menu 1' i have 3 fields created, called field_1 fileds_2 filed_3.

I created a 'User' and i have two users now
1. Admin
2. User

My question is 'How to make one field(field_3) 'User' and 'Admin' editable in Openerp-7' Remaining fields in 'User' should be readonly only field_3 should be editable.

How to do this?

Upvotes: 0

Views: 87

Answers (1)

Alessandro Ruffolo
Alessandro Ruffolo

Reputation: 1575

The first thing that come into my mind is to override fields_view_get and change the readonly and modifiers attribute of field_1 and field_2 based on groups of self.env.user. Of course you need to assign User to a specific group, different to the one of Admin.

class example_class(models.Model):

    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        res = super(example_class, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        group = self.pool['ir.model.data'].xmlid_to_object(cr, uid, 'your_group', raise_if_not_found=True, context=context) 
        if view_type == 'form' and group in self.env.user.groups_id:
            doc = etree.XML(res['arch'])
            #this for cycle must be repeated for each field you need to make readonly
            for node in doc.xpath("//field[@name='field_2']"):
                if 'modifiers' in node.attrib:
                    text = node.attrib['modifiers']
                    j = json.loads(text)
                    j['readonly'] = '1'
                else:
                    j = {'readonly': '1'}
                node.attrib['modifiers'] = json.dumps(j)
            res['arch'] = etree.tostring(doc)
        return res

Upvotes: 1

Related Questions