How to do it...

To trace the execution of export_stock_level() with pdb, perform the following steps:

  1. Edit the code of the method, and insert the line highlighted here:
def export_stock_level(self, stock_location):  
import pdb; pdb.set_trace()
products = self.with_context( location=stock_location.id ).search([])
fname = join(EXPORTS_DIR, 'stock_level.txt')
try:
with open(fname, 'w') as fobj:
for prod in products.filtered('qty_available'):
fobj.write('%s %f ' % (prod.name, prod.qty_available))
except IOError:
raise exceptions.UserError('unable to save file')
  1. Run the method. We will use the Odoo shell, as explained in the Using the Odoo shell to interactively call methods recipe:
    $ ./odoo-bin shell -c project.cfg --log-level=error
    [...]
    >>> product = env['product.product']
    >>> location_stock = env.ref('stock.stock_location_stock')
    >>> product.export_stock_level(location_stock)
    > /home/cookbook/stock_level/models.py(18)export_stock_level()
    -> products = self.with_context(
    (Pdb)  
  1. At the (Pdb) prompt, issue the args command (the shortcut of which is a) to get the values of the arguments that were passed to the method:
(Pdb) a 
self = product.product() 
stock_location = stock.location(14,) 
  1. Enter the list command to check where in the code you are standing:
(Pdb) list 
 13       @api.model
14 def export_stock_level(self, stock_location):
15 _logger.info('export stock level for %s',
16 stock_location.name)
17 import pdb; pdb.set_trace()
18 -> products = self.with_context(
19 location=stock_location.id).search([])
20 products = products.filtered('qty_available')
21 _logger.debug('%d products in the location',
22 len(products))
23 fname = join(EXPORTS_DIR, 'stock_level.txt')
(Pdb)
  1. Enter the next command three times to walk through the first lines of the method. You may also use n, which is a shortcut:
(Pdb) next
> /home/cookbook/stock_level/models.py(19)export_stock_level()
-> location=stock_location.id).search([])
(Pdb) n
> /home/cookbook/stock_level/models.py(20)export_stock_level()
-> products = products.filtered('qty_available')
(Pdb) n
> /home/cookbook/stock_level/models.py(21)export_stock_level()
-> _logger.debug('%d products in the location',
(Pdb) n
> /home/cookbook/stock_level/models.py(22)export_stock_level()
-> len(products))
(Pdb) n
> /home/cookbook/stock_level/models.py(23)export_stock_level()
-> fname = join(EXPORTS_DIR, 'stock_level.txt')
(Pdb) n
> /home/cookbook/stock_level/models.py(24)export_stock_level()
-> try:
  1. Use the p command to display the values of the products and fname variables:
(Pdb) p products
product.product(32, 14, 17, 19, 21, 22, 23, 29, 34, 33, 26, 27, 42) (Pdb) p fname '/srv/exports/stock_level.txt'
  1. Change the value of fname to point to the /tmp directory:
(Pdb) !fname = '/tmp/stock_level.txt'
  1. Use the return (shortcut: r) command to execute the current function:
(Pdb) return
--Return--
> /home/cookbook/stock_level/models.py(26)export_stock_level()->None
-> for product in products:
  
  1. Use the cont (shortcut: c) command to resume the execution of the program:
(Pdb) c
>>>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset