Show a message and assign value to a field in an onchange method - odoo

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',{})}

Related

How to display a dialog with warning message from controller in Odoo?

Is it possible to display a warning message when overriding a controller function? This is the code I have:
raise Warning(_('Entered Quantity is greater than quantity on source.'))
return super(CheckCart, self).cart(**post)
I want to render the cart template but also raise a Warning, but I get the following:
500: Internal Server Error
Error Error message: ('Entered Quantity is greater than quantity on
source.', '')
Traceback
Without anything else.
Controller File.
request.render("custom_module_name.redirect_fail_page", {})
XML File.
<template id="redirect_fail_page" name="Failure Code Page">
<div>
<span>Failed</span>
</div>
<script>
setTimeout(function(){
window.location.href = '/';
}, 1100)
</script>
</template>
Here you can use request.render
You can do it like this way,
Called the Json Controller was based on logic return the value and on the js check with the value is satisfied then with Dialog you can raise the warning.
On Py File,
#http.route(['/custom/url'], type='json', auth="public", website=True)
def checkout_custom(self, **post):
# Logic Based on the that return True/False.
On JS File,
var ajax = require('web.ajax');
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;'>Warning Message.</div>"),
buttons: [{
text: _t('Ok'),
classes: "btn-primary",
close: true
}],
}).open();
} else {
// Process
}});
Thanks

Issue in odoo 8 displaying warning message

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",
}
}

How do I print a report AND avoid the wizard window being closed?

This is a sample method I use to print a report from a wizard:
def mymethod(self, cr, uid, ids, context=None):
"""
"""
return {
'type': 'ir.actions.report.xml',
'report_name': 'trescloud_ats_2013_report',
'datas': {
'model': 'sri.ats.2013',
'res_ids': ids
}
}
How do I also avoid the wizard being closed?
Call the report with a button with type="action" instead of type="object". Like this you don't need to call a python method. If you need to run some python code before as well, then you should open again the wizard with the proper elements in the return of the function.
<button name="%(module_name.report_id)d"
type="action" />

Openerp show message dialog

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!'
}
}

Openerp "Save" button persists even after confirming the PO

I am not sure whether I broke the flow and introduced this bug. When I am editing a PO and confirming the PO (see Fig 2).
The changes get updated in database however the save button is still there. But the PO gets confirmed (See fig 3).
I need the save button to be replaced with "Edit" button (By default it was like that).
Can anyone suggest What could be wrong or any settings stuff??
Any help is appreciated..
In web addons-->web-static-src-->js-->view_form.js
add below lines of code:
on_button_save: function() {
var self = this;
var result = confirm("Do you want to save Record..?");
if (result==true) {
return this.save().done(function(result) {
self.trigger("save", result);
self.reload().then(function() {
self.to_view_mode();
var parent = self.ViewManager.ActionManager.getParent();
if(parent){
parent.menu.do_reload_needaction();
}
});
});
}
else{
return result;
}
},
This is the default behaviour to have the save button appear as it is if you did not click it and you clicked the button on the form.
Actually i written this code for my requirement.Before saving the record should ask the conformation to save.this code may help full you to further implements ion of you are requirement.