Menampilkan Warning Message Sekaligus Update Data

Ketika kita menampilkan pesan error, biasanya menggunakan ValidationError, UserError, dll dan tidak bisa melanjutkan proses, artinya proses terhenti. Tapi ada kalanya warning message yang ditampilkan hanya merupakan peringatan, dan user masih bisa melanjutkan proses. Untuk melakukan hal tersebut, kita tidak bisa lagi menggunakan library ValidationError dan UserError.

Salah satu cara yang bisa dilakukan adalah dengan menggunakan popup wizard. Jadi, ketika ada warning yang perlu ditampilkan, kita tampilkan dalam bentuk wizard dan berikan pilihan confirm atau cancel, kalau confirm berarti proses akan dilanjutkan. Syntax nya kurang lebih seperti ini:

File warning_message_wizard.py

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class MsWarningMessageWizard(models.Model):
    _name = "ms.warning.message.wizard"
    _description = "Warning Message Wizard"

    name = fields.Text(
        string="Message",
        required=False)

    def action_confirm(self):
        rec_id = self.env[self.env.context['active_model']].browse(self.env.context['active_id'])
        action_name = self.env.context['confirm_action_name']
        context = rec_id.env.context.copy()
        context.update({
            'has_confirmed': True,
        })
        rec_id = rec_id.with_context(context)
        if self.env.context['confirm_action_params']:
            return getattr(rec_id, action_name)(self.env.context['confirm_action_params'])
        else:
            return getattr(rec_id, action_name)()

File warning_message_wizard_views.xml

<odoo>
<data>

<record id="ms_warning_message_wizard_form_view" model="ir.ui.view">
<field name="name">ms.warning.message.wizard.form</field>
<field name="model">ms.warning.message.wizard</field>
<field name="type">form</field>
<field name="arch" type="xml">

<form>
<sheet>
<group>
<field name="name" readonly="1" colspan="4" nolabel="1"/>
</group>
<footer>
<button name="action_confirm" string="Confirm" type="object" class="oe_highlight"/>
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</sheet>
</form>

</field>
</record>

<record id="ms_warning_message_wizard_action" model="ir.actions.act_window">
<field name="name">Warning Message</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">ms.warning.message.wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="ms_warning_message_wizard_form_view"/>
<field name="target">new</field>
</record>

</data>
</odoo>

Dan di bagian yang akan menampilkan warning message, dalam hal ini contohnya pada button confirm PO:

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

def button_confirm(self):
if not self.env.context.get('has_confirmed'):
action = self.env.ref('ms_warning_message_base.ms_warning_message_wizard_action').sudo().read()[0]
context = self.env.context.copy()
context.update({
'default_name': 'There is difference price unit between current price and price unit set on product. Do you want to force confirm this PO?',
'confirm_action_name': 'button_confirm',
'confirm_action_params': False,
})
action.update({
'context': context
})
return action
else:
return super().button_confirm()

Dan berikut ini hasilnya: