简体   繁体   中英

How to display a warning when returning an act_window (from python code) ? Odoo 14

In the product.template model , I added new smart button which return an ir.actions.act_window action to the model stock.quant , when clicking on the same button I want also to return a warning if there is a warning in the product based on conditions : Here is the code of warning:

if not self or not self.env.user.has_group('sale.group_warning_sale'):
            return

        if self.sale_line_warn != 'no-message':
            if self.sale_line_warn_msg:
                message = self.sale_line_warn_msg
                warning = {
                    'title': ("Warning for %s") % self.name,
                    'message': message
                }

            if self.sale_line_warn == 'block':
                self = False

            return {'warning': warning}

Here is the xml file:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="view_product_inherit" model="ir.ui.view">
        <field name="name">view_product_inherit</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view"/>
        <field name="arch" type="xml">
            <div name="button_box" position="inside">
                <button class="oe_stat_button" name="my_action"
                        type="object" icon="fa-shopping-cart" string="Stock">
                </button>
            </div>
        </field>
    </record>
</odoo>

Here is my function:

  def my_action(self):
        return {
            'name': _('Stock'),
            'view_type': 'form',
            'view_mode': 'tree',
            'res_model': 'stock.quant',
            'context': {'default_name': 'My warning message'},
            'type': 'ir.actions.act_window',
        }

*Any help please ? Is it possible to do it ? Thanks

The simple solution I can think of, is adding a boolean field and a datetime field to the product.template model. And in the action if there were any warnings check if the boolean field is True and if the time passed from the datetime field is less than for example 30 seconds, then don't show the warning and return your ir.actions.act_window action. But if the boolean field is False then show warning by raising a UserWarning or ValidationError and at the end of the warning message ask the user " if you are sure and want to continue, click on the button again ". And don't forget to set the boolean field to True and the datetime field to the current time when showing returning the warning. Also set the boolean field to False when returning the action.

This way the user only gets the warning once and if he wants to continue anyway he can click on the button again.

Add a condition at the top of your my_action method (before returning the action) like this:

if condition:   
      raise ValidationError("Error message")

Don't forget to import ValidationError at the top of your .py file.

from odoo.exceptions import ValidationError

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