Hide edit button Odoo15

Viewed 196

How can i hide the edit button depends on the condition? For example: If the john created the data nobody can change it. So hide the edit button..

<record id="hideEdit_rule" model="ir.rule">
        <field name="name">Hide Edit </field>
        <field name="model_id" ref="model_hospital_patient" />
        <field name="domain_force">[('user', '=', 'john')]</field>
        <field name="groups" eval="[(4, ref('om_hospital.hospital_management_pratisyen_doctor'))]"/>
        <field name="perm_read" eval="True"/>
        <field name="perm_write" eval="False"/>
        <field name="perm_create" eval="True"/>
        <field name="perm_unlink" eval="True"/>
</record>

I have already tried this code if the created user is john, edit is false.. It does not work.

2 Answers

Record rules are conditions that records must satisfy for an operation (create, read, update or delete) to be allowed.

The record rule above will be checked when a user tries to read, create or delete a hospital.patient record. to check the user who created a record use create_uid on the left and use the context value user to get the current user record.

To check if the current user created the record, use the following domain:

<field name="domain_force">[('create_uid', '=', user.id)]</field>

It can't be used to hide the Edit button since access rules are checked after calling the write method

There is an OCA module (Web Access Rules Buttons) that disables the Edit button on the form views if the user cannot edit the current record according to the record access rules.

Usage:

When using Odoo, even if a user has no rights to edit a record, the Edit button is shown. The user can edit the record but won't be able to save his changes. Now, the user won't be able to click on the Edit button.

Now the module is available for version 14.0, but if you update the manifest file like the following, it should work.

  1. Comment the following line:

    "views/web_access_rule_buttons.xml",

  2. Add the following entry:

    "assets": {'web.assets_backend': ['web_access_rule_buttons/static/src/js/form_controller.js'],},

Try with following record rule:

<record id="hideEdit_rule" model="ir.rule">
    <field name="name">Hide Edit</field>
    <field name="model_id" ref="model_hospital_patient"/>
    <field name="domain_force">[('create_uid', '=', user.id)]</field>
    <field name="groups" eval="[(4, ref('om_hospital.hospital_management_pratisyen_doctor'))]"/>
    <field name="perm_write" eval="True"/>
</record>

You can overide fields_view_get method and disable edit according to condition.

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    res = super(<MODEL NAME>, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
    if view_type == 'form' and <ADD YOUR CONDITION HERE>:
        root = etree.fromstring(res['arch'])
        root.set('edit', 'false')
        res['arch'] = etree.tostring(root)
    else:
        pass
    return res
Related