How can I show a message box in openerp? I was using raise like this:
raise osv.except_osv(_("Warning!"), _("Error"))
But this stops executing other code, I only want to display an informative message box.
Raising an osv.except_osv does a couple of things:
1) Interrupts the current processing (it is a python exception after all).
2) Causes OpenERP to roll back the current database transaction.
3) Causes OpenERP to display a dialog box to the user rather than dumping a stack trace and giving the user a "bad stuff happened" message.
For onchange we can return
warning = {
'title': 'Warning!',
'message' : 'Your message.'
}
return {'warning': warning}
But it will not work for other things like button.
For your case you can do
cr.commit()
raise osv.except_osv(_("Warning!"), _("Error"))
But calling cr.commit explicitly in business transaction will leads to severe issues.
The other way is you can return a wizard with warning message. This is what most people used.
return {
'name': 'Provide your popup window name',
'view_type': 'form',
'view_mode': 'form',
'view_id': [res and res[1] or False],
'res_model': 'your.popup.model.name',
'context': "{}",
'type': 'ir.actions.act_window',
'nodestroy': True,
'target': 'new',
'res_id': record_id or False,##please replace record_id and provide the id of the record to be opened
}
One way comes to my mind... You can use some on_change method that would return dict like this:
return {
'warning': {
'title':'Message title!',
'message':'Your message text goes here!'
}
}
Related
I have a client who loves to open new tabs for multiple records but doesn't want each tab to be in focus or active. Is there a way to achieve this? The parameter 'target': '_blank' seems to be the same as 'target':'new' in this instance and will open a new tab and activate the window. I'm working with odoo v10. Thanks!
def open_product_new_tab(self):
self.ensure_one()
act_id = self.env.ref('product.product_template_action').id
menu_id = self.env.ref('custom_module.custom_menu').id
return {
'name': 'Product Template View',
'type': 'ir.actions.act_url',
'target': '_blank', #open in a new tab but not active?
'url': '/web?#id=%s&view_type=form&model=product.template&action=%s&menu_id=%s' % (self.id, act_id, menu_id)
}
I need to show a warning popup button when the user presses the "Checkout" button (just an ok/dismiss button)
I avoid using raise Warning() or raise ValidationError() since I want to remain on the existing page and simply show a pop-up warning.
Can you please share the simplest way to do this in Odoo 13?
On click proceed checkout button called jsonRpc where call your json controller with that add your custom logic on the controller and return that and on the js check with your condition and raise your Dialog-Box Like this,On Js:
var Dialog = require('web.Dialog');
ajax.jsonRpc("/custom/url", 'call', {}).then(function(data) {
if (data) {
var dialog = new Dialog(this, {
size: 'medium',
$content: _t("<div style='font-size:14px;font-family:Helvetica, Arial, sans-serif;'>Error.</div>"),
buttons: [{
text: _t('Ok'),
classes: "btn-primary",
close: true
}],
}).open();
} else {
// odoo logic
}
});
Thanks
I am trying to validate a "capacity" field with onchange decorator but for some reason when I send the warning message the previous line stops working. The template updates the field fine whitout the warning
#api.onchange('capacity')
def check_capacity_values(self):
if self.capacity<0:
self.capacity=0
raise Warning(_('wrong capacity.'))
You can use a dictionary as return value for methods decorated by api.onchange. The key for warning messages will be warning and the value another dictionary with keys title and message. An example:
return {
'warning': {'title': "WARNING!",
'message': "It isn't allowed to have a negative capacity!"}
}
I think that the problem may be that the change you made in self.capacity just before raising the warning is not stored in the database because you are using #api.onchange, so the new value is just shown in the UI, but not stored in the database.
Try it using #api.depends instead, the change will be reflected both in the UI and the database.
#api.onchange('capacity')
def check_capacity_values(self):
if self.capacity<0:
self.capacity=0
return {'warning': {
'title': "Warning",
'message': "message",
}
}
I am trying to write an onchange that returns a message and updates a value at the same time. So far it displays the message but the field remains the same. The code I have is:
#api.onchange('changed_field')
def my_onchange_method(self):
if self.other_field:
self.changed_field=False
raise Warning('Some message.')
I think my mistake is in the way of sending the message, could anyone tell me how to achieve this in odoo 9? Thanks.
I think you're raising the builtin Warning exception, which is probably why the field isn't updated (I think the changes are rolled back when the exception is raised).
Try this instead :
#api.onchange('changed_field')
def my_onchange_method(self):
if self.other_field:
self.changed_field = False
return {
'warning': {
'title': 'TITLE OF THE WARNING MESSAGE BOX',
'message': 'YOUR WARNING MESSAGE',
}
}
I can confirm this works at least for odoo 8. It will probably work for odoo 9.
def onchange_amount_paid(self, cr, uid, ids, amount_paid, context=None):
res = {'value':{}}
if amount_paid:
if fee_type==1 and amount_paid<70:
warning = { 'title': ("Warning"), 'message': ('registration account minimum payment is 70'), }
return {'value': res.get('value',{}), 'warning':warning}
return {'value': res.get('value',{})}
I have a question about SystemDialogs? I need to implement one in QML, but the sample project (“dialogs”) available on Github appears as containing errors when built with the 10.1 SDK. They do however run normally.
The code of interest is as follows:
SystemDialog {
id: dialog
title: qsTr("DIALOG")
body: qsTr("Dialog body")
confirmButton.label: qsTr("Okay button")
confirmButton.enabled: true
cancelButton.label: qsTr("Cancel button")
cancelButton.enabled: true
buttons: [
SystemUiButton {
id: random
label: qsTr("RANDOM")
enabled: true
},
SystemUiButton {
id: random2
label: qsTr("RANDOM2")
enabled: true
}
]
…
}
The “error” properties are the label and enabled properties of the confirm and cancel buttons and the buttons array property of the SystemDialog. As mentioned, although the IDE highlights them as errors, the code appears to work as expected.
My question is, is there a way to do something similar in SDK10.1? I need to set the text on the buttons in the dialog.
The names 'label' and 'enabled' are correct.
You can check bbndk-10.1/target_10_1_0_1020/qnx6/usr/include/bb/system/SystemUiButton.hpp
I think the IDE is not correct in regarding those names as an error.