Skip to content

Commit

Permalink
[FIX] product_variant_configurator_manual_creation: faster has_pendin…
Browse files Browse the repository at this point in the history
…g_variants computation for products with huge number of variant combinations
  • Loading branch information
giarve committed Nov 16, 2023
1 parent b3a3cda commit 49f931a
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,59 @@ class ProductTemplate(models.Model):

has_pending_variants = fields.Boolean(
string="Has pending variants?",
compute="_compute_pending_variants",
compute="_compute_has_pending_variants",
)

def _possible_pending_variants_calculation(self):
# Use the maximum number of combinations to avoid infinite processing
# this value is also set in self._create_variant_ids() in `product` module
possible_combinator_downcounter = 1000

Check warning on line 20 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L20

Added line #L20 was not covered by tests

product_template_attribute_values_per_line = [
ptal.product_template_value_ids._only_active()
for ptal in self.valid_product_template_attribute_line_ids
]

# iterate over lines and check whether there is any newId
for line in product_template_attribute_values_per_line:
for product_template_attribute_value in line:
if isinstance(
product_template_attribute_value.id,
models.NewId,
) or isinstance(
product_template_attribute_value.attribute_id.id, models.NewId
):
return False

Check warning on line 36 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L36

Added line #L36 was not covered by tests

cartesian_generator = self._cartesian_product(

Check warning on line 38 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L38

Added line #L38 was not covered by tests
product_template_attribute_values_per_line, None
)

# next() the generator until it's exhausted or hits
# "possible_combinator_downcounter" combinations
while next(cartesian_generator, None):
possible_combinator_downcounter -= 1

Check warning on line 45 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L45

Added line #L45 was not covered by tests
if possible_combinator_downcounter == 0:
return False

Check warning on line 47 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L47

Added line #L47 was not covered by tests

return True

Check warning on line 49 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L49

Added line #L49 was not covered by tests

@api.depends(
"product_variant_ids",
"attribute_line_ids",
"attribute_line_ids.attribute_id",
"attribute_line_ids.value_ids",
)
def _compute_pending_variants(self):
def _compute_has_pending_variants(self):
for rec in self:
if not rec._possible_pending_variants_calculation():
# Disable wizard as it will be impossible
# to compute all values without variant
rec.has_pending_variants = False
continue

Check warning on line 63 in product_variant_configurator_manual_creation/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

product_variant_configurator_manual_creation/models/product_template.py#L62-L63

Added lines #L62 - L63 were not covered by tests

# if there are less than possible_combinator_downcounter possible combinations
# proceed with the calculation
rec.has_pending_variants = bool(self._get_values_without_variant())

def _get_values_without_variant(self):
Expand Down

0 comments on commit 49f931a

Please sign in to comment.