Issue in odoo 8 displaying warning message - api

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

Related

Office UI Fabric - PeoplePicker: Cannot get createGenericItem to work

Perhaps it's just a misunderstanding on my side, but I thought the callback for createGenericItem in the PeoplePicker (https://developer.microsoft.com/en-us/fabric#/components/peoplepicker) was used to handle input, that cannot be matched to any of the available items, and then give the possibility to create an adhoc item for this. But, whatever I tried, the callback is never called.
I made a simple pen here for the issue: https://codepen.io/anon/pen/daGPWe?editors=0010
In the example, there are two items, Peter and Maria. If you type something different (and hit enter, tab, space, whatever) I'd expect the createGenericItem callback to be called, but it isn't.
What am I doing wrong? Or is there a misunderstanding of the purpose of this callback? I'm unable to find an example anywhere.
Regarding
but I thought the callback for createGenericItem in the PeoplePicker
(https://developer.microsoft.com/en-us/fabric#/components/peoplepicker)
was used to handle input
that's correct. In order to trigger IBasePickerProps.createGenericItem function, IBasePickerProps.onValidateInput function needs to be provided with ValidationState.valid as a return value, for example:
<NormalPeoplePicker
createGenericItem={this.createGenericItem}
onValidateInput={this.handleValidateInput}
selectedItems={this.state.selectedItems}
onResolveSuggestions={this.handleResolveSuggestions}
onChange={this.handleChange}
/>
private handleValidateInput(input: string) {
return ValidationState.valid;
}
private createGenericItem(input: string, validationState: ValidationState) {
return { text: "Unknown person", state: validationState };
}
This demo demonstrates it, once tab or enter key is clicked and value cannot be resolved to any of the available items, Unknown person item is getting displayed

How to change keys lable of yii2 advanced API response

I have created API module in my yii2 advanced application and also added HttpBearerAuth in controller file and it is working.
On Unauthorized I'm getting below response :
{"name":"Unauthorized","message":"Your request was made with invalid credentials.","code":0,"status":401,"type":"yii\\web\\UnauthorizedHttpException"}
I want to change key label of this response like below :
{"error":"Unauthorized","errorMessage":"Your request was made with invalid credentials.","code":0,"status":401}
How do I update these keys?
Attach an event handler to yii\web\Response::EVENT_BEFORE_SEND and examine the $data attribute of the yii\web\Response class. Not sure, but guess you'll find an array where the keys are exactly those that you want to change.
You just need to filter out the responses you want to handle (eg everything except status codes 200 & 201).
Maybe something like this... probably bugs included :-)
Event::on(yii\web\Response::className(), yii\web\Response::EVENT_BEFORE_SEND, function ($event) {
if (Yii::$app->response->getStatusCode() > 201) {
if (isset(Yii::$app->response->data['name']) {
Yii::$app->response->data['error'] = Yii::$app->response->data['name'];
unset(Yii::$app->response->data['name']);
}
}
});

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

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

Dojo load failes with error 'Uncaught string.substitute could not find key "!actionBarTemplate" in template' after recent dojo update

I have updated dojo for one of my project which uses the framework. After the recent update I noticed a weird error 'Uncaught string.substitute could not find key "!actionBarTemplate" in template' and dojo load fails. I tried to research online about the error and track from the source files but could not figure out the cause of it. If any of you have an idea of why this might happen or has encountered a similar situation, please let me know. Thanks!
This error is thrown if the dijit/_TemplatedMixin couldn't find a key that is present in the template but not in the widget, exactly in the _stringRepl function that is called in the buildRendering of a widget.
Here is what returns this function:
return string.substitute(tmpl, this, function(value, key){
if(key.charAt(0) == '!'){ value = lang.getObject(key.substr(1), false, _this); }
if(typeof value == "undefined"){ throw new Error(className+" template:"+key); }
if(value == null){ return ""; }
Note the throw new Error part?
Now, the only templates containing a key !actionBarTemplate are
- dijit\templates\TooltipDialog.html
- dijit\templates\Dialog.html
So, do you have a widget that use this templates and does not have property actionBarTemplate? Probably adding the property actionBarTemplate: "" to does widgets fix your issue.
Have in mind that if you have in your templates, for example ${title}, this will end up asking if this.title exist, so, make sure that at least you have title: null
Hope it helps

client web - how to get current record id at any time

I'm trying to work on the "different permissions based on workflow state" issue but I'm struggling with the fact that it seems impossible to get the id of the current object 'at any time' that is necessary in order to get the permission of that object. What I mean is that I manage to get it from the client state following jquery bbq docs like:
$.bbq.getState().id
BUT it looks like this is doable only AFTER a complete page load. I investigated this by placing some alert in the main view events, like:
openerp.web.PageView = openerp.web.PageView.extend({
on_loaded: function(data) {
this._super(data);
alert('page load ' + $.bbq.getState().id);
},
do_show: function() {
this._super();
alert('page show ' + $.bbq.getState().id);
},
reload: function() {
this._super();
alert('page reload ' + $.bbq.getState().id);
},
on_record_loaded: function(record) {
this._super(record);
alert('record loaded ' + $.bbq.getState().id);
}
});
and I found that when you open the page view (by clicking on an item in a search view, for instance) you get always "undefined".
Then, you get it into "reload" and "on_record_loaded" when passing from an object to another using paged navigation. And then, you miss it again when you click on the "edit" button.
In the form view I successfully got it only on the 1st load because it seems that some caching is in-place. So that, if I place a pdb into web client's fields_view_get and I do this into the form "init_view":
var ids = [];
if ($.bbq.getState().id){
ids = [parseInt($.bbq.getState().id)];
}
console.log(ids);
return this.rpc("/web/view/load", {
"model": this.model,
"view_id": this.view_id,
"view_type": "form",
toolbar: this.options.sidebar,
context: context,
ids: ids,
}, this.on_loaded);
I get it only the 1st time that the page gets loaded. The same happen if I take ids from
this.dataset.ids
I looked anywhere at the core web module and I can't find a proper API for this and it looks weird (above all on dataset) that we don't have a proper way for getting/working on the current record/s. Even the context and the session do not have any information about that.
Probably I should store this into the view itself on 1st load...
Thanks in advance for any pointers.
try:
this.view.datarecord.id
OpenERP 7 in form view:
debugged using google chrome
Try the combination of the
this.dataset.ids and this.dataset.index
like
curr_id = this.dataset.ids[this.dataset.index]
this might solve your problem.