Allowing to use python's 'with' statement on browse records
Take following typical code:
for move in self.browse(cr, uid, ids, context=context):
price = move.price_unit
if move.partner_id:
# Apply supplier cost to price, first ratio then fixed cost
price *= move.product_
A better implementation would be:
for move in self.browse(cr, uid, ids, context=context):
price = move.price_unit
with move.partner_id as partner:
# Apply supplier cost to price, first ratio then fixed cost
price *= move.product_
This would have a cleaner effect, more concise code and is relatively easy to implement and will not break back-ward compatibility.
Blueprint information
- Status:
- Complete
- Approver:
- OpenERP Core Team
- Priority:
- Undefined
- Drafter:
- Niels Huylebroeck
- Direction:
- Needs approval
- Assignee:
- Niels Huylebroeck
- Definition:
- Obsolete
- Series goal:
- None
- Implementation:
- Needs Code Review
- Milestone target:
- None
- Started by
- Niels Huylebroeck
- Completed by
- Niels Huylebroeck
Related branches
Related bugs
Sprints
Whiteboard
As far as I can see, the semantics are completely different: the code at the bottom will try to perform the computation even if move.partner_id is not set, context managers can not bypass the body
So the code at the bottom is equivalent to removing the conditional altogether.