Search code examples
pythonodooqweb

Inherit button function from one model into another - Odoo v9 community


I'm trying to use the functions/buttons from stock.picking model into fleet.vehicle.log.services one.

So far on my view:

        <record model='ir.ui.view' id='fleet_vehicle_log_services_form_inherit_1'>
        <field name='name'>fleet.vehicle.log.services.form0</field>
        <field name='model'>fleet.vehicle.log.services</field>
        <field name='inherit_id' ref='fleet.fleet_vehicle_log_services_form'/>
        <field name="priority">89</field>
        <field name='arch' type='xml'>
            <xpath expr="//form//sheet//group[1]" position="before">
            <header>
                <button name="action_confirm" states="draft" string="Mark as Todo" type="object" class="oe_highlight" groups="base.group_user"/>
                <button name="action_assign" states="confirmed,waiting" string="Reserve" type="object" class="oe_highlight" groups="base.group_user"/>
                <button name="force_assign" states="confirmed,waiting,partially_available" string="Force Availability" type="object" groups="base.group_user"/>
                <button name="do_new_transfer" states="draft,partially_available,assigned" string="Validate" groups="stock.group_stock_user" type="object" class="oe_highlight"/>
                <button name="do_print_picking" string="Print" groups="stock.group_stock_user" type="object" attrs="{'invisible': [('state', 'not in', ('assigned', 'partially_available'))]}"/>
                <button name="%(stock.act_stock_return_picking)d" string="Reverse" states="done" type="action" groups="base.group_user"/>
                <button name="action_cancel" states="assigned,confirmed,partially_available,draft,waiting" string="Cancel" groups="base.group_user" type="object"/>
                <field name="state" widget="statusbar" statusbar_visible="draft,confirmed,partially_available,assigned,done" statusbar_colors='{"shipping_except":"red","invoice_except":"red","waiting_date":"blue"}'/>                
            </header>
            </xpath>
        </field>
    </record>

On the model:

    _inherit = "fleet.vehicle.log.services"
    stock_picking = fields.Many2one("stock.picking", "Picking")
    state = fields.Selection(string="Estados", store=True, readonly=True, related="stock_picking.state")

So far, state field shows fine, no problem with that, but I can't seem to make the buttons work, they just doesn't appear on the view.

I guess I need to relate these functions to stock_picking as I did with state, but these are function fields...

Any ideas on how to achieve this?


Solution

  • You almost got it. The header tag must be a direct child of the form tag. So changing that line <xpath expr="//form//sheet//group[1]" position="before"> to <xpath expr="//form/sheet" position="before"> should help.

    As to calling those functions, I'd try with :

    from openerp import models, api
    
    class FleetVehicleLogServices(models.Model):
        _inherit = "fleet.vehicle.log.services"
    
        stock_picking = fields.Many2one("stock.picking", "Picking")
        state = fields.Selection(string="Estados", store=True, readonly=True, related="stock_picking.state")
    
        @api.multi
        def action_confirm(self):
            for record in self:
                record.stock_picking.action_confirm()
            return True
    

    And you can continue this way with the other methods.