Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
[ENH] Raise more meaningful configuration warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard deMeester committed May 5, 2017
1 parent 89750da commit 00785c1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion product_configurator/demo/product_attribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
ref('product_configurator.product_attribute_value_tow_hook'),
]
)]"/>
<field name="required" eval="True"/>
<field name="required" eval="False"/>
<field name="multi" eval="True"/>
</record>

Expand Down
3 changes: 3 additions & 0 deletions product_configurator/demo/product_config_lines.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ref('product_attribute_value_m235i'),
ref('product_attribute_value_m235i_xdrive')])]"/>
<field name="domain_id" ref="product_config_domain_gasoline"/>
<field name="rule_description">Gasoline Fuel requires a Gasoline Engine to be selected</field>
</record>

<record id="product_config_line_diesel_engines" model="product.config.line">
Expand All @@ -22,6 +23,7 @@
ref('product_attribute_value_220d_xdrive'),
ref('product_attribute_value_225d')])]"/>
<field name="domain_id" ref="product_config_domain_diesel"/>
<field name="rule_description">Diesel Fuel requires a Diesel Engine to be selected</field>
</record>

<record id="product_config_line_218_lines" model="product.config.line">
Expand All @@ -31,6 +33,7 @@
ref('product_attribute_value_sport_line'),
ref('product_attribute_value_luxury_line')])]"/>
<field name="domain_id" ref="product_config_domain_218_engine"/>
<field name="rule_description">218i Engine has a limited selection of Lines</field>
</record>

<record id="product_config_line_luxury_lines" model="product.config.line">
Expand Down
21 changes: 18 additions & 3 deletions product_configurator/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ def create_get_variant(self, value_ids, custom_values=None):
"""
if custom_values is None:
custom_values = {}
valid = self.validate_configuration(value_ids, custom_values)
valid = self.validate_configuration(value_ids, custom_values,
do_raise=True)
if not valid:
raise ValidationError(_('Invalid Configuration'))

Expand Down Expand Up @@ -440,7 +441,8 @@ def values_available(self, attr_val_ids, sel_val_ids):
return avail_val_ids

@api.multi
def validate_configuration(self, value_ids, custom_vals=None, final=True):
def validate_configuration(self, value_ids,
custom_vals=None, final=True, do_raise=False):
""" Verifies if the configuration values passed via value_ids and custom_vals
are valid
Expand All @@ -466,6 +468,9 @@ def validate_configuration(self, value_ids, custom_vals=None, final=True):
common_vals = set(value_ids) & set(line.value_ids.ids)
custom_val = custom_vals.get(attr.id)
if line.required and not common_vals and not custom_val:
if do_raise:
raise ValidationError(_("No value provided for %s") %
line.attribute.name)
# TODO: Verify custom value type to be correct
return False

Expand All @@ -478,7 +483,17 @@ def validate_configuration(self, value_ids, custom_vals=None, final=True):
custom_attr_ids = self.attribute_line_ids.filtered(
'custom').mapped('attribute_id').ids

if not set(custom_vals.keys()) <= set(custom_attr_ids):
invalid_custom_vals = set(custom_vals.keys()) - set(custom_attr_ids)
if invalid_custom_vals:
if do_raise:
ProductAttribute = self.env['product.attribute']
attributes_names = ', '.join(attr.name
for attr in
ProductAttribute.browse(
list(invalid_custom_vals)))
raise ValidationError(
_('Attributes (%s) cannot hold custom values') %
attributes_names)
return False

# Check if there are multiple values passed for non-multi attributes
Expand Down
9 changes: 7 additions & 2 deletions product_configurator/models/product_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ def onchange_attribute(self):
string='Restrictions'
)

rule_description = fields.Char(
string='Rule Description',
help='User Displayed error if rule is broken'
)

sequence = fields.Integer(string='Sequence', default=10)

_order = 'product_tmpl_id, sequence, id'
Expand Down Expand Up @@ -229,7 +234,7 @@ def _check_value_ids(self):
if not valid:
raise ValidationError(
_("Values entered for line '%s' generate "
"a incompatible configuration" % cfg_img.name)
"an incompatible configuration" % cfg_img.name)
)


Expand Down Expand Up @@ -451,7 +456,7 @@ def write(self, vals):
for x in self.custom_value_ids
}
valid = self.product_tmpl_id.validate_configuration(
self.value_ids.ids, custom_val_dict, final=False)
self.value_ids.ids, custom_val_dict, final=False, do_raise=True)
if not valid:
raise ValidationError(_('Invalid Configuration'))
return res
Expand Down
5 changes: 5 additions & 0 deletions product_configurator/tests/test_configuration_rules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError


class ConfigurationRules(TransactionCase):
Expand Down Expand Up @@ -56,6 +57,10 @@ def test_invalid_configuration(self):
validation = self.cfg_tmpl.validate_configuration(attr_val_ids)
self.assertFalse(validation, "Incompatible values (Diesel Fuel -> "
"Gasoline Engine) configuration passed validation")
self.assertRaises(ValidationError,
self.cfg_tmpl.validate_configuration,
attr_val_ids, do_raise=True,
)

def test_missing_val_configuration(self):
conf = [
Expand Down
1 change: 1 addition & 0 deletions product_configurator/views/product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
options="{'no_create': True, 'no_create_edit': True}"
context="{'show_attribute': False}"/>
<field name="domain_id"/>
<field name="rule_description" />
</tree>
</field>
<separator colspan="4" string="Configuration Steps"/>
Expand Down

0 comments on commit 00785c1

Please sign in to comment.