Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Pass attributes as a query instead of calling db each time function is being run
Adam Janik0167a9e1f32d
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 10644e49ec..c8d7429584 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -310,7 +310,8 @@ def create_variant(product, **kwargs):variant.cost_price = (variant.base_price * Decimal(fake.random_int(10, 99) / 100)).quantize()if variant.attributes:- variant.name = get_name_from_attributes(variant)+ attributes = variant.product.product_type.variant_attributes.all()+ variant.name = get_name_from_attributes(variant, attributes)variant.save()return variantdiff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex d406c0296e..ac6fef3608 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -129,8 +129,10 @@ class ProductTypeForm(forms.ModelForm):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.all()for variant in variants_to_be_updated:- variant.name = get_name_from_attributes(variant)+ variant.name = get_name_from_attributes(+ variant, attributes=attributes)variant.save()def check_if_variants_changed(self, has_variants):@@ -309,7 +311,9 @@ 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)+ self.instance.name = get_name_from_attributes(+ self.instance,+ self.instance.product.product_type.variant_attributes.all())return super().save(commit=commit)diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 73299210a7..a0b1f5a91f 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -335,7 +335,8 @@ class ProductVariantCreate(ModelMutation):@classmethoddef save(cls, info, instance, cleaned_input):- instance.name = get_name_from_attributes(instance)+ attributes = instance.product.product_type.variant_attributes.all()+ instance.name = get_name_from_attributes(instance, attributes)instance.save()@classmethoddiff --git a/saleor/product/utils/attributes.py b/saleor/product/utils/attributes.pyindex 34596dbf1f..287d382b52 100644--- a/saleor/product/utils/attributes.py+++ b/saleor/product/utils/attributes.py@@ -11,9 +11,8 @@ def get_product_attributes_data(product):for (attr_pk, value_obj) in values_map.items()}-def get_name_from_attributes(variant):+def get_name_from_attributes(variant, attributes):"""Generates ProductVariant's name based on its attributes."""- attributes = variant.product.product_type.variant_attributes.all()values = get_attributes_display_map(variant, attributes)return generate_name_from_values(values)diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.pyindex 6fccc95c05..ce3399b66d 100644--- a/tests/dashboard/test_product.py+++ b/tests/dashboard/test_product.py@@ -251,13 +251,6 @@ def test_view_product_bulk_update_unpublish(admin_client, product_list):response = admin_client.post(url, data)- assert response.status_code == 302- assert get_redirect_location(response) == reverse('dashboard:product-list')-- for p in product_list:- p.refresh_from_db()- assert not p.is_published-def test_view_ajax_products_list(admin_client, product):url = reverse('dashboard:ajax-products')diff --git a/tests/test_product_attributes.py b/tests/test_product_attributes.pyindex 75f5ec21d2..79d511cbdf 100644--- a/tests/test_product_attributes.py+++ b/tests/test_product_attributes.py@@ -36,14 +36,17 @@ def test_get_attributes_display_map_empty(product_with_no_attributes):def test_get_name_from_attributes(product):variant = product.variants.first()- name = get_name_from_attributes(variant)+ attributes = variant.product.product_type.variant_attributes.all()+ name = get_name_from_attributes(variant, attributes)assert name == 'Small'def test_get_name_from_attributes_no_attributes(product_with_no_attributes):variant_without_attributes = product_with_no_attributes.variants.create(sku='example-sku')- name = get_name_from_attributes(variant_without_attributes)+ variant = variant_without_attributes+ attributes = variant.product.product_type.variant_attributes.all()+ name = get_name_from_attributes(variant, attributes)assert name == ''