[fix] item attribute validation and tests
diff --git a/erpnext/controllers/item_variant.py b/erpnext/controllers/item_variant.py
index 3ff3d88..8294b64 100644
--- a/erpnext/controllers/item_variant.py
+++ b/erpnext/controllers/item_variant.py
@@ -24,22 +24,23 @@
if not args:
frappe.throw(_("Please specify at least one attribute in the Attributes table"))
- validate_item_variant_attributes(template, args)
-
return find_variant(template, args, variant)
def validate_item_variant_attributes(item, args=None):
+ if isinstance(item, basestring):
+ item = frappe.get_doc('Item', item)
+
if not args:
args = {d.attribute.lower():d.attribute_value for d in item.attributes}
- attribute_values = get_attribute_values()
-
- numeric_attributes = frappe._dict({d.attribute.lower(): d for d
- in item.attributes if d.numeric_values==1})
+ attribute_values, numeric_values = get_attribute_values()
for attribute, value in args.items():
- if attribute.lower() in numeric_attributes:
- numeric_attribute = numeric_attributes[attribute.lower()]
+ if not value:
+ continue
+
+ if attribute.lower() in numeric_values:
+ numeric_attribute = numeric_values[attribute.lower()]
from_range = numeric_attribute.from_range
to_range = numeric_attribute.to_range
@@ -57,22 +58,28 @@
is_incremental = remainder==0 or remainder==increment
if not (is_in_range and is_incremental):
- frappe.throw(_("Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3}")\
- .format(attribute, from_range, to_range, increment), InvalidItemAttributeValueError)
+ frappe.throw(_("Value for Attribute {0} must be within the range of {1} to {2} in the increments of {3} for Item {2}")\
+ .format(attribute, from_range, to_range, increment, item.name), InvalidItemAttributeValueError)
elif value not in attribute_values.get(attribute.lower(), []):
- frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values").format(
- value, attribute), InvalidItemAttributeValueError)
+ frappe.throw(_("Value {0} for Attribute {1} does not exist in the list of valid Item Attribute Values for Item {2}").format(
+ value, attribute, item.name), InvalidItemAttributeValueError)
def get_attribute_values():
if not frappe.flags.attribute_values:
attribute_values = {}
+ numeric_values = {}
for t in frappe.get_all("Item Attribute Value", fields=["parent", "attribute_value"]):
- (attribute_values.setdefault(t.parent.lower(), [])).append(t.attribute_value)
+ attribute_values.setdefault(t.parent.lower(), []).append(t.attribute_value)
+
+ for t in frappe.get_all('Item Attribute',
+ fields=["name", "from_range", "to_range", "increment"], filters={'numeric_values': 1}):
+ numeric_values[t.name.lower()] = t
frappe.flags.attribute_values = attribute_values
+ frappe.flags.numeric_values = numeric_values
- return frappe.flags.attribute_values
+ return frappe.flags.attribute_values, frappe.flags.numeric_values
def find_variant(template, args, variant_item_code=None):
conditions = ["""(iv_attribute.attribute="{0}" and iv_attribute.attribute_value="{1}")"""\
@@ -126,7 +133,7 @@
variant.set("attributes", variant_attributes)
copy_attributes_to_variant(template, variant)
- make_variant_item_code(template, variant)
+ make_variant_item_code(template.item_code, variant)
return variant
@@ -144,7 +151,7 @@
for d in variant.attributes:
variant.description += "<p>" + d.attribute + ": " + cstr(d.attribute_value) + "</p>"
-def make_variant_item_code(template, variant):
+def make_variant_item_code(template_item_code, variant):
"""Uses template's item code and abbreviations to make variant's item code"""
if variant.item_code:
return
@@ -160,8 +167,10 @@
}, as_dict=True)
if not item_attribute:
- # somehow an invalid item attribute got used
return
+ # frappe.throw(_('Invalid attribute {0} {1}').format(frappe.bold(attr.attribute),
+ # frappe.bold(attr.attribute_value)), title=_('Invalid Attribute'),
+ # exc=InvalidItemAttributeValueError)
if item_attribute[0].numeric_values:
# don't generate item code if one of the attributes is numeric
@@ -170,7 +179,7 @@
abbreviations.append(item_attribute[0].abbr)
if abbreviations:
- variant.item_code = "{0}-{1}".format(template.item_code, "-".join(abbreviations))
+ variant.item_code = "{0}-{1}".format(template_item_code, "-".join(abbreviations))
if variant.item_code:
variant.item_name = variant.item_code