From 4ebf0322d7d9acd60be8451c9934acaf2e0cb760 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Mon, 20 Feb 2023 19:19:24 +0100 Subject: [PATCH 1/3] [FIX] product_assortment: Product black_list_domain invalidates whitelist_product_ids --- product_assortment/models/ir_filters.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/product_assortment/models/ir_filters.py b/product_assortment/models/ir_filters.py index 2deb6d7b12e..d619b2489a9 100644 --- a/product_assortment/models/ir_filters.py +++ b/product_assortment/models/ir_filters.py @@ -90,15 +90,6 @@ def _compute_all_partner_ids(self): def _get_eval_domain(self): res = super()._get_eval_domain() - - if self.whitelist_product_ids: - result_domain = [("id", "in", self.whitelist_product_ids.ids)] - res = expression.OR([result_domain, res]) - - if self.blacklist_product_ids: - result_domain = [("id", "not in", self.blacklist_product_ids.ids)] - res = expression.AND([result_domain, res]) - if self.apply_black_list_product_domain: black_list_domain = safe_eval( self.black_list_product_domain, @@ -107,6 +98,12 @@ def _get_eval_domain(self): res = expression.AND( [expression.distribute_not(["!"] + black_list_domain), res] ) + if self.whitelist_product_ids: + result_domain = [("id", "in", self.whitelist_product_ids.ids)] + res = expression.OR([result_domain, res]) + if self.blacklist_product_ids: + result_domain = [("id", "not in", self.blacklist_product_ids.ids)] + res = expression.AND([result_domain, res]) return res def _get_eval_black_list_domain(self): From 2aad05fbd0b071148f2653f8242c91cc54693151 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Mon, 20 Feb 2023 23:23:44 +0100 Subject: [PATCH 2/3] [IMP] product_assortment: Add new methods to improve performance using ormcache --- product_assortment/__manifest__.py | 7 ++++- product_assortment/data/ir_cron.xml | 27 +++++++++++++++++++ product_assortment/i18n/es.po | 13 ++++++--- .../i18n/product_assortment.pot | 7 +++++ product_assortment/models/ir_filters.py | 24 +++++++++++++++++ product_assortment/models/res_partner.py | 8 ++++++ 6 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 product_assortment/data/ir_cron.xml diff --git a/product_assortment/__manifest__.py b/product_assortment/__manifest__.py index ffa756fc72b..60e89fee6f2 100644 --- a/product_assortment/__manifest__.py +++ b/product_assortment/__manifest__.py @@ -1,4 +1,5 @@ # Copyright 2021 ACSONE SA/NV +# Copyright 2023 Tecnativa - Carlos Dauden # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { @@ -11,6 +12,10 @@ "author": "ACSONE SA/NV,Odoo Community Association (OCA)", "website": "https://github.com/OCA/product-attribute", "depends": ["base", "product"], - "data": ["views/product_assortment.xml", "views/res_partner_view.xml"], + "data": [ + "data/ir_cron.xml", + "views/product_assortment.xml", + "views/res_partner_view.xml", + ], "installable": True, } diff --git a/product_assortment/data/ir_cron.xml b/product_assortment/data/ir_cron.xml new file mode 100644 index 00000000000..fc455e79836 --- /dev/null +++ b/product_assortment/data/ir_cron.xml @@ -0,0 +1,27 @@ + + + + + Product assortment reset cache + + + code + model.clear_caches() + + + + + 1 + days + -1 + + + + + diff --git a/product_assortment/i18n/es.po b/product_assortment/i18n/es.po index 41b22e69837..1709e8e1e8e 100644 --- a/product_assortment/i18n/es.po +++ b/product_assortment/i18n/es.po @@ -6,9 +6,9 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 13.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2022-07-19 05:53+0000\n" -"PO-Revision-Date: 2022-07-19 07:55+0200\n" -"Last-Translator: Sergio Teruel \n" +"POT-Creation-Date: 2023-03-06 11:12+0000\n" +"PO-Revision-Date: 2023-03-06 12:14+0100\n" +"Last-Translator: Carlos Dauden \n" "Language-Team: \n" "Language: es\n" "MIME-Version: 1.0\n" @@ -109,6 +109,13 @@ msgstr "Surtido de producto" msgid "Product assortment" msgstr "Surtido de producto" +#. module: product_assortment +#: model:ir.actions.server,name:product_assortment.action_product_assortment_invalidate_cache +#: model:ir.cron,cron_name:product_assortment.ir_cron_product_assortment_invalidate_cache +#: model:ir.cron,name:product_assortment.ir_cron_product_assortment_invalidate_cache +msgid "Product assortment reset cache" +msgstr "Resetear cache de surtido de producto" + #. module: product_assortment #: code:addons/product_assortment/models/ir_filters.py:0 #, python-format diff --git a/product_assortment/i18n/product_assortment.pot b/product_assortment/i18n/product_assortment.pot index d4aa4a16f46..a2b0a2c2331 100644 --- a/product_assortment/i18n/product_assortment.pot +++ b/product_assortment/i18n/product_assortment.pot @@ -105,6 +105,13 @@ msgstr "" msgid "Product assortment" msgstr "" +#. module: product_assortment +#: model:ir.actions.server,name:product_assortment.action_product_assortment_invalidate_cache +#: model:ir.cron,cron_name:product_assortment.ir_cron_product_assortment_invalidate_cache +#: model:ir.cron,name:product_assortment.ir_cron_product_assortment_invalidate_cache +msgid "Product assortment reset cache" +msgstr "" + #. module: product_assortment #: code:addons/product_assortment/models/ir_filters.py:0 #, python-format diff --git a/product_assortment/models/ir_filters.py b/product_assortment/models/ir_filters.py index d619b2489a9..4a1f0140090 100644 --- a/product_assortment/models/ir_filters.py +++ b/product_assortment/models/ir_filters.py @@ -1,8 +1,10 @@ # Copyright 2021 ACSONE SA/NV +# Copyright 2023 Tecnativa - Carlos Dauden # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import _, api, fields, models from odoo.osv import expression +from odoo.tools import ormcache from odoo.tools.safe_eval import datetime, safe_eval @@ -74,6 +76,22 @@ def create(self, vals_list): string="Restricted product domain", default="[]", required=True ) + @ormcache("self.id") + def get_all_partner_ids(self): + self.ensure_one() + return self.all_partner_ids.ids + + @api.model + @ormcache() + def get_partner_domain_fields(self): + field_set = set() + for ir_filter in self.sudo().search([("is_assortment", "=", True)]): + domain = ir_filter._get_eval_partner_domain() + for item in domain: + if isinstance(item, (list, tuple)) and isinstance(item[0], str): + field_set.add(item[0].split(".")[0]) + return field_set + @api.depends("partner_ids", "partner_domain") def _compute_all_partner_ids(self): """Summarize selected partners and partners from partner domain field""" @@ -141,6 +159,12 @@ def _get_action_domain(self, action_id=None): return domain + def write(self, vals): + res = super().write(vals) + if "partner_ids" in vals or "partner_domain" in vals: + self.clear_caches() + return res + def show_products(self): self.ensure_one() xmlid = "product.product_normal_action_sell" diff --git a/product_assortment/models/res_partner.py b/product_assortment/models/res_partner.py index d1da9ea5e64..c155ba6a39d 100644 --- a/product_assortment/models/res_partner.py +++ b/product_assortment/models/res_partner.py @@ -1,4 +1,5 @@ # Copyright 2021 Tecnativa - Carlos Roca +# Copyright 2021 Tecnativa - Carlos Dauden # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). from odoo import models @@ -24,3 +25,10 @@ def action_define_product_assortment(self): ) action["context"] = ctx return action + + def write(self, vals): + res = super().write(vals) + IrFilters = self.env["ir.filters"] + if IrFilters.get_partner_domain_fields() & set(vals.keys()): + IrFilters.clear_caches() + return res From d05fd5a594fd84687bdcf76903ccf26799b9c449 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Mon, 5 Jun 2023 18:10:34 +0200 Subject: [PATCH 3/3] [IMP] product_assortment: New field applied_assortments_ids to avoid cache "inefficiency" using workers because cache is isolated by worker --- product_assortment/data/ir_cron.xml | 18 +++++++++----- product_assortment/models/ir_filters.py | 5 ---- product_assortment/models/res_partner.py | 31 ++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/product_assortment/data/ir_cron.xml b/product_assortment/data/ir_cron.xml index fc455e79836..1315b9a1fca 100644 --- a/product_assortment/data/ir_cron.xml +++ b/product_assortment/data/ir_cron.xml @@ -1,23 +1,29 @@ - - - Product assortment reset cache + + + Product assortment recompute all partbers code - model.clear_caches() + model.search([])._compute_all_partner_ids() - + + 1 days -1 +