Skip to content

Commit

Permalink
[IMP] product_pricelist: chunk products update
Browse files Browse the repository at this point in the history
  • Loading branch information
Iryna Vyshnevska committed Apr 8, 2024
1 parent 2b404d5 commit 0c6df85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 12 additions & 6 deletions pricelist_cache/models/product_pricelist.py
Expand Up @@ -3,7 +3,9 @@

from datetime import date

from odoo import api, fields, models
from odoo import api, fields, models, tools

from .product_pricelist_item import PRODUCT_BATCH


class Pricelist(models.Model):
Expand Down Expand Up @@ -57,15 +59,19 @@ def _get_parent_list_tree(self):
@api.model_create_multi
def create(self, vals_list):
res = super().create(vals_list)
cache_model = self.env["product.pricelist.cache"].with_delay()
for record in res:
if record._is_factor_pricelist() or record._is_global_pricelist():
product_ids_to_cache = None
product_ids_to_cache = self.env["product.product"].search([]).ids
else:
product_ids_to_cache = record.item_ids._get_pricelist_products()
cache_model = self.env["product.pricelist.cache"].with_delay()
cache_model.update_product_pricelist_cache(
product_ids=product_ids_to_cache, pricelist_ids=record.ids
)

for product_chunk_ids in tools.misc.split_every(
PRODUCT_BATCH, product_ids_to_cache
):
cache_model.update_product_pricelist_cache(
product_ids=product_chunk_ids, pricelist_ids=record.ids
)
return res

def _get_product_prices(self, product_ids):
Expand Down
11 changes: 7 additions & 4 deletions pricelist_cache/models/product_pricelist_item.py
Expand Up @@ -3,7 +3,9 @@

from collections import defaultdict

from odoo import fields, models
from odoo import fields, models, tools

PRODUCT_BATCH = 1000


class PricelistItem(models.Model):
Expand Down Expand Up @@ -75,6 +77,7 @@ def update_product_pricelist_cache(self):
# Update cache
cache_object = self.env["product.pricelist.cache"]
for pricelist_id, product_ids in pricelist_products.items():
cache_object.with_delay().update_product_pricelist_cache(
product_ids=product_ids, pricelist_ids=[pricelist_id]
)
for product_chunk_ids in tools.misc.split_every(PRODUCT_BATCH, product_ids):
cache_object.with_delay().update_product_pricelist_cache(
product_ids=product_chunk_ids, pricelist_ids=[pricelist_id]
)

0 comments on commit 0c6df85

Please sign in to comment.