Skip to content

Commit

Permalink
[IMP] product_assortment: New field applied_assortments_ids to avoid …
Browse files Browse the repository at this point in the history
…cache "inefficiency" using workers because cache is isolated by worker
  • Loading branch information
carlosdauden committed Jun 5, 2023
1 parent 2aad05f commit f6a4f14
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
1 change: 0 additions & 1 deletion product_assortment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"website": "https://github.com/OCA/product-attribute",
"depends": ["base", "product"],
"data": [
"data/ir_cron.xml",
"views/product_assortment.xml",
"views/res_partner_view.xml",
],
Expand Down
27 changes: 0 additions & 27 deletions product_assortment/data/ir_cron.xml

This file was deleted.

5 changes: 0 additions & 5 deletions product_assortment/models/ir_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ 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):
Expand Down
33 changes: 31 additions & 2 deletions product_assortment/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# 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
from odoo import api, fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

applied_assortments_ids = fields.Many2many(
comodel_name="ir.filters",
relation="ir_filter_all_partner_rel",
column1="partner_id",
column2="filter_id",
)

def action_define_product_assortment(self):
self.ensure_one()
xmlid = "product_assortment.actions_product_assortment_view"
Expand All @@ -26,9 +33,31 @@ def action_define_product_assortment(self):
action["context"] = ctx
return action

def _update_partner_assortments(self):
# Using sudo to contemplate evaluation of domains with restricted fields
self = self.sudo()
assortments = self.env["ir.filters"].search([("is_assortment", "=", True)])
for partner in self:
# Use ids instead of record to improve performance (Remove in next versions)
partner_assortments_ids = []
for assortment in assortments:
if partner in assortment.partner_ids or partner.filtered_domain(
assortment._get_eval_partner_domain()
):
partner_assortments_ids += assortment.id
partner.applied_assortments_ids = assortments.browse(
partner_assortments_ids
)

@api.model_create_multi
def create(self, vals_list):
partners = super().create(vals_list)
self._update_partner_assortments()
return partners

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()
self._update_partner_assortments()
return res

0 comments on commit f6a4f14

Please sign in to comment.