简体   繁体   中英

How do I call a method in ir.rule domain in Odoo?

I'd like to create a record rule for my app.

Here is my XML:

<record model="ir.rule" id="ir_rule_something"> 
    <field name="name">something</field> 
    <field name="model_id" ref="model_something"/> 
    <field name="domain_force">[('agent','in',function_that_return_list_of_ids)]</field> 
    <field name="perm_read" eval="True"/> 
    <field name="perm_write" eval="False"/> 
    <field name="perm_unlink" eval="False"/> 
    <field name="perm_create" eval="False"/> 
</record>

I would like the function_that_return_list_of_ids to return a list of id . Where do I put the method and how do I call it?

The easiest way will be for you to override _eval_context or _eval_context_for_combinations in ir.rule itself.

class MyIrRule(models.Model):
    _inherit ="ir.rule"

    @api.model
    def _eval_context(self):
        context = super(MyIrRule,self)._eval_context()
        context.update(function_of_ids=function_of_ids)

In this example, function_of_ids is not a method but a simple function.

I found out another method to call a function from ir.rule. Here it is.

<record id="manager_asterisk_cdr_rule" model="ir.rule">
    <field name="name">Manager Asterisk CDR rule</field>
    <field name="perm_write" eval="1"/>
    <field name="perm_read" eval="1"/>
    <field name="perm_create" eval="1"/>
    <field name="perm_unlink" eval="1"/>
    <field name="groups" eval="[(6,0, [ref('group_barrier_manager')])]"/>
    <field name="model_id" ref="asterisk_base.model_asterisk_cdr"/>
    <field name="domain_force">user.manager.get_cdr_domain()</field>
</record>

The main part is:

<field name="domain_force">user.manager.get_cdr_domain()</field>

And here is the function:

@api.multi
def get_cdr_domain(self):
    # Used from facility_manager/security.xml to filter CDR records
    facility_ids = [f.id for f in self.facilities]
    barriers = self.env['barrier.barrier'].search(
                                        [('facility', 'in', facility_ids)])
    numbers = []
    for b in barriers:
        # Add barrier access phones
        numbers.extend([a.number for a in b.access_phones])
        # Add panels GSM number
        numbers.extend(
            [p.gsm_number for p in b.call_panels if p.gsm_number])
        # Add panels SIP number
        numbers.extend(
            [p.sip_number for p in b.call_panels if p.sip_number])
    # Add keys of type=phone
    key_numbers = [k.number for k in self.env[
        'barrier.resident_key'].search([
                                       ('facility', 'in', facility_ids),
                                       ('key_type', '=', 'phone')])]
    numbers.extend(key_numbers)
    domain = ['|', ('src', 'in', numbers), ('dst', 'in', numbers)]
    return domain

Also when you add new records to the models requested from the function you need to clear cache:

self.env['ir.rule'].clear_cache()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM