Skip to Content
Menu
This question has been flagged
1 Reply
7923 Views

Say I have a custom web controller e.g. 

@http.route(....)
def myroute(self, ...):
    order = request.env['sale.order'].browse(some_id)
    product = request.env['product.product'].ref('mymodule.product1')
    product.write({'list_price': -100})
    order._cart_update(product_id=product.id, set_qty=1, add_qty=1)
    return request.render('mymodule.mytemplate', {...})

I'm concerned that the product update could be overwritten before it's added to the cart, which is theoretically possible if the controller method isn't atomic. Can anyone comment on how the framework will handle this?


Avatar
Discard
Best Answer

In odoo transaction atomic at the request(http) level, it binds with HTTP layer
when any request comes odoo create a new environment for it.

when request(thread) return the result to a client cursor attached to it has been explicitly committed (unless there is no traceback) ref: https://github.com/odoo/odoo/blob/12.0/odoo/http.py#L285
so in your case, `list_price` value change inside _cart_update it will be updated at the end of the request dispatch and very next request get new value.

you can forcefully commit the cursor in the middle of the transaction using request.env.cr.commit()  but in general case it not need  

Avatar
Discard
Author

So assuming there are no explicit calls to commit(), is it fair to say that everything from the entry to the return is contained within a transaction then? And as such in the example case the price used in the _cart_update() process must be the list_price set in the previous line?

yes it because in some case we need to rollback transaction if data don't satisfy the requirement mostly in create/write so it's a good idea to commit at last and rollback on exception.

yes it processed list_price set by the previous line

Related Posts Replies Views Activity
3
Jun 24
1830
0
Apr 24
548
2
Mar 24
4297
1
Mar 23
1959
1
Oct 21
7820