Skip to content

Commit

Permalink
[IMP] product_packaging_container_deposit: improve performance
Browse files Browse the repository at this point in the history
Do not write on `sale.order.order_line` field, this is somehow slowing
down the process. Creating/updating `sale.order.line` directly is
faster.
  • Loading branch information
sebalix committed Jul 5, 2024
1 parent 0212ec6 commit e39e2c7
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from functools import partial

from odoo import Command, _, models
from odoo import _, models

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit e39e2c7

Please sign in to comment.