Reputation: 33
I have a button which I need to show only if the user is a superuser (admin). My problem is that when I'm using xpath to include the attrs, nothing is working as expected. My code is:
<record id="wms_stock_view_move_form" model="ir.ui.view">
<field name="name">wms.stock.view.move.form</field>
<field name="model">stock.move</field>
<field name="inherit_id" ref="stock.view_move_form" />
<field name="arch" type="xml">
<field name="location_id" position="attributes">
<attribute name="domain">[('name','!=', 'Scrapped')]</attribute>
</field>
<field name="location_id" position="after">
<field name="is_superuser"/>
</field>
<field name="location_dest_id" position="attributes">
<attribute name="domain">[('name','!=', 'Scrapped')]</attribute>
</field>
<xpath expr='//form[@string="Stock Moves"]' position='attributes'>
<attribute name="create">false</attribute>
<attribute name="edit">false</attribute>
<attribute name="delete">false</attribute>
</xpath>
<xpath expr="//button[@name='action_cancel']" position="attributes">
<attribute name="attrs">{'invisible':[('is_superuser','=', True)]}</attribute>
</xpath>
</field>
</record>
here, is_superuser is a computed field, its code is:
is_superuser = fields.Boolean(compute='_is_super_user')
def _is_super_user(self):
if self._uid == SUPERUSER_ID:
self.is_superuser = True
else:
self.is_superuser = False
The original code for the button, in its original view is:
<button name="action_cancel" states="draft,assigned,confirmed" string="Cancel Move" type="object"/>
any idea, what I'm doing wrong ? Thanks in advance.
Upvotes: 2
Views: 2443
Reputation: 33
I really appreciate all your help, but the above responses does not helped me. However, I found the answer for this question. Since, it has states included, we need to take that into account as well. We need to override the behavior of 'states' tag as well. so the code needs to be:
<xpath expr="//button[@name='action_cancel']" position="attributes">
<attribute name="states"></attribute>
<attribute name="attrs">{'invisible':['|', ('is_superuser','=', False), ('state', 'not in', ('draft','assigned','confirmed'))]}</attribute>
</xpath>
Upvotes: 0
Reputation: 14751
The code that you are using should work :
first you need to put @api.depends()
on the method definition, without putting any field so it will be computed every time the record is called.
second check the result of the compute field doe's it work like it should because the xml code is correct use print in console to see if the code i working is the method gets called.
Upvotes: 0
Reputation: 14801
I would prefer to use Odoo's group access system for such behaviour. Just extend the button with the attribute groups
and ofcourse the correct group (for example base.group_system
or base.group_no_one
for admins):
<field name="action_cancel" states="draft,assigned,confirmed" position="attributes">
<attribute name="groups">base.group_system</attribute>
</field>
Upvotes: 1