From 9301abab02cbc4b4b7390afa9791d9e14ee4e5a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 28 Aug 2017 18:25:22 +0200 Subject: [PATCH] Use defaultdict to avoid KeyError This can happen if all products from incoming moves are not in the inventory moves. For example, if the quantity of an incoming move is set to zero. issue6705 review43391002 --- stock.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/stock.py b/stock.py index 0be650aa4c..98f209a869 100644 --- a/stock.py +++ b/stock.py @@ -1,5 +1,7 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from collections import defaultdict + from trytond.model import ModelView, Workflow from trytond.pool import Pool, PoolMeta @@ -27,11 +29,8 @@ class ShipmentIn: 'in', move_ids), ('purchase_request.origin', 'like', 'sale.sale,%'), ]) - pbl = {} + pbl = defaultdict(lambda: defaultdict(lambda: 0)) for move in shipment.inventory_moves: - pbl.setdefault(move.product, {}).setdefault( - move.to_location, 0.0) - pbl[move.product][move.to_location] += \ - move.internal_quantity + pbl[move.product][move.to_location] += move.internal_quantity for sale_line in sale_lines: sale_line.assign_supplied(pbl[sale_line.product])