[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).
This commit is contained in:
Victor Feyens
2020-07-23 16:38:17 +00:00
parent 5dc8b7bdd8
commit addffca423
2 changed files with 16 additions and 12 deletions
@@ -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')
@@ -5,6 +5,7 @@
<field name="model">choose.delivery.package</field>
<field name="arch" type="xml">
<form string="Package">
<field name="picking_id" invisible="1"/>
<group>
<field name="delivery_packaging_id" domain="[('package_carrier_type', '=', context.get('current_package_carrier_type', 'none'))]"
context="{'form_view_ref':'delivery.product_packaging_delivery_form'}"/>