Search code examples
pythonxmlxpathodooodoo-11

Add attribute to button in inherited form Odoo


i want to view button (from inherited form) based on bool field (based on function) to enable manager of employee to approve or refuse leave request of his employees

views.xml

<record id="manager_approve_refuse_his_employees_leaves" model="ir.ui.view">
  <field name="name">Manager Approve Refuse His Employees Leaves</field>
  <field name="model">hr.holidays</field>
  <field name="inherit_id" ref="hr_holidays.edit_holiday_new"/>
  <field name="arch" type="xml">
    <xpath expr="//field[@name='state']" position="before">
      <field name="view_leaves" invisible="0"/>
    </xpath>
    <xpath expr="//button[@name='action_approve']" position="attributes">
      <attribute name="attrs">{'invisible': [('view_leaves' ,'=', False)]}</attribute>
    </xpath>
  </field>
</record>

models.py

class InheritHrHolidays(models.Model):
    _inherit = 'hr.holidays'

    view_leaves = fields.Boolean(compute="view_leaves_for_manager", store=False)
    @api.multi
    def view_leaves_for_manager(self):
        if self.env.uid == self.employee_id.parent_id.user_id.id:
            self.view_leaves = True

this is the original button

<button string="Approve" name="action_approve" states="confirm" type="object" groups="hr_holidays.group_hr_holidays_user" class="oe_highlight"/>

but button doesn't appear yet .. what is the problem


Solution

  • In this case, you have first remove 'states' attributes from button and then add 'attrs' attribute. For example:

    <xpath expr="//button[@name='action_approve']" position="attributes">
        <attribute name="states"/>
        <attribute name="groups"/>
        <attribute name="attrs">{'invisible': [('view_leaves' ,'=', False)]}</attribute>
    </xpath>
    

    Afterwards, restart your server and upgrade module.