Skip to content

Commit

Permalink
Add conditional operator to filter motor products by test results
Browse files Browse the repository at this point in the history
Enhanced the `MotorDismantleResult` model to include a conditional operator for more flexible filtering. Implemented the `is_condition_met` method to evaluate test results against the specified condition. Updated the relevant view and logic to support these changes.
  • Loading branch information
cbusillo committed Oct 27, 2024
1 parent 36aba9c commit a103acb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion product_connect/models/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def create_motor_products(self) -> None:

if any(
test.template.id in product_template.excluded_by_tests.conditional_test.ids
and product_template.excluded_by_tests.condition_value.lower() == test.computed_result.lower()
and product_template.excluded_by_tests.is_condition_met(test.computed_result)
for test in motor.tests
):
continue
Expand Down
35 changes: 34 additions & 1 deletion product_connect/models/motor_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Self

import odoo
from odoo import api, fields, models
from odoo import api, fields, models, exceptions


class MotorDismantleResult(models.Model):
Expand All @@ -19,8 +19,41 @@ class MotorProductTemplateCondition(models.Model):
template = fields.Many2one("motor.product.template", ondelete="cascade")
conditional_test = fields.Many2one("motor.test.template", ondelete="cascade", required=True)
condition_value = fields.Char(required=True)
conditional_operator = fields.Selection(
[
(">", "Greater Than"),
("<", "Less Than"),
(">=", "Greater Than or Equal To"),
("<=", "Less Than or Equal To"),
("=", "Equal To"),
("!=", "Not Equal To"),
],
required=True,
default="=",
)
excluded_by_tests = fields.One2many("motor.product", "template", string="Excluded by Tests")

def is_condition_met(self, test_value: str | float) -> bool:
self.ensure_one()
if not self.conditional_operator or not self.condition_value:
return False

if self.conditional_test.result_type == "selection":
if self.conditional_operator not in ["=", "!="]:
raise exceptions.UserError("Conditional Operator must be equal to '=' or '!=' for Selection Test Type.")

conditional_map = {
">": lambda x, y: x > y,
"<": lambda x, y: x < y,
">=": lambda x, y: x >= y,
"<=": lambda x, y: x <= y,
"=": lambda x, y: x == y,
"!=": lambda x, y: x != y,
}

operation = conditional_map.get(self.conditional_operator)
return operation(test_value, self.condition_value)


class MotorProductTemplate(models.Model):
_name = "motor.product.template"
Expand Down
1 change: 1 addition & 0 deletions product_connect/views/motor_product_template_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<field name="excluded_by_tests">
<tree editable="bottom">
<field name="conditional_test"/>
<field name="conditional_operator"/>
<field name="condition_value"/>
</tree>
</field>
Expand Down

0 comments on commit a103acb

Please sign in to comment.