Skip to content

Saleor Core: the high performance, composable, headless commerce API.

WIP

Pacu290b40c92f0d8
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 115d3499db..d023394fe1 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -300,7 +300,7 @@ def create_variant(product, **kwargs):
defaults.update(kwargs)
variant = ProductVariant(**defaults)
if variant.attributes:
- variant.name = get_name_from_attributes(variant, variant.attributes)
+ variant.name = get_name_from_attributes(variant)
variant.save()
create_stock(variant)
return variant
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 9a8d030aaa..ebe3722207 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -294,8 +294,7 @@ class ProductVariantForm(forms.ModelForm, AttributesMixin):
def save(self, commit=True):
attributes = self.get_saved_attributes()
self.instance.attributes = attributes
- self.instance.name = get_name_from_attributes(
- self.instance, attributes)
+ self.instance.name = get_name_from_attributes(self.instance)
return super().save(commit=commit)
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index a5853daa11..34c0ac52ec 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -357,22 +357,18 @@ def get_product_attributes_data(product):
"""Returns attributes associated with the product,
as dict of ProductAttribute: AttributeChoiceValue values.
"""
- product_attributes = list(product.product_type.product_attributes.all())
- values_map = get_attributes_display_map(product, product_attributes)
- return {
- attribute if attribute in product_attributes else None: value_obj
- for (attribute, value_obj) in values_map.items()}
+ attributes = product.product_type.product_attributes.all()
+ attributes_map = {attribute.pk: attribute for attribute in attributes}
+ values_map = get_attributes_display_map(product, attributes)
+ return {attributes_map.get(attr_pk): value_obj
+ for (attr_pk, value_obj) in values_map.items()}
def get_name_from_attributes(variant, attributes=None):
"""Generates ProductVariant's name based on its attributes."""
if attributes is None:
attributes = variant.product.product_type.variant_attributes.all()
-
- if isinstance(attributes, dict):
- values = get_attributes_display_map_from_dict(variant, attributes)
- else:
- values = get_attributes_display_map(variant, attributes)
+ values = get_attributes_display_map(variant, attributes)
return generate_name_from_values(values)
@@ -390,35 +386,13 @@ def get_attributes_display_map(obj, attributes):
choices = {smart_text(a.pk): a for a in attribute.values.all()}
choice_obj = choices.get(value)
if choice_obj:
- display_map[attribute] = choice_obj
+ display_map[attribute.pk] = choice_obj
else:
- display_map[attribute] = value
- return display_map
-
-
-def get_attributes_display_map_from_dict(variant, attributes):
- """Returns attributes associated with the product,
- as dict of ProductAttribute: AttributeChoiceValue values.
-
- Args:
- attributes: Dict of attribute_pk: attributechoicevalue_pk
- """
- display_map = {}
- for attribute_pk, attribute_choice_pk in attributes.items():
- attributes = variant.product.product_type.variant_attributes.all()
- attribute = next(
- attribute for attribute in attributes if
- attribute.pk == int(attribute_pk))
- if attribute:
- choices = attribute.values.all()
- choice_obj = next(
- choice for choice in choices
- if choice.pk == int(attribute_choice_pk))
- if choice_obj:
- display_map[attribute] = choice_obj
+ display_map[attribute.pk] = value
return display_map
-def generate_name_from_values(values):
+def generate_name_from_values(attributes_dict):
return ' / '.join(
- smart_text(value) for attribute, value in values.items())
+ smart_text(attribute_value)
+ for attribute_value in sorted(attributes_dict.values()))
diff --git a/tests/test_product.py b/tests/test_product.py
index 46a1096b4f..cd03ebbc3f 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -9,13 +9,14 @@ from saleor.cart import CartStatus, utils
from saleor.cart.models import Cart
from saleor.product import (
ProductAvailabilityStatus, VariantAvailabilityStatus, models)
-from saleor.product.models import Category, ProductImage
+from saleor.product.models import (
+ AttributeChoiceValue, Category, ProductAttribute, ProductImage)
from saleor.product.thumbnails import create_product_thumbnails
from saleor.product.utils import (
allocate_stock, deallocate_stock, decrease_stock,
- get_attributes_display_map, get_availability,
- get_product_availability_status, get_variant_availability_status,
- get_variant_picker_data, increase_stock)
+ generate_name_from_values, get_attributes_display_map, get_availability,
+ get_name_from_attributes, get_product_availability_status,
+ get_variant_availability_status, get_variant_picker_data, increase_stock)
from .utils import filter_products_by_attribute
@@ -314,7 +315,7 @@ def test_get_attributes_display_map(product_in_stock):
attr_value = product_attr.values.first()
assert len(attributes_display_map) == 1
- assert attributes_display_map == {product_attr: attr_value}
+ assert attributes_display_map == {product_attr.pk: attr_value}
def test_get_attributes_display_map_empty(product_with_no_attributes):
@@ -324,6 +325,16 @@ def test_get_attributes_display_map_empty(product_with_no_attributes):
assert get_attributes_display_map(product, attributes) == {}
+ 'values, expected_result', (
+ ({}, ''),
+ ({'color': 'blue'}, 'blue'),
+ ({'color': 'blue', 'size': 'large'}, 'blue / large')))
+def test_generate_name_from_values(values, expected_result):
+ name = generate_name_from_values(values)
+ assert name == expected_result
+
+
def test_product_availability_status(unavailable_product):
product = unavailable_product
product.product_type.has_variants = True