Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] pricelist_cache: update price cache in chuncks #3060

Open
wants to merge 5 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 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 @@ -59,13 +61,18 @@ def create(self, vals_list):
res = super().create(vals_list)
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.mapped("product_id").ids
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
)
product_ids_to_cache = record.item_ids._get_pricelist_products()

for product_chunk_ids in tools.misc.split_every(
PRODUCT_BATCH, product_ids_to_cache
):
self.env[
"product.pricelist.cache"
].with_delay().update_product_pricelist_cache(
product_ids=product_chunk_ids, pricelist_ids=record.ids
)
return res

def _get_product_prices(self, product_ids):
Expand Down
32 changes: 26 additions & 6 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 All @@ -25,6 +27,19 @@ def _has_date_range(self):
"""Returns whether any of the item records in recordset is based on dates."""
return any(bool(record.date_start or record.date_end) for record in self)

def _get_pricelist_products(self):
products = []
for rec in self:
if rec.product_tmpl_id.id:
products = (
self.env["product.product"]
.search([("product_tmpl_id", "=", rec.product_tmpl_id.id)])
.ids
)
else:
products = rec.product_id.ids
return products

vuwnevska marked this conversation as resolved.
Show resolved Hide resolved
def _get_pricelist_products_group(self):
"""Returns a mapping of products grouped by pricelist.

Expand All @@ -34,15 +49,19 @@ def _get_pricelist_products_group(self):
"""
pricelist_products = defaultdict(list)
for item in self:
pricelist_products[item.pricelist_id.id].append(item.product_id.id)
pricelist_products[item.pricelist_id.id].extend(
item._get_pricelist_products()
)
return pricelist_products

def update_product_pricelist_cache(self):
"""Executed when a product item is modified. Filters items not based
on variants or based on dates, then updates the cache.
"""
# Filter items applied on variants
items = self.filtered(lambda i: i.applied_on == "0_product_variant")
items = self.filtered(
lambda i: i.applied_on in ["0_product_variant", "1_product"]
vuwnevska marked this conversation as resolved.
Show resolved Hide resolved
)
# Filter items based on dates
item_ids_to_update = []
for item in items:
Expand All @@ -58,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]
)