Skip to content

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

Optimize updating variant name

fowczarek92ff7b00eb05
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 38cd08b5fc..db02ff9af0 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -17,6 +17,7 @@ from ...core.weight import WeightField
from ...product.models import (
Attribute, AttributeValue, Category, Collection, Product, ProductImage,
ProductType, ProductVariant, VariantImage)
+from ...product.tasks import update_variants_names
from ...product.thumbnails import create_product_thumbnails
from ...product.utils.attributes import get_name_from_attributes
from ..forms import ModelChoiceOrCreationField, OrderedModelMultipleChoiceField
@@ -146,27 +147,9 @@ class ProductTypeForm(forms.ModelForm):
return data
self.check_if_variants_changed(has_variants)
- self.update_variants_names(saved_attributes=variant_attr)
+ update_variants_names.delay(self.instance, saved_attributes=variant_attr)
return data
- def update_variants_names(self, saved_attributes):
- # Some variant attributes could be removed so name should be updated
- # accordingly
- initial_attributes = set(self.instance.variant_attributes.all())
- attributes_changed = initial_attributes.intersection(saved_attributes)
- if not attributes_changed:
- return
- variants_to_be_updated = ProductVariant.objects.filter(
- product__in=self.instance.products.all(),
- product__product_type__variant_attributes__in=attributes_changed)
- variants_to_be_updated = variants_to_be_updated.prefetch_related(
- 'product__product_type__variant_attributes__values').all()
- attributes = self.instance.variant_attributes.prefetch_related(
- 'translations', 'values__translations')
- for variant in variants_to_be_updated:
- variant.name = get_name_from_attributes(variant, attributes)
- variant.save(update_fields=['name'])
-
def check_if_variants_changed(self, has_variants):
variants_changed = (
self.fields['has_variants'].initial != has_variants)
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index d9d200bdb0..055d7580ce 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -6,6 +6,7 @@ from graphene.types import InputObjectType
from graphql_jwt.decorators import permission_required
from ....product import models
+from ....product.tasks import update_variants_names
from ....product.utils.attributes import get_name_from_attributes
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ...core.types.common import Decimal, SeoInput
@@ -14,7 +15,7 @@ from ...core.utils import clean_seo_fields
from ...file_upload.types import Upload
from ...shipping.types import WeightScalar
from ..types import Category, Collection, Product, ProductImage, ProductVariant
-from ..utils import attributes_to_hstore, update_variants_names
+from ..utils import attributes_to_hstore
class CategoryInput(graphene.InputObjectType):
@@ -494,7 +495,7 @@ class ProductTypeUpdate(ProductTypeCreate):
variant_attr = cleaned_input.get('variant_attributes')
if variant_attr:
variant_attr = set(variant_attr)
- update_variants_names(instance, variant_attr)
+ update_variants_names.delay(instance, variant_attr)
super().save(info, instance, cleaned_input)
diff --git a/saleor/graphql/product/utils.py b/saleor/graphql/product/utils.py
index 8d196734f3..4e0442ef56 100644
--- a/saleor/graphql/product/utils.py
+++ b/saleor/graphql/product/utils.py
@@ -1,7 +1,6 @@
from django.utils.text import slugify
-from ...product.models import Attribute, AttributeValue, ProductVariant
-from ...product.utils.attributes import get_name_from_attributes
+from ...product.models import Attribute, AttributeValue
def attributes_to_hstore(attribute_value_input, attributes_queryset):
@@ -42,19 +41,3 @@ def attributes_to_hstore(attribute_value_input, attributes_queryset):
attributes_hstore[str(attribute_id)] = str(value_id)
return attributes_hstore
-
-
-def update_variants_names(instance, saved_attributes):
- initial_attributes = set(instance.variant_attributes.all())
- attributes_changed = initial_attributes.intersection(saved_attributes)
- if not attributes_changed:
- return
- variants_to_be_updated = ProductVariant.objects.filter(
- product__in=instance.products.all(),
- product__product_type__variant_attributes__in=attributes_changed)
- variants_to_be_updated = variants_to_be_updated.prefetch_related(
- 'product__product_type__variant_attributes__values').all()
- attributes = instance.variant_attributes.all()
- for variant in variants_to_be_updated:
- variant.name = get_name_from_attributes(variant, attributes)
- variant.save()
diff --git a/saleor/product/tasks.py b/saleor/product/tasks.py
new file mode 100644
index 0000000000..8acbaaeb24
--- /dev/null
+++ b/saleor/product/tasks.py
@@ -0,0 +1,25 @@
+from celery import shared_task
+
+from .models import ProductVariant
+from .utils.attributes import get_name_from_attributes
+
+
+def _update_variants_names(instance, saved_attributes):
+ initial_attributes = set(instance.variant_attributes.all())
+ attributes_changed = initial_attributes.intersection(saved_attributes)
+ if not attributes_changed:
+ return
+ variants_to_be_updated = ProductVariant.objects.filter(
+ product__in=instance.products.all(),
+ product__product_type__variant_attributes__in=attributes_changed)
+ variants_to_be_updated = variants_to_be_updated.prefetch_related(
+ 'product__product_type__variant_attributes__values').all()
+ attributes = instance.variant_attributes.all()
+ for variant in variants_to_be_updated:
+ variant.name = get_name_from_attributes(variant, attributes)
+ variant.save(update_fields=['name'])
+
+
+@shared_task
+def update_variants_names(instance, saved_attributes):
+ return _update_variants_names(instance, saved_attributes)
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 8122642234..2bf82e77d0 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1,4 +1,4 @@
-from unittest.mock import MagicMock, Mock
+from unittest.mock import patch
import graphene
import pytest
@@ -10,7 +10,6 @@ from tests.utils import create_image, create_pdf_file_with_image_ext
from saleor.graphql.core.types import ReportingPeriod
from saleor.graphql.product.types import StockAvailability
-from saleor.graphql.product.utils import update_variants_names
from saleor.product.models import (
Category, Collection, Product, ProductImage, ProductType, ProductVariant)
@@ -1240,9 +1239,10 @@ def test_unassign_not_assigned_variant_image(
assert content['data']['variantImageUnassign']['errors'][0]['field'] == (
'imageId')
-
+@patch('saleor.product.tasks.update_variants_names.delay')
def test_product_type_update_changes_variant_name(
- staff_api_client, product_type, product, permission_manage_products):
+ mock_update_variants_names, staff_api_client, product_type,
+ product, permission_manage_products):
query = """
mutation updateProductType(
$id: ID!,
@@ -1282,18 +1282,10 @@ def test_product_type_update_changes_variant_name(
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_products])
content = get_graphql_content(response)
- product.refresh_from_db()
- variant = product.variants.first()
- attribute = product.product_type.variant_attributes.first()
- value = attribute.values.first().name
- assert variant.name == value
-
+ variant_attributes = set(variant_attributes)
+ mock_update_variants_names.assert_called_once_with(
+ product_type, variant_attributes)
-def test_update_variants_changed_does_nothing_with_no_attributes():
- product_type = MagicMock(spec=ProductType)
- product_type.variant_attributes.all = Mock(return_value=[])
- saved_attributes = []
- assert update_variants_names(product_type, saved_attributes) is None
def test_product_variants_by_ids(user_api_client, variant):
diff --git a/tests/test_product_attributes.py b/tests/test_product_attributes.py
index 4345093b0f..1d2ad85661 100644
--- a/tests/test_product_attributes.py
+++ b/tests/test_product_attributes.py
@@ -1,7 +1,10 @@
+from unittest.mock import MagicMock, Mock
+
import pytest
from saleor.product.models import (
- AttributeValue, Product, Attribute)
+ Attribute, AttributeValue, Product, ProductType)
+from saleor.product.tasks import _update_variants_names
from saleor.product.utils.attributes import (
generate_name_from_values, get_attributes_display_map,
get_name_from_attributes)
@@ -67,3 +70,18 @@ def test_generate_name_from_values():
def test_generate_name_from_values_empty():
name = generate_name_from_values({})
assert name == ''
+
+
+def test_product_type_update_changes_variant_name(product_type):
+ variant = product_type.variant_attributes.first()
+ variant.name = "test_name"
+ variant.save()
+ _update_variants_names(product_type, set([variant]))
+ assert product_type.variant_attributes.first().name == "test_name"
+
+
+def test_update_variants_changed_does_nothing_with_no_attributes():
+ product_type = MagicMock(spec=ProductType)
+ product_type.variant_attributes.all = Mock(return_value=[])
+ saved_attributes = []
+ assert _update_variants_names(product_type, saved_attributes) is None