Skip to content

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

Optimize products bulk create (#14019)

Tomasz Szymański86a196e83445
diff --git a/saleor/attribute/utils.py b/saleor/attribute/utils.py
index 6e1222ea3f..6a03bdf479 100644
--- a/saleor/attribute/utils.py
+++ b/saleor/attribute/utils.py
@@ -68,9 +68,12 @@ def validate_attribute_owns_values(attribute: Attribute, value_ids: Set[int]) ->
:raise: AssertionError
"""
- attribute_actual_value_ids = set(attribute.values.values_list("pk", flat=True))
- found_associated_ids = attribute_actual_value_ids & value_ids
- if found_associated_ids != value_ids:
+ attribute_actual_value_ids = set(
+ AttributeValue.objects.filter(
+ pk__in=value_ids, attribute=attribute
+ ).values_list("pk", flat=True)
+ )
+ if attribute_actual_value_ids != value_ids:
raise AssertionError("Some values are not from the provided attribute.")
diff --git a/saleor/core/tests/test_core.py b/saleor/core/tests/test_core.py
index cda31e609f..3600d22f55 100644
--- a/saleor/core/tests/test_core.py
+++ b/saleor/core/tests/test_core.py
@@ -18,7 +18,13 @@ from ...product import ProductTypeKind
from ...product.models import ProductType
from ...shipping.models import ShippingZone
from ..storages import S3MediaStorage
-from ..utils import build_absolute_uri, generate_unique_slug, get_client_ip, random_data
+from ..utils import (
+ build_absolute_uri,
+ generate_unique_slug,
+ get_client_ip,
+ prepare_unique_attribute_value_slug,
+ random_data,
+)
type_schema = {
"Vegetable": {
@@ -348,3 +354,28 @@ def test_cleardb_preserves_data(admin_user, app, site_settings, staff_user):
app.refresh_from_db()
site_settings.refresh_from_db()
staff_user.refresh_from_db()
+
+
+def test_prepare_unique_attribute_value_slug(color_attribute):
+ # given
+ value_1 = color_attribute.values.first()
+
+ # when
+ value_2 = AttributeValue(
+ name=value_1.name, attribute=color_attribute, slug=value_1.slug
+ )
+
+ # then
+ result = prepare_unique_attribute_value_slug(value_2.attribute, value_2.slug)
+
+ assert result == f"{value_1.slug}-2"
+
+
+def test_prepare_unique_attribute_value_slug_non_existing_slug(color_attribute):
+ # when
+ non_existing_slug = "non-existing-slug"
+
+ # then
+ result = prepare_unique_attribute_value_slug(color_attribute, non_existing_slug)
+
+ assert result == non_existing_slug
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 225130b226..911cec90e3 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -10,6 +10,9 @@ from django.utils.encoding import iri_to_uri
from django.utils.text import slugify
from text_unidecode import unidecode
+if TYPE_CHECKING:
+ from ...attribute.models import Attribute
+
task_logger = get_task_logger(__name__)
@@ -117,3 +120,10 @@ def prepare_unique_slug(slug: str, slug_values: Iterable):
unique_slug = f"{slug}-{extension}"
return unique_slug
+
+
+def prepare_unique_attribute_value_slug(attribute: "Attribute", slug: str):
+ value_slugs = attribute.values.filter(slug__startswith=slug).values_list(
+ "slug", flat=True
+ )
+ return prepare_unique_slug(slug, value_slugs)
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index 9d0fdbb367..f86663dd7c 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -17,7 +17,11 @@ from text_unidecode import unidecode
from ...attribute import AttributeEntityType, AttributeInputType, AttributeType
from ...attribute import models as attribute_models
from ...attribute.utils import associate_attribute_values_to_instance
-from ...core.utils import generate_unique_slug, prepare_unique_slug
+from ...core.utils import (
+ generate_unique_slug,
+ prepare_unique_attribute_value_slug,
+ prepare_unique_slug,
+)
from ...core.utils.editorjs import clean_editor_js
from ...core.utils.url import get_default_storage_root_url
from ...page import models as page_models
@@ -181,26 +185,23 @@ class AttributeAssignmentMixin:
)
@classmethod
- def _create_value_instance(
- cls, attribute, attr_value, external_ref, attr_and_value_slugs_map
- ):
+ def _create_value_instance(cls, attribute, attr_value, external_ref):
try:
- slug = prepare_unique_slug(
- slugify(unidecode(attr_value)), attr_and_value_slugs_map[attribute]
+ value_slug = prepare_unique_attribute_value_slug(
+ attribute, slugify(unidecode(attr_value))
)
value = attribute_models.AttributeValue.objects.create(
external_reference=external_ref,
attribute=attribute,
name=attr_value,
- slug=slug,
+ slug=value_slug,
)
except IntegrityError:
raise ValidationError(
"Attribute value with given externalReference already exists."
)
- attr_and_value_slugs_map[attribute].add(slug)
- return (value,)
+ return value
@classmethod
def clean_input(
@@ -396,13 +397,6 @@ class AttributeAssignmentMixin:
AttributeInputType.RICH_TEXT: cls._pre_save_rich_text_values,
}
clean_assignment = []
- attr_and_value_slugs_map = {}
-
- for attribute, _ in cleaned_input:
- if attribute not in attr_and_value_slugs_map:
- attr_and_value_slugs_map[attribute] = set(
- attribute.values.all().values_list("slug", flat=True)
- )
for attribute, attr_values in cleaned_input:
is_handled_by_values_field = (
@@ -418,9 +412,7 @@ class AttributeAssignmentMixin:
attribute_values = cls._pre_save_values(attribute, attr_values)
else:
pre_save_func = pre_save_methods_mapping[attribute.input_type]
- attribute_values = pre_save_func(
- instance, attribute, attr_values, attr_and_value_slugs_map
- )
+ attribute_values = pre_save_func(instance, attribute, attr_values)
associate_attribute_values_to_instance(
instance, attribute, *attribute_values
@@ -440,7 +432,6 @@ class AttributeAssignmentMixin:
_,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
if not attr_values.dropdown:
return tuple()
@@ -449,10 +440,8 @@ class AttributeAssignmentMixin:
external_ref = attr_values.dropdown.external_reference
if external_ref and attr_value:
- value = cls._create_value_instance(
- attribute, attr_value, external_ref, attr_and_value_slugs_map
- )
- return value
+ value = cls._create_value_instance(attribute, attr_value, external_ref)
+ return (value,)
if external_ref:
value = attribute_models.AttributeValue.objects.get(
@@ -472,8 +461,7 @@ class AttributeAssignmentMixin:
return (value,)
if attr_value:
- value = prepare_attribute_values(attribute, [attr_value])
- return value
+ return prepare_attribute_values(attribute, [attr_value])
return tuple()
@@ -483,7 +471,6 @@ class AttributeAssignmentMixin:
_,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
if not attr_values.swatch:
return tuple()
@@ -492,10 +479,8 @@ class AttributeAssignmentMixin:
external_ref = attr_values.swatch.external_reference
if external_ref and attr_value:
- value = cls._create_value_instance(
- attribute, attr_value, external_ref, attr_and_value_slugs_map
- )
- return value
+ value = cls._create_value_instance(attribute, attr_value, external_ref)
+ return (value,)
if external_ref:
value = attribute_models.AttributeValue.objects.get(
@@ -526,7 +511,6 @@ class AttributeAssignmentMixin:
_,
attribute: attribute_models.Attribute,
attr_values_input: AttrValuesInput,
- attr_and_value_slugs_map,
):
if not attr_values_input.multiselect:
return tuple()
@@ -537,9 +521,9 @@ class AttributeAssignmentMixin:
if external_ref and attr_value.value:
value = cls._create_value_instance(
- attribute, attr_value.value, external_ref, attr_and_value_slugs_map
+ attribute, attr_value.value, external_ref
)
- return value
+ return (value,)
if external_ref:
value = attribute_models.AttributeValue.objects.get(
@@ -578,7 +562,6 @@ class AttributeAssignmentMixin:
instance: T_INSTANCE,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
if attr_values.values:
value = attr_values.values[0]
@@ -614,7 +597,6 @@ class AttributeAssignmentMixin:
instance: T_INSTANCE,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
if not attr_values.rich_text:
return tuple()
@@ -632,7 +614,6 @@ class AttributeAssignmentMixin:
instance: T_INSTANCE,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
if not attr_values.plain_text:
return tuple()
@@ -648,7 +629,6 @@ class AttributeAssignmentMixin:
instance: T_INSTANCE,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
if attr_values.boolean is None:
return tuple()
@@ -670,7 +650,6 @@ class AttributeAssignmentMixin:
instance: T_INSTANCE,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
is_date_attr = attribute.input_type == AttributeInputType.DATE
tz = timezone.utc
@@ -716,7 +695,6 @@ class AttributeAssignmentMixin:
instance,
attribute: attribute_models.Attribute,
attr_values: AttrValuesInput,
- attr_and_value_slugs_map,
):
"""Lazy-retrieve or create the database objects from the supplied raw values.
@@ -751,7 +729,6 @@ class AttributeAssignmentMixin:
instance: T_INSTANCE,
attribute: attribute_models.Attribute,
attr_value: AttrValuesInput,
- attr_and_value_slugs_map,
):
"""Create database file attribute value object from the supplied value.