Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Odoo how to force overwriting of a record rule which is defined in a base module and data is set to noupdate='1'?

Tags:

odoo

odoo-8

The following record rule is defined in product module

<data noupdate="1">
    <record id="product_comp_rule" model="ir.rule">
        <field name="name" >Product multi-company</field>
        <field name="model_id" ref="model_product_template"/>
        <field name="global" eval="True"/>
        <field name="domain_force"> ['|',('company_id','=',user.company_id.id),('company_id','=',False)]</field>
    </record>
</data>

I want to edit it in my custom module to be

<record id="product.product_comp_rule" model="ir.rule">
    <field name="name" >All Products (Parent Company)</field>
    <field name="model_id" ref="product.model_product_template"/>
    <field name="global" eval="True"/>
    <field name="domain_force">['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])] </field>
</record>
like image 722
Mustafa Avatar asked Aug 17 '16 11:08

Mustafa


People also ask

How to update Record rule in Odoo?

In order to do this in the backend, first, we need to get the XML file of the Record rule. We can get this by simply searching the name of the Record Rule. After that in the new module inside the security folder, create a security . xml and paste the code in that security.

What is Noupdate in Odoo?

noupdate means when second time this module will upgraded, this record will not be updated again. The record which is inside noupdate="1" will be initialized only at installation time. Tip : In case if you delete the record then at next update system will Re-create it.

What is IR rule in Odoo?

rule. Record rules are conditions that records must satisfy for an operation (create, read, write or delete) to be allowed. Example of a condition: User can update Task that assigned to him. Group field defines for which group rule is applied.

What is record rule in Odoo?

Record Rules are an important part of security in Odoo. They control which records users can access. For example, sales users can only access their own quotations and sales orders (see below). Record Rules can make it easier to develop functionality in Odoo (because the rules have already been setup)


1 Answers

You can use :

<function name="fix_er_role" model="ir.rule"/>

After that add the method to ir.rule and fix your data

class IRRule(models.Model):
    _inherit = 'ir.rule'
    
    def fix_er_role(self):
         rol_id = self.env.ref('product.product_comp_rule')
         rol_id = self.env['ir.rule'].search([('id','=',rol_id)])
         rol_id.write({'domain_force':['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])]})

Edit : an other good solution for this requirement.

    <function name="write" model="ir.model.data">
         <!-- get the record if from data base -->
        <function name="search" model="ir.model.data">
            <value
              eval="[('module', '=', 'product'), ('name', '=', 'product_comp_rule')]"
              />
        </function>
       <!-- remove noupdate -->
        <value eval="{'noupdate': False}" />
    </function>


    <record id="product.product_comp_rule" model="ir.rule">
        <field name="name" >All Products (Parent Company)</field>
        <field name="model_id" ref="product.model_product_template"/>
        <field name="global" eval="True"/>
        <field name="domain_force">['|','|',('company_id','=',user.company_id.id),('company_id','=','False'),('company_id','child_of',[user.company_id.id])] </field>
    </record>

    <!-- reset noupdate -->
    <function name="write" model="ir.model.data">
        <function name="search" model="ir.model.data">
            <value
              eval="[('module', '=', 'product'), ('name', '=', 'product_comp_rule')]"
              />
        </function>
        <value eval="{'noupdate': True}" />
    </function>
like image 125
Charif DZ Avatar answered Jan 03 '23 11:01

Charif DZ