Skip to Content
Menu
This question has been flagged
8986 Views

Hello,

I want to add a condition to tree to add 'create=false' attribute according to groups permission. Is it possible?

Here is my code:

My goal is <tree create="false/true"> depends on the group value.

<fieldname="arch"type="xml">
    <xpath expr="//tree" position="attributes">
        <attribute name="create">groups.group_admin</attribute>
    </xpath>
</field>
I want a way that somehow will evaluate to 'true' or 'false' to suffice the statement.  Any help, would be greatly appreciated.


PS. My actual goal is to not allow a group (group_coordinator):

access_contacts_coordinator,res.partner.coordinator,model_res_partner,custom_contacts.group_coordinator,1,0,0,0

to create/update data in res.partner. I have my group permissions set properly however, the user assigned to this group is still able to create/update.. And so, I think of adding the 'create' attributes in 'tree' to somehow do the trick. 

Avatar
Discard
Author

Hello everyone, i have already found the answer to this question i get the answer from this link:

https://www.odoo.com/forum/help-1/question/how-to-hide-the-create-button-dynamical-tree-view-in-openerp-42661

From his answer i get:

An other option would be to alter `fields_view_get` method which a check on user:

@api.model

def fields_view_get(self, view_id=None, view_type='form',

toolbar=False, submenu=False):

res = super(MyModel, self).fields_view_get(

view_id=view_id, view_type=view_type,

toolbar=toolbar, submenu=submenu)

if view_type != 'search' and self.env.uid != SUPERUSER_ID:

# Check if user is in group that allow creation

has_my_group = self.env.user.has_group('my_group_with_more_access')

if not has_my_group:

root = etree.fromstring(res['arch'])

root.set('create', 'false')

res['arch'] = etree.tostring(root)

return res

And to make it work in my end:

from lxml import etree

@api.model

def fields_view_get(self, view_id=None, view_type='form',

toolbar=False, submenu=False):

res = super(MyModel, self).fields_view_get(

view_id=view_id, view_type=view_type,

toolbar=toolbar, submenu=submenu)

user = self.env['res.users'].browse(self.env.uid)

if view_type != 'search' and self.env.uid != SUPERUSER_ID:

# Check if user is in group that allow creation

has_my_group = user.has_group('group_admin')

if not has_my_group:

root = etree.fromstring(res['arch'])

root.set('create', 'false')

root.set('edit', 'false')

root.set('delete', 'false')

res['arch'] = etree.tostring(root)

return res

Related Posts Replies Views Activity
1
Jul 19
4003
2
Jan 25
6251
2
May 24
1598
1
Apr 24
2271
1
Dec 23
4121