Skip to content

Commit

Permalink
Merge pull request OCA#28 from PCatinean/10.0
Browse files Browse the repository at this point in the history
[FIX] Custom value and PEP8 fixes
  • Loading branch information
PCatinean authored and simahawk committed Oct 23, 2024
1 parent 3f75dee commit f494e92
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
62 changes: 40 additions & 22 deletions product_configurator/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ def get_config_image_obj(self, value_ids, size=None):
max_matches = matches
return img_obj

@api.multi
def encode_custom_values(self, custom_values):
""" Hook to alter the values of the custom values before creating or writing
:param custom_values: dict {product.attribute.id: custom_value}
:returns: list of custom values compatible with write and create
"""
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:
custom_vals.update({'value': val})
custom_lines.append((0, 0, custom_vals))
return custom_lines

@api.multi
def get_variant_vals(self, value_ids, custom_values=None, **kwargs):
""" Hook to alter the values of the product variant before creation
Expand All @@ -296,25 +324,10 @@ def get_variant_vals(self, value_ids, custom_values=None, **kwargs):
'image_small': all_images['image_medium'],
}

binary_attribute_ids = self.env['product.attribute'].search([
('custom_type', '=', 'binary')]).ids

if not custom_values:
return vals

custom_lines = []

for key, val in custom_values.iteritems():
custom_vals = {'attribute_id': key}
if key in binary_attribute_ids:
custom_vals.update({
'attachment_ids': [(6, 0, val.ids)]
})
else:
custom_vals.update({'value': val})
custom_lines.append((0, 0, custom_vals))
vals.update({'value_custom_ids': custom_lines})

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

@api.multi
Expand Down Expand Up @@ -387,10 +400,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
39 changes: 31 additions & 8 deletions product_configurator/models/product_attribute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

from ast import literal_eval

from openerp import models, fields, api, _
from openerp.exceptions import ValidationError

Expand Down Expand Up @@ -82,16 +84,12 @@ def onchange_custom_type(self):
'this attribute?'
)

sequence = fields.Integer(
string='Sequence',
default=10,
help='Gives the sequence of this line when '
'displaying the attributes'
uom_id = fields.Many2one(
comodel_name='product.uom',
string='Unit of Measure'
)

uom_id = fields.Many2one('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 @@ -107,6 +105,31 @@ def check_searchable_field(self):
self.custom_type)
)

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'):
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

0 comments on commit f494e92

Please sign in to comment.