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 22, 2023
1 parent 50194ff commit a401833
Showing 1 changed file with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,71 @@ 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):
# If the product has not been created yet,
# it is useless to check for pending variants.
if isinstance(
self.id,
models.NewId,
):
return False

Check warning on line 24 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#L24

Added line #L24 was not covered by tests

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

Check warning on line 28 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#L28

Added line #L28 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 44 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#L44

Added line #L44 was not covered by tests

# If line is empty (no configurable attribute values)
# remove it from the list so _cartesian_product still works
if not line:
product_template_attribute_values_per_line.remove(line)

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

cartesian_generator = self._cartesian_product(

Check warning on line 51 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#L51

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

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

Check warning on line 58 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#L58

Added line #L58 was not covered by tests
if possible_combination_downcounter == 0:
return False

Check warning on line 60 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#L60

Added line #L60 was not covered by tests

return True

Check warning on line 62 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

Added line #L62 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 76 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#L75-L76

Added lines #L75 - L76 were not covered by tests

# proceed with the accurate calculation
rec.has_pending_variants = bool(self._get_values_without_variant())

def _get_values_without_variant(self):
Expand Down

0 comments on commit a401833

Please sign in to comment.