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

Commit

Permalink
[WIP] PR Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PCatinean committed Mar 1, 2017
1 parent 73d73ca commit 2d5047a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
22 changes: 16 additions & 6 deletions product_configurator/models/product.py
Expand Up @@ -276,19 +276,22 @@ def encode_custom_values(self, custom_values):
:returns: list of custom values compatible with write and create
"""
binary_attribute_ids = self.env['product.attribute'].search([
attr_obj = self.env['product.attribute']
binary_attribute_ids = attr_obj.search([
('custom_type', '=', 'binary')]).ids

custom_lines = []

for key, val in custom_values.iteritems():
custom_vals = {'attribute_id': key}
# TODO: Is this extra check neccesairy as we already make
# the check in validate_configuration?
attr_obj.browse(key).validate_custom_val(val)
if key in binary_attribute_ids:
custom_vals.update({
'attachment_ids': [(6, 0, val.ids)]
})
else:
self.env['product.attribute'].browse(key).validate_custom_val(val)
custom_vals.update({'value': val})
custom_lines.append((0, 0, custom_vals))
return custom_lines
Expand Down Expand Up @@ -318,7 +321,9 @@ def get_variant_vals(self, value_ids, custom_values=None, **kwargs):
}

if custom_values:
vals.update({'value_custom_ids': self.encode_custom_values(custom_values)})
vals.update({
'value_custom_ids': self.encode_custom_values(custom_values)
})

return vals

Expand Down Expand Up @@ -392,10 +397,15 @@ def validate_configuration(self, value_ids, custom_vals=None, final=True):
# Check if required values are missing for final configuration
if custom_vals is None:
custom_vals = {}
if final:
for line in self.attribute_line_ids:

for line in self.attribute_line_ids:
# Validate custom values
attr = line.attribute_id
if attr.id in custom_vals:
attr.validate_custom_val(custom_vals[attr.id])
if final:
common_vals = set(value_ids) & set(line.value_ids.ids)
custom_val = custom_vals.get(line.attribute_id.id)
custom_val = custom_vals.get(attr.id)
if line.required and not common_vals and not custom_val:
# TODO: Verify custom value type to be correct
return False
Expand Down
35 changes: 25 additions & 10 deletions product_configurator/models/product_attribute.py
Expand Up @@ -83,9 +83,12 @@ def onchange_custom_type(self):
'this attribute?'
)

uom_id = fields.Many2one('product.uom', string='Unit of Measure')
uom_id = fields.Many2one(
comodel_name='product.uom',
string='Unit of Measure'
)

image = fields.Binary('Image')
image = fields.Binary(string='Image')

# TODO prevent the same attribute from being defined twice on the
# attribute lines
Expand All @@ -104,16 +107,28 @@ def check_searchable_field(self):
def validate_custom_val(self, val):
""" Pass in a desired custom value and ensure it is valid.
Probaly should check type, etc, but let's assume fine for the moment.
"""
"""
self.ensure_one()
if self.custom_type in ('int', 'float'):
if self.min_val or self.max_val:
val = literal_eval(val)
if val < self.min_val or val > self.max_val:
raise ValidationError(
_("Selected custom value '%s' must be between %s and %s" %
(self.name, self.min_val, self.max_val))
)
minv = self.min_val
maxv = self.max_val
val = literal_eval(val)
if minv and maxv and (val < minv or val > maxv):
raise ValidationError(
_("Selected custom value '%s' must be between %s and %s"
% (self.name, self.min_val, self.max_val))
)
elif minv and val < minv:
raise ValidationError(
_("Selected custom value '%s' must be at least %s" %
(self.name, self.min_val))
)
elif maxv and val > maxv:
raise ValidationError(
_("Selected custom value '%s' must be lower than %s" %
(self.name, self.max_val + 1))
)


class ProductAttributeLine(models.Model):
_inherit = 'product.attribute.line'
Expand Down
7 changes: 2 additions & 5 deletions product_configurator_wizard/wizard/product_configurator.py
Expand Up @@ -653,14 +653,11 @@ def write(self, vals):
attr_val_dict.update({
attr_id: field_val
})
# Ensure there is no custom value stored if we have switched from custom
# to chosen value.
# Ensure there is no custom value stored if we have switched
# from custom value to selected attribute value.
if attr_line.custom:
custom_val_dict.update({attr_id: False})
elif attr_line.custom:
# For non-binary fields, a custom field value may have been entered which
# is 0 or some other way False, and this will not be in the dictionary of
# values, so need to assume that if not passed it is a "False" value desired.
val = vals.get(custom_field_name, False)
if attr_line.attribute_id.custom_type == 'binary':
# TODO: Add widget that enables multiple file uploads
Expand Down

0 comments on commit 2d5047a

Please sign in to comment.