Friday, October 22, 2010

OpenERP on_change: bunch of parameters vs dictionaries

def product_uom_change(self, cursor, user, ids, pricelist, product, qty=0,
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
            lang=False, update_tax=True, date_order=False):
        res = self.product_id_change(cursor, user, ids, pricelist, product,
                qty=qty, uom=uom, qty_uos=qty_uos, uos=uos, name=name,
                partner_id=partner_id, lang=lang, update_tax=update_tax,
                date_order=date_order)
        if 'product_uom' in res['value']:
            del res['value']['product_uom']
        if not uom:
            res['value']['price_unit'] = 0.0
        return res

Look at this on_change, 15 parameters and calling another one with 14 parameters. This is too much, a good idea will be to use dictionaries:


def product_uom_change(self, cursor, user, ids, params, context=None):
        res = self.product_id_change(cursor, user, ids, params, context=context)
        if 'product_uom' in res['value']:
            del res['value']['product_uom']
        if not params['uom']:
            res['value']['price_unit'] = 0.0
        return res

PS: Here I have also a context parameter, OpenERP and his CEO Fabien Pinckaers forget it frequently.

2 comments:

  1. Hi, thanks for the great idea. But could you give an example of the function call in the xml view ?


    thx
    p

    ReplyDelete
  2. Good day I have a question on one ship_via sale.order defined variable (many2one) as I can do to send the call to multiply product_id_change worth ship_via.porcentaje to price_unit and update the new price_ unit this unit is part of the sale.order.line thanks

    ReplyDelete