From e39e2c70cb4c298397e5d773858875086f46758a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Alix?= Date: Fri, 5 Jul 2024 09:45:46 +0200 Subject: [PATCH] [IMP] product_packaging_container_deposit: improve performance Do not write on `sale.order.order_line` field, this is somehow slowing down the process. Creating/updating `sale.order.line` directly is faster. --- .../models/container_deposit_order_mixin.py | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/product_packaging_container_deposit/models/container_deposit_order_mixin.py b/product_packaging_container_deposit/models/container_deposit_order_mixin.py index a405d0d72b62..b06f48e6618c 100644 --- a/product_packaging_container_deposit/models/container_deposit_order_mixin.py +++ b/product_packaging_container_deposit/models/container_deposit_order_mixin.py @@ -4,7 +4,7 @@ import logging from functools import partial -from odoo import Command, _, models +from odoo import _, models _logger = logging.getLogger(__name__) @@ -50,7 +50,8 @@ def update_order_container_deposit_quantity(self): deposit_container_qties = ( lines_to_comp_deposit._get_order_lines_container_deposit_quantities() ) - values_lst = [] + lines_to_update = {} + lines_to_create = [] for line in self[self._get_order_line_field()]: if not line.is_container_deposit: continue @@ -67,30 +68,25 @@ def update_order_container_deposit_quantity(self): # TODO: check if it is needed for UI only new_vals["name"] = _("[DEL] %(name)s", name=line.name) # else: - values_lst.append( - Command.update( - line.id, - new_vals, - ) - ) + lines_to_update[line.id] = new_vals else: - values_lst.append( - Command.update( - line.id, - { - line._get_product_qty_field(): qty, - line._get_product_qty_delivered_received_field(): qty_dlvd_rcvd, - }, - ) - ) + lines_to_update[line.id] = { + line._get_product_qty_field(): qty, + line._get_product_qty_delivered_received_field(): qty_dlvd_rcvd, + } for product in deposit_container_qties: if deposit_container_qties[product][0]: values = order.prepare_deposit_container_line( product, deposit_container_qties[product][0] ) - values_lst.append(Command.create(values)) - order.write({self._get_order_line_field(): values_lst}) + values["order_id"] = order.id + lines_to_create.append(values) + line_model = self._fields[self._get_order_line_field()].comodel_name + for line_id, values in lines_to_update.items(): + self.env[line_model].browse(line_id).exists().write(values) + if lines_to_create: + self.env[line_model].create(lines_to_create) # Schedule line to delete after commit to avoid caching issue w/ UI if line_ids_to_delete: self.env.cr.postcommit.add(