This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
[FIX] Wizard Attributes #104
Open
richard-willdooit
wants to merge
6
commits into
pledra:10.0
Choose a base branch
from
richard-willdooit:10.0-wizard-attributes-control
base: 10.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from . import test_wizard | ||
from . import test_wizard_attrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from odoo.addons.product_configurator_wizard.tests.test_wizard \ | ||
import ConfigurationRules | ||
|
||
|
||
class ConfigurationAttributes(ConfigurationRules): | ||
|
||
def setUp(self): | ||
""" | ||
Product with 3 sizes: | ||
Small or Medium allow Blue or Red, colour is required | ||
Large does not allow colour selection | ||
""" | ||
super(ConfigurationAttributes, self).setUp() | ||
|
||
self.attr_size = self.env['product.attribute'].create( | ||
{'name': 'Size'}) | ||
self.attr_val_small = self.env['product.attribute.value'].create( | ||
{'attribute_id': self.attr_size.id, | ||
'name': 'Small', | ||
} | ||
) | ||
self.attr_val_med = self.env['product.attribute.value'].create( | ||
{'attribute_id': self.attr_size.id, | ||
'name': 'Medium', | ||
} | ||
) | ||
self.attr_val_large = self.env['product.attribute.value'].create( | ||
{'attribute_id': self.attr_size.id, | ||
'name': 'Large (Green)', | ||
} | ||
) | ||
domain_small_med = self.env['product.config.domain'].create( | ||
{'name': 'Small/Med', | ||
'domain_line_ids': [ | ||
(0, 0, {'attribute_id': self.attr_size.id, | ||
'condition': 'in', | ||
'operator': 'and', | ||
'value_ids': | ||
[(6, 0, | ||
[self.attr_val_small.id, self.attr_val_med.id] | ||
)] | ||
}), | ||
], | ||
} | ||
) | ||
self.attr_colour = self.env['product.attribute'].create( | ||
{'name': 'Colour'}) | ||
self.attr_val_blue = self.env['product.attribute.value'].create( | ||
{'attribute_id': self.attr_colour.id, | ||
'name': 'Blue', | ||
} | ||
) | ||
self.attr_val_red = self.env['product.attribute.value'].create( | ||
{'attribute_id': self.attr_colour.id, | ||
'name': 'Red', | ||
} | ||
) | ||
self.product_temp = self.env['product.template'].create( | ||
{'name': 'Config Product', | ||
'config_ok': True, | ||
'type': 'product', | ||
'categ_id': self.env['ir.model.data'].xmlid_to_res_id( | ||
'product.product_category_5' | ||
), | ||
'attribute_line_ids': [ | ||
(0, 0, {'attribute_id': self.attr_size.id, | ||
'value_ids': [ | ||
(6, 0, self.attr_size.value_ids.ids), | ||
], | ||
'required': True, | ||
}), | ||
(0, 0, {'attribute_id': self.attr_colour.id, | ||
'value_ids': [ | ||
(6, 0, self.attr_colour.value_ids.ids), | ||
], | ||
'required': False, | ||
}) | ||
], | ||
} | ||
) | ||
self.template_colour_line = \ | ||
self.product_temp.attribute_line_ids.filtered( | ||
lambda a: a.attribute_id == self.attr_colour) | ||
self.env['product.config.line'].create({ | ||
'product_tmpl_id': self.product_temp.id, | ||
'attribute_line_id': self.template_colour_line.id, | ||
'value_ids': [(6, 0, self.attr_colour.value_ids.ids)], | ||
'domain_id': domain_small_med.id, | ||
}) | ||
|
||
def test_configurations_option_or_not_reqd(self): | ||
# Start a new configuration wizard | ||
wizard_obj = self.env['product.configurator'].with_context({ | ||
'active_model': 'sale.order', | ||
'active_id': self.so.id | ||
# 'default_order_id': self.so.id | ||
}) | ||
|
||
wizard = wizard_obj.create({'product_tmpl_id': self.product_temp.id}) | ||
wizard.action_next_step() | ||
|
||
dynamic_fields = {} | ||
for attribute_line in self.product_temp.attribute_line_ids: | ||
field_name = '%s%s' % ( | ||
wizard.field_prefix, | ||
attribute_line.attribute_id.id | ||
) | ||
dynamic_fields[field_name] = [] if attribute_line.multi else False | ||
field_name_colour = '%s%s' % ( | ||
wizard.field_prefix, | ||
self.attr_colour.id | ||
) | ||
ro_name_colour = '%s%s' % ( | ||
wizard.ro_field_prefix, | ||
self.attr_colour.id | ||
) | ||
reqd_name_colour = '%s%s' % ( | ||
wizard.reqd_field_prefix, | ||
self.attr_colour.id | ||
) | ||
|
||
# Define small without colour specified | ||
self.wizard_write_proceed(wizard, [self.attr_val_small]) | ||
new_variant = self.product_temp.product_variant_ids | ||
self.assertTrue(len(new_variant) == 1 and | ||
set(new_variant.attribute_value_ids.ids) == | ||
set([self.attr_val_small.id]), | ||
"Wizard did not accurately create a variant with " | ||
"optional value undefined") | ||
config_variants = self.product_temp.product_variant_ids | ||
|
||
order_line = self.so.order_line.filtered( | ||
lambda l: l.product_id.config_ok | ||
) | ||
|
||
# Redefine to medium without colour | ||
self.do_reconfigure(order_line, [self.attr_val_med]) | ||
new_variant = self.product_temp.product_variant_ids - config_variants | ||
self.assertTrue(len(new_variant) == 1 and | ||
set(new_variant.attribute_value_ids.ids) == | ||
set([self.attr_val_med.id]), | ||
"Wizard did not accurately reconfigure a variant with " | ||
"optional value undefined") | ||
config_variants = self.product_temp.product_variant_ids | ||
|
||
# Redefine to medium blue | ||
self.do_reconfigure(order_line, [self.attr_val_blue]) | ||
new_variant = self.product_temp.product_variant_ids - config_variants | ||
self.assertTrue(len(new_variant) == 1 and | ||
set(new_variant.attribute_value_ids.ids) == | ||
set([self.attr_val_med.id, self.attr_val_blue.id]), | ||
"Wizard did not accurately reconfigure a variant with " | ||
"to add an optional value") | ||
config_variants = self.product_temp.product_variant_ids | ||
|
||
# Redefine to large - should remove colour, as this is invalid | ||
reconfig_action = order_line.reconfigure_product() | ||
wizard = self.env['product.configurator'].browse( | ||
reconfig_action.get('res_id') | ||
) | ||
attr_large_dict = self.get_wizard_write_dict(wizard, | ||
[self.attr_val_large]) | ||
attr_blue_dict = self.get_wizard_write_dict(wizard, | ||
[self.attr_val_blue]) | ||
oc_vals = dynamic_fields.copy() | ||
oc_vals.update({'id': wizard.id}) | ||
oc_vals.update(dict(attr_blue_dict, **attr_large_dict)) | ||
oc_result = wizard.onchange( | ||
oc_vals, | ||
attr_large_dict.keys()[0], | ||
{} | ||
) | ||
self.assertTrue(field_name_colour in oc_result['value'] and | ||
not oc_result['value'][field_name_colour], | ||
"Colour should have been cleared by wizard" | ||
) | ||
self.assertTrue(ro_name_colour in oc_result['value'] and | ||
oc_result['value'][ro_name_colour], | ||
"Colour should have become readonly" | ||
) | ||
self.assertFalse(oc_result['value'].get(reqd_name_colour, False), | ||
"Colour should not have become required" | ||
) | ||
|
||
vals = self.get_wizard_write_dict(wizard, [self.attr_val_large], | ||
remove_values=[self.attr_val_blue]) | ||
wizard.write(vals) | ||
wizard.action_next_step() | ||
if wizard.exists(): | ||
while wizard.action_next_step(): | ||
pass | ||
new_variant = self.product_temp.product_variant_ids - config_variants | ||
self.assertTrue(len(new_variant) == 1 and | ||
set(new_variant.attribute_value_ids.ids) == | ||
set([self.attr_val_large.id]), | ||
"Wizard did not accurately reconfigure a variant " | ||
"to remove invalid attribute") | ||
|
||
# Now test reqd attribute changes. | ||
last_variant = new_variant | ||
self.template_colour_line.write({'required': True}) | ||
|
||
# Redefine to medium - should make colour required | ||
reconfig_action = order_line.reconfigure_product() | ||
wizard = self.env['product.configurator'].browse( | ||
reconfig_action.get('res_id') | ||
) | ||
attr_med_dict = self.get_wizard_write_dict(wizard, | ||
[self.attr_val_med]) | ||
oc_vals = dynamic_fields.copy() | ||
oc_vals.update({'id': wizard.id}) | ||
oc_vals.update(attr_med_dict) | ||
oc_result = wizard.onchange( | ||
oc_vals, | ||
attr_med_dict.keys()[0], | ||
{} | ||
) | ||
self.assertTrue(oc_result['value'].get(reqd_name_colour), | ||
"Colour should have become required" | ||
) | ||
# Redefine to large - should make colour not required, despite master | ||
# file | ||
vals = self.get_wizard_write_dict(wizard, [self.attr_val_large]) | ||
wizard.write(vals) | ||
wizard.action_next_step() | ||
if wizard.exists(): | ||
while wizard.action_next_step(): | ||
pass | ||
self.assertTrue(order_line.product_id == last_variant, | ||
"Wizard did not end up with the same " | ||
"variant") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree that there might be a case where a user might want "required if options available" rather than flat required.
On one hand you can have this flexibility on the other hand you are not guaranteed to have a required value and if you mess up your configuration restrictions the configuration will silently pass.
Maybe there's a way we can differentiate between two cases or also provide an error message when a line is required and has certain configurations which do not provide any options for the required line.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PCatinean My initial feeling was similar, and I was going to make a 2 way flag. But I fell back to this - if there are no possible selections based on other domains, then it just seemed logical that it can't be invalid - the definition must be...
If you want it changed to two types of required, I think we should do it as another PR after this one is merged.