'Prepare pack_operations, returns a list of dict to give at create'

def _prepare_pack_ops(self, quants, forced_qties):

    valid_quants = quants.filtered((lambda quant: (quant.qty > 0)))
    _Mapping = namedtuple('Mapping', ('product', 'package', 'owner', 'location', 'location_dst_id'))
    all_products = ((valid_quants.mapped('product_id') | self.env['product.product'].browse((p.id for p in forced_qties.keys()))) | self.move_lines.mapped('product_id'))
    computed_putaway_locations = dict(((product, (self.location_dest_id.get_putaway_strategy(product) or self.location_dest_id.id)) for product in all_products))
    product_to_uom = dict(((product.id, product.uom_id) for product in all_products))
    picking_moves = self.move_lines.filtered((lambda move: (move.state not in ('done', 'cancel'))))
    for move in picking_moves:
        if ((move.product_uom != product_to_uom[move.product_id.id]) and (move.product_uom.factor > product_to_uom[move.product_id.id].factor)):
            product_to_uom[move.product_id.id] = move.product_uom
    if (len(picking_moves.mapped('location_id')) > 1):
        raise UserError(_('The    source    location    must    be    the    same    for    all    the    moves    of    the    picking.'))
    if (len(picking_moves.mapped('location_dest_id')) > 1):
        raise UserError(_('The    destination    location    must    be    the    same    for    all    the    moves    of    the    picking.'))
    pack_operation_values = []
    top_lvl_packages = valid_quants._get_top_level_packages(computed_putaway_locations)
    for pack in top_lvl_packages:
        pack_quants = pack.get_content()
        pack_operation_values.append({'picking_id': self.id, 'package_id': pack.id, 'product_qty': 1.0, 'location_id': pack.location_id.id, 'location_dest_id': computed_putaway_locations[pack_quants[0].product_id], 'owner_id': pack.owner_id.id})
        valid_quants -= pack_quants
    qtys_grouped = {}
    lots_grouped = {}
    for quant in valid_quants:
        key = _Mapping(quant.product_id, quant.package_id, quant.owner_id, quant.location_id, computed_putaway_locations[quant.product_id])
        qtys_grouped.setdefault(key, 0.0)
        qtys_grouped[key] += quant.qty
        if ((quant.product_id.tracking != 'none') and quant.lot_id):
            lots_grouped.setdefault(key, dict()).setdefault(quant.lot_id.id, 0.0)
            lots_grouped[key][quant.lot_id.id] += quant.qty
    for (product, qty) in forced_qties.items():
        if (qty <= 0.0):
            continue
        key = _Mapping(product, self.env['stock.quant.package'], self.owner_id, self.location_id, computed_putaway_locations[product])
        qtys_grouped.setdefault(key, 0.0)
        qtys_grouped[key] += qty
    Uom = self.env['product.uom']
    product_id_to_vals = {}
    for (mapping, qty) in qtys_grouped.items():
        uom = product_to_uom[mapping.product.id]
        val_dict = {'picking_id': self.id, 'product_qty': mapping.product.uom_id._compute_quantity(qty, uom), 'product_id': mapping.product.id, 'package_id': mapping.package.id, 'owner_id': mapping.owner.id, 'location_id': mapping.location.id, 'location_dest_id': mapping.location_dst_id, 'product_uom_id': uom.id, 'pack_lot_ids': [(0, 0, {'lot_id': lot, 'qty': 0.0, 'qty_todo': lots_grouped[mapping][lot]}) for lot in lots_grouped.get(mapping, {}).keys()]}
        product_id_to_vals.setdefault(mapping.product.id, list()).append(val_dict)
    for move in self.move_lines.filtered((lambda move: (move.state not in ('done', 'cancel')))):
        values = product_id_to_vals.pop(move.product_id.id, [])
        pack_operation_values += values
    return pack_operation_values
