How to hide edit/create button on form by conditions?

只愿长相守 提交于 2019-12-12 14:33:55

问题


I'm a new Odoo developer and I need to hide the edit button when my form is entering a custom state, I need this because of a security problem.

This code in XML does not work when I try to give an attribute to the form.

<record model="ir.ui.view" id="pesan_form_view">
    <field name="name">pesan_service_form</field>
    <field name="model">pesan.service</field>
    <field name="arch" type="xml">
    <form string="Booking Service" attrs="{edit:'false':[('state','in','baru')]}">
    <!-- structure of form -->
</record>

I don't know why it doesn't work.


回答1:


qWeb conditions doesn't work for FormView.

You can check it here(path_to_odoo/addons/web/static/src/js/framework/view.js):

 /**
  * Return whether the user can perform the action ('create', 'edit', 'delete') in this view.
  * An action is disabled by setting the corresponding attribute in the view's main element,
  * like: <form string="" create="false" edit="false" delete="false">
  */
  is_action_enabled: function(action) {
      var attrs = this.fields_view.arch.attrs;
      return (action in attrs) ? JSON.parse(attrs[action]) : true;
  },

This method calls from template FormView.buttons in path_to_odoo/addons/web/static/src/xml/base.xml:

<button t-if="widget.is_action_enabled('edit')"
    type="button"
    class="oe_form_button_edit btn btn-default btn-sm" accesskey="E">
    Edit
</button>

These problems are solved in Odoo with the help of rules(ir.rule object of Odoo)

You can find and edit rules in GUI here: Settings(top menu) -> Security(left menu) -> Access Rules(left menu). Use admin user in debug mode for this.

At the same you can add some rules to data.xml of your module for import. They will be added when you install or update module.

Be careful! Record rules do not apply to the Administrator user.

At the same you can try to expand widget FormView.

Hope this helps you.




回答2:


Try this code.

<record model="ir.ui.view" id="pesan_form_view">
    <field name="name">pesan_service_form</field>
    <field name="model">pesan.service</field>
    <field name="arch" type="xml">
    <form string="Booking Service" attrs="{'edit': [('state', 'in', ['baru'])]}">
    <!-- structure of form -->
</record>


来源:https://stackoverflow.com/questions/35196277/how-to-hide-edit-create-button-on-form-by-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!