Allowing to use python's 'with' statement on browse records

Registered by Niels Huylebroeck

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.partner_id.supplier_surplus_ratio
                price += move.partner_id.supplier_surplus_cost
            price *= move.product_uos_qty

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 *= partner.supplier_surplus_ratio
                price += partner.supplier_surplus_cost
            price *= move.product_uos_qty

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

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.

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.