4

I am using Odoo 11 and I want to display a Warning popup in a @api.constraint method. The warning popup that I want to display has two buttons, the first is an OK button used to ignore the warning and the other one is a Cancel button used to attempt the saving operation, it' s similar to the warning popup that odoo uses like on the picture below : The warning message that i want to display is similar to this one

I searched a lot on the web and I found different proposed solutions like using Wizard, exception.Warning() and osv.except_osv(), but unfortunately no one of this solution gives me exactly what I want.

Any help please?

  • 1
    So you don´t want a normal Warning right? A normal Warning always breaks the transaction. Could you describe more what would you like to achieve? I mean if an `@api.constrains` raise an Warning then the record cannot be saved. Would you like to save it if you press the OK button in the Warning wizard? – ChesuCR Mar 27 '18 at 13:09

4 Answers4

4

You can raise warning message by different ways. I have created message related to stock quantity by this way:

if self.qty > new_qty:
       message = _('You plan to sell %s quantity but you only have %s available in %s warehouse.') % \
                     (self.qty, self.product_id.virtual_available, self.order_id.warehouse_id.name)
       mess= {
                    'title': _('Not enough inventory!'),
                    'message' : message
                }
       return {'warning': mess}

This return same wizard of warning as you want and as shown given image.

Keval Mehta
  • 689
  • 3
  • 28
4

The basic odoo Warning that you can use is called from the odoo.exception class. For example:

from odoo.exceptions import AccessError, UserError, RedirectWarning, ValidationError, Warning

@api.constrains('age')
def _check_something(self):
    for record in self:
        if record.age > 20:
            raise Warning(_("Your record is too old: %s" % record.age))

This should work for your issue.

stee
  • 41
  • 3
1

I write a (draft) module to open Dialog box see: https://github.com/Micronaet/micronaet-sales/tree/master/confirm_dialog_wizard

In your button you can write this code to open, for ex. to hide a product:

@api.multi
def hide_product_pricelist(self):
    """ Hide product
    """
    return self.env['dialog.box.wizard'].open_dialog(
        message='The product will be hided, <b>you cannot use again</b> '
                'but remain in sale order where yet present, <br/>'
                'confirm?',
        action='self.env["product.product"].browse(%s).write('
               '{"active": False})' % self.id,
        title='Confirm request:',
        mode='cancel_confirm',
    )

The program will pop up a window to confirm (as in tree you cannot use "confirm" message parameter), I'm try to do better but ... its a begin ... :)

Worker
  • 2,411
  • 6
  • 29
  • 55
0

It is similar to the warning shown when we use the confirm attribute on a button.

<button confirm="Reset to draft!"/>

Raising a validation error or returning a warning as dictionary did not show the Cancel button.

Kenly
  • 24,317
  • 7
  • 44
  • 60