Skip to content

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

WIP

Pacu2cf523156006a
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 543ed51b48..05d87fe95d 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -2,7 +2,6 @@ import itertools
import os
import random
import unicodedata
-from collections import defaultdict
from django.conf import settings
from django.contrib.auth.models import Group, Permission
@@ -26,6 +25,7 @@ from ...product.models import (
AttributeChoiceValue, Category, Collection, Product, ProductAttribute,
ProductImage, ProductType, ProductVariant, Stock, StockLocation)
from ...product.thumbnails import create_product_thumbnails
+from ...product.utils import generate_name_from_variant_attributes
from ...shipping.models import ANY_COUNTRY, ShippingMethod
fake = Factory.create()
@@ -298,7 +298,11 @@ def create_variant(product, **kwargs):
defaults = {
'product': product}
defaults.update(kwargs)
- variant = ProductVariant.objects.create(**defaults)
+ variant = ProductVariant(**defaults)
+ if variant.attributes:
+ variant.name = generate_name_from_variant_attributes(
+ variant, variant.attributes)
+ variant.save()
create_stock(variant)
return variant
diff --git a/saleor/core/utils/text.py b/saleor/core/utils/text.py
index 0330a81c89..977be625bd 100644
--- a/saleor/core/utils/text.py
+++ b/saleor/core/utils/text.py
@@ -1,7 +1,6 @@
# coding: utf-8
import bleach
-from django.forms import fields
from django.template.defaultfilters import truncatechars
from html5lib.serializer import HTMLSerializer
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index c7614aa7be..8b5b9a5029 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -15,7 +15,8 @@ from ...product.models import (
ProductImage, ProductType, ProductVariant, Stock, StockLocation,
VariantImage)
from ...product.thumbnails import create_product_thumbnails
-from ...product.utils import display_variant_attributes
+from ...product.utils import (
+ display_variant_attributes, generate_name_from_variant_attributes)
from ..forms import ModelChoiceOrCreationField, OrderedModelMultipleChoiceField
from ..seo.fields import SeoDescriptionField, SeoTitleField
from ..seo.utils import prepare_seo_description
@@ -137,6 +138,19 @@ class ProductTypeForm(forms.ModelForm):
'Some products of this type have more than '
'one variant.')
self.add_error('has_variants', msg)
+
+ # Some variant attributes could be removed so name should be updated
+ # accordingly
+ initial_attributes = set(list(self.instance.variant_attributes.all()))
+ saved_attributes = set(list(data.get('variant_attributes')))
+ attributes_changed = initial_attributes != saved_attributes
+ if attributes_changed:
+ variants_to_be_updated = ProductVariant.objects.filter(
+ product__in=self.instance.products.all()).prefetch_related(
+ 'product__product_type__variant_attributes__values').all()
+ for variant in variants_to_be_updated:
+ variant.name = display_variant_attributes(variant)
+ variant.save()
return data
@@ -275,7 +289,7 @@ class ProductVariantForm(forms.ModelForm, AttributesMixin):
def save(self, commit=True):
attributes = self.get_saved_attributes()
self.instance.attributes = attributes
- self.instance.name = display_variant_attributes(
+ self.instance.name = generate_name_from_variant_attributes(
self.instance, attributes)
return super().save(commit=commit)
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 760906712b..6e7c8a7729 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -179,7 +179,7 @@ class Product(SeoModel):
class ProductVariant(models.Model):
sku = models.CharField(max_length=32, unique=True)
- name = models.CharField(max_length=100, blank=True)
+ name = models.CharField(max_length=255, blank=True)
price_override = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
blank=True, null=True)
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index d88673fd3c..fedd7bbb78 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -203,13 +203,9 @@ def get_variant_url_from_product(product, attributes):
def get_variant_url(variant):
- attributes = {}
- values = {}
- for attribute in variant.product.product_type.variant_attributes.all():
- attributes[str(attribute.pk)] = attribute
- for value in attribute.values.all():
- values[str(value.pk)] = value
-
+ attributes = {
+ str(attribute.pk): attribute
+ for attribute in variant.product.product_type.variant_attributes.all()}
return get_variant_url_from_product(variant.product, attributes)
@@ -386,7 +382,33 @@ def display_variant_attributes(variant, attributes=None):
if not values:
return ''
return ', '.join(
- [
- '%s: %s' % (
- smart_text(attributes.get(id=int(key))), smart_text(value))
- for (key, value) in values.items()])
+ '%s: %s' % (
+ smart_text(
+ next(attr for attr in attributes if attr.pk == key)),
+ smart_text(value))
+ for (key, value) in values.items())
+
+
+def get_attributes_from_dict(variant, attributes):
+ 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
+ return display_map
+
+
+def generate_name_from_variant_attributes(variant, attributes):
+ values = get_attributes_from_dict(variant, attributes)
+ if not values:
+ return ''
+ return ' / '.join(
+ smart_text(attribute) for attribute, value in values.items())