简体   繁体   中英

Call Python function in window action odoo

I added this python function to 'hr.holidays' model

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

def _get_holiday_status_id_domain(self):
    if not self.env.user.has_group('hr_holidays.group_hr_holidays_user'):
        allocate_type = self.env['hr.holidays.status'].search([('name', '=', 'Compensatory Days')], limit=1)
        return [('id', '=', allocate_type.id)]
    elif self.env.user.has_group('hr_holidays.group_hr_holidays_user') and not self.env.user.has_group(
            'hr_holidays.group_hr_holidays_manager'):
        allocation_types = self.env['hr.holidays.status'].search([('name', '!=', 'Unpaid')])
        return [('id', 'in', allocation_types.mapped('id'))]

I want to call this function in action id="hr_holidays.open_allocation_holidays' to still execute while action running..

<record id="open_allocation_holidays" model="ir.actions.act_window">
        <field name="name">Allocation Request</field>
        <field name="res_model">hr.holidays</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,kanban,form</field>
        <field name="context">{
            'default_type':'add',
            'search_default_my_leaves': 1,
            'needaction_menu_ref':
            [
                'hr_holidays.menu_open_company_allocation',
            ]
        }</field>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">
                Click here to create a new leave allocation request.
            </p>
        </field>
        <field name="domain">[('type','=','add')]</field>
        <field name="view_id" ref="edit_holiday_new"/>
        <field name="search_view_id" ref="view_hr_holidays_filter"/>
    </record>

You can't call a function in an window action, instead you have to create for example a server action (with code), which then should be called or used by the menu item. In that server action you can use the original window action but manipulated with the domain from your function.

The code for the server actions should be something like this:

action = env.ref('hr_holidays.open_allocation_holidays').read()[0]
domain = model._get_holiday_status_id_domain()
action['domain'] = domain

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