Skip to content

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

WIP

Pacu263652a4faa4d
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index ebe3722207..76298ae0ce 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -127,19 +127,11 @@ class ProductTypeForm(forms.ModelForm):
if not self.instance.pk:
return data
- variants_changed = (
- self.fields['has_variants'].initial != has_variants)
- if variants_changed:
- query = self.instance.products.all()
- query = query.annotate(variants_counter=Count('variants'))
- query = query.filter(variants_counter__gt=1)
- if query.exists():
- msg = pgettext_lazy(
- 'Product type form error',
- 'Some products of this type have more than '
- 'one variant.')
- self.add_error('has_variants', msg)
+ self.check_if_variants_changed(has_variants)
+ self.update_variants_names(data)
+ return data
+ def update_variants_names(self, data):
# Some variant attributes could be removed so name should be updated
# accordingly
initial_attributes = set(list(self.instance.variant_attributes.all()))
@@ -152,7 +144,20 @@ class ProductTypeForm(forms.ModelForm):
for variant in variants_to_be_updated:
variant.name = get_name_from_attributes(variant)
variant.save()
- return data
+
+ def check_if_variants_changed(self, has_variants):
+ variants_changed = (
+ self.fields['has_variants'].initial != has_variants)
+ if variants_changed:
+ query = self.instance.products.all()
+ query = query.annotate(variants_counter=Count('variants'))
+ query = query.filter(variants_counter__gt=1)
+ if query.exists():
+ msg = pgettext_lazy(
+ 'Product type form error',
+ 'Some products of this type have more than '
+ 'one variant.')
+ self.add_error('has_variants', msg)
class AttributesMixin(object):
diff --git a/tests/test_product.py b/tests/test_product.py
index cd03ebbc3f..71800e29af 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -10,14 +10,14 @@ from saleor.cart.models import Cart
from saleor.product import (
ProductAvailabilityStatus, VariantAvailabilityStatus, models)
from saleor.product.models import (
- AttributeChoiceValue, Category, ProductAttribute, ProductImage)
+ AttributeChoiceValue, Category, ProductImage)
from saleor.product.thumbnails import create_product_thumbnails
from saleor.product.utils import (
allocate_stock, deallocate_stock, decrease_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
@@ -325,14 +325,22 @@ 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):
+def test_get_name_from_attributes():
+ raise NotImplementedError
+
+
+def test_generate_name_from_values():
+ red = AttributeChoiceValue.objects.create(name='Red', slug='red')
+ blue = AttributeChoiceValue.objects.create(name='Blue', slug='blue')
+ yellow = AttributeChoiceValue.objects.create(name='Yellow', slug='yellow')
+ values = {'3': red, '2': blue, '1': yellow}
name = generate_name_from_values(values)
- assert name == expected_result
+ assert name == 'Yellow / Blue / Red'
+
+
+def test_generate_name_from_values_empty():
+ name = generate_name_from_values({})
+ assert name == ''
def test_product_availability_status(unavailable_product):