From addffca4235b0f751f3ad43943ca837544b307b6 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Tue, 14 Jul 2020 14:37:37 +0000 Subject: [PATCH] [IMP] delivery: correct default_get behavior When a fields default value depends on another default, this value should be computed in a default_get override, not in a default method specified on the field itself. Furthermore, the management of default_ context keys() should be left to the orm level (better code understanding, localizing a behavior in one place instead of multiple places). --- .../wizard/choose_delivery_package.py | 27 ++++++++++--------- .../wizard/choose_delivery_package_views.xml | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/addons/delivery/wizard/choose_delivery_package.py b/addons/delivery/wizard/choose_delivery_package.py index 789e74e0d72..7151c2510be 100644 --- a/addons/delivery/wizard/choose_delivery_package.py +++ b/addons/delivery/wizard/choose_delivery_package.py @@ -9,21 +9,24 @@ class ChooseDeliveryPackage(models.TransientModel): _name = 'choose.delivery.package' _description = 'Delivery Package Selection Wizard' - def _default_shipping_weight(self): - picking = self.env['stock.picking'].browse(self.env.context.get('default_picking_id')) - move_line_ids = picking.move_line_ids.filtered(lambda m: - float_compare(m.qty_done, 0.0, precision_rounding=m.product_uom_id.rounding) > 0 - and not m.result_package_id - ) - total_weight = 0.0 - for ml in move_line_ids: - qty = ml.product_uom_id._compute_quantity(ml.qty_done, ml.product_id.uom_id) - total_weight += qty * ml.product_id.weight - return total_weight + def default_get(self, fields_list): + defaults = super().default_get(fields_list) + if 'shipping_weight' in fields_list: + picking = self.env['stock.picking'].browse(defaults.get('picking_id')) + move_line_ids = picking.move_line_ids.filtered(lambda m: + float_compare(m.qty_done, 0.0, precision_rounding=m.product_uom_id.rounding) > 0 + and not m.result_package_id + ) + total_weight = 0.0 + for ml in move_line_ids: + qty = ml.product_uom_id._compute_quantity(ml.qty_done, ml.product_id.uom_id) + total_weight += qty * ml.product_id.weight + defaults['shipping_weight'] = total_weight + return defaults picking_id = fields.Many2one('stock.picking', 'Picking') delivery_packaging_id = fields.Many2one('product.packaging', 'Delivery Packaging', check_company=True) - shipping_weight = fields.Float('Shipping Weight', default=_default_shipping_weight) + shipping_weight = fields.Float('Shipping Weight') weight_uom_name = fields.Char(string='Weight unit of measure label', compute='_compute_weight_uom_name') company_id = fields.Many2one(related='picking_id.company_id') diff --git a/addons/delivery/wizard/choose_delivery_package_views.xml b/addons/delivery/wizard/choose_delivery_package_views.xml index 2eb671e0e8f..5f39d08d81a 100644 --- a/addons/delivery/wizard/choose_delivery_package_views.xml +++ b/addons/delivery/wizard/choose_delivery_package_views.xml @@ -5,6 +5,7 @@ choose.delivery.package
+