Tek Siong, Hock

Apr 4, 20231 min

Odoo Control View Access with fields_view_get method

In the standard Odoo, there is a standard method that we can override to control the access to the form view, eg, readonly, invisible, etc.

In the following example, we can make the 'sales & purchase' page in the res.partner (Contact), to be readable for normal account user or other users, while billing manager can edit.

@api.model
 
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
 
res = super(ResPartner, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=submenu)
 
if view_type == 'form':
 
user = self.env.user
 
user_is_account_user = user.has_group('account.group_account_invoice')
 
user_is_account_manager = user.has_group('account.group_account_manager')
 
if (user_is_account_user and not user_is_account_manager) or not user_is_account_user:
 
# Make sale purchase page fields readonly for the user who is only sale person and not manager
 
doc = etree.XML(res['arch'])
 
for field in doc.xpath("//notebook/page[@name='sales_purchases']//field"):
 
if field.get('name') == 'delivery_note':
 
continue
 
modifiers_dict = ast.literal_eval(
 
field.get('modifiers').replace('true', 'True').replace('false', 'False'))
 
modifiers_dict['readonly'] = True
 
# replace Title Case to lower case to make it readable for JS
 
modifiers_str = str(modifiers_dict).replace("'", '"').replace('True', 'true').replace('False',
 
'false')
 
field.set('modifiers', modifiers_str)
 
res['arch'] = etree.tostring(doc)
 

 
return res

    590
    0