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 12, 2023
1 parent 2aad05f commit 8daf409
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
18 changes: 12 additions & 6 deletions product_assortment/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">

<record id="action_product_assortment_invalidate_cache" model="ir.actions.server">
<field name="name">Product assortment reset cache</field>
<!-- Force to recompute all partners to take into account changes in related partner fields models.
P.E.: Domain includes tag_ids.name contains "**" and user changes tag name from tags view -->
<record
id="action_product_assortment_recompute_all_partners"
model="ir.actions.server"
>
<field name="name">Product assortment recompute all partbers</field>
<field name="model_id" ref="model_ir_filters" />
<field name="binding_model_id" ref="model_ir_filters" />
<field name="state">code</field>
<field name="code">model.clear_caches()</field>
<field name="code">model.search([])._compute_all_partner_ids()</field>
</record>

<record id="ir_cron_product_assortment_invalidate_cache" model="ir.cron">
<!-- Cron to recompute all partners. (Not active by default). -->
<record id="ir_cron_product_assortment_recompute_all_partners" model="ir.cron">
<field
name="ir_actions_server_id"
ref="action_product_assortment_invalidate_cache"
ref="action_product_assortment_recompute_all_partners"
/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False" />
<field name="active" eval="False" />
<field
name="nextcall"
eval="(DateTime.now().replace(hour=3, minute=0, second=0) + timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')"
Expand Down
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 = []

Check warning on line 42 in product_assortment/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

product_assortment/models/res_partner.py#L42

Added line #L42 was not covered by tests
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(

Check warning on line 48 in product_assortment/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

product_assortment/models/res_partner.py#L47-L48

Added lines #L47 - L48 were not covered by tests
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()

Check warning on line 62 in product_assortment/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

product_assortment/models/res_partner.py#L62

Added line #L62 was not covered by tests
return res

0 comments on commit 8daf409

Please sign in to comment.