From 6939f21174c7aa7f9ba6a14d1578a9c9aff95154 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Wed, 25 Oct 2023 09:41:45 +0200 Subject: [PATCH] [FIX] product_attribute_variant_rules: Review fixes --- .../models/product_attribute_rule.py | 18 ++++-------------- .../models/product_template.py | 2 +- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/product_attribute_variant_rules/models/product_attribute_rule.py b/product_attribute_variant_rules/models/product_attribute_rule.py index dcfb1eec954..94aa0b52342 100644 --- a/product_attribute_variant_rules/models/product_attribute_rule.py +++ b/product_attribute_variant_rules/models/product_attribute_rule.py @@ -1,8 +1,8 @@ # Copyright 2023 Akretion (https://www.akretion.com). # @author Florian Mounier -from collections import defaultdict from odoo import api, fields, models +from odoo.tools import groupby class ProductAttributeRule(models.Model): @@ -126,9 +126,9 @@ def _combination_matches_conditions(self, combination, conditions): # Check if the combination matches the preconditions ANDed between # different attributes - for attribute, attribute_values in self._aggregate_conditions( - conditions - ).items(): + for attribute, attribute_values in groupby( + conditions, lambda condition: condition.attribute_id + ): if ( combination.filtered( lambda value: value.attribute_id == attribute @@ -140,13 +140,3 @@ def _combination_matches_conditions(self, combination, conditions): # The combination matches all preconditions return True - - def _aggregate_conditions(self, conditions): - """ - Group the attribute values by attribute. - """ - aggregated_conditions = defaultdict(set) - for condition in conditions: - aggregated_conditions[condition.attribute_id].add(condition) - - return aggregated_conditions diff --git a/product_attribute_variant_rules/models/product_template.py b/product_attribute_variant_rules/models/product_template.py index 57f0348a10b..d2dc7b7e310 100644 --- a/product_attribute_variant_rules/models/product_template.py +++ b/product_attribute_variant_rules/models/product_template.py @@ -57,7 +57,7 @@ def _is_combination_possible_with_rules(self, combination): return True def write(self, vals): - res = super(ProductTemplate, self).write(vals) + res = super().write(vals) # Recreate variants if the rules have changed empty = vals.get("active") and len(self.product_variant_ids) == 0