Skip to content

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

Add support for multi values and attribute visibility in storefront 1.0

NyanKiyoshi7572e2f46f7a
diff --git a/.isort.cfg b/.isort.cfg
index 9dee5c09e8..20d12ef055 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,bleach,bootstrap4,braintree,captcha,celery,dj_database_url,dj_email_url,django,django_babel,django_cache_url,django_countries,django_elasticsearch_dsl,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,elasticsearch_dsl,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,impersonate,markdown,measurement,mptt,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,social_core,storages,stripe,templated_email,text_unidecode,versatileimagefield
+known_third_party = PIL,babel,bleach,bootstrap4,braintree,bs4,captcha,celery,dj_database_url,dj_email_url,django,django_babel,django_cache_url,django_countries,django_elasticsearch_dsl,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,elasticsearch_dsl,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_django_optimizer,graphql,graphql_jwt,graphql_relay,html5lib,html_to_draftjs,i18naddress,impersonate,markdown,measurement,mptt,phonenumber_field,phonenumbers,prices,promise,pytest,razorpay,requests,sentry_sdk,six,social_core,storages,stripe,templated_email,text_unidecode,versatileimagefield
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index 05c3ffc77f..9624eb90f6 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -49,14 +49,14 @@ class ProductFilter(SortedFilterSet):
q_product_attributes = self._get_product_attributes_lookup()
q_variant_attributes = self._get_variant_attributes_lookup()
product_attributes = (
- Attribute.objects.all()
- .prefetch_related("translations", "values__translations")
+ Attribute.objects.prefetch_related("translations", "values__translations")
+ .exclude(filterable_in_storefront=False)
.filter(q_product_attributes)
.distinct()
)
variant_attributes = (
- Attribute.objects.all()
- .prefetch_related("translations", "values__translations")
+ Attribute.objects.prefetch_related("translations", "values__translations")
+ .exclude(filterable_in_storefront=False)
.filter(q_variant_attributes)
.distinct()
)
diff --git a/saleor/product/utils/attributes.py b/saleor/product/utils/attributes.py
index d6ffc6eeb8..eca04b5284 100644
--- a/saleor/product/utils/attributes.py
+++ b/saleor/product/utils/attributes.py
@@ -7,13 +7,20 @@ def get_product_attributes_data(product):
It is returned as a dict of Attribute: AttributeValue values.
"""
- attributes = product.product_type.product_attributes.all()
+ attributes = product.product_type.product_attributes.exclude(
+ visible_in_storefront=False
+ )
attributes_map = {attribute.pk: attribute.translated for attribute in attributes}
values_map = get_attributes_display_map(product, attributes)
- return {
- attributes_map[attr_pk]: value_obj.translated
- for (attr_pk, value_obj) in values_map.items()
- }
+ product_attributes = {}
+
+ for attr_pk, value_obj in values_map.items():
+ key = attributes_map[attr_pk]
+ if not isinstance(value_obj, str):
+ value_obj = value_obj.translated
+ product_attributes[key] = value_obj
+
+ return product_attributes
def get_name_from_attributes(variant, attributes):
@@ -32,10 +39,10 @@ def get_attributes_display_map(obj, attributes):
"""
display_map = defaultdict(str)
for attribute in attributes:
- values = obj.attributes.get(str(attribute.pk)) # type: List
- if values:
+ attribute_values = obj.attributes.get(str(attribute.pk)) # type: List
+ if attribute_values:
choices = {str(a.pk): a.translated for a in attribute.values.all()}
- for value in values:
+ for value in attribute_values:
current_display_value = display_map[attribute.pk]
if not current_display_value:
current_display_value = choices[value]
diff --git a/templates/product/details.html b/templates/product/details.html
index 8ed67ad949..bcfcccae59 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -201,7 +201,7 @@
{% for attribute, value in product_attributes.items %}
<tr>
<td>{{ attribute.translated }}:</td>
- <td><strong>{{ value.translated }}</strong></td>
+ <td><strong>{{ value }}</strong></td>
</tr>
{% endfor %}
</table>
diff --git a/tests/conftest.py b/tests/conftest.py
index fe80d9f059..c591008d3d 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -469,31 +469,27 @@ def product(product_type, category):
@pytest.fixture
-def variant_with_multiple_values_attributes(
- variant, product_type, category
-) -> ProductVariant:
- variant_attr = Attribute.objects.create(
+def product_with_multiple_values_attributes(product, product_type, category) -> Product:
+
+ attribute = Attribute.objects.create(
slug="modes", name="Available Modes", input_type=AttributeInputType.MULTISELECT
)
attr_val_1 = AttributeValue.objects.create(
- attribute=variant_attr, name="Eco Mode", slug="eco"
+ attribute=attribute, name="Eco Mode", slug="eco"
)
attr_val_2 = AttributeValue.objects.create(
- attribute=variant_attr, name="Performance Mode", slug="power"
+ attribute=attribute, name="Performance Mode", slug="power"
)
- product_type.variant_attributes.clear()
- product_type.variant_attributes.add(variant_attr)
+ product_type.product_attributes.clear()
+ product_type.product_attributes.add(attribute)
- variant.attributes = {
- smart_text(variant_attr.pk): [
- smart_text(attr_val_1.pk),
- smart_text(attr_val_2.pk),
- ]
+ product.attributes = {
+ smart_text(attribute.pk): [smart_text(attr_val_1.pk), smart_text(attr_val_2.pk)]
}
- variant.save(update_fields=["attributes"])
- return variant
+ product.save(update_fields=["attributes"])
+ return product
@pytest.fixture
diff --git a/tests/test_product.py b/tests/test_product.py
index f9c8534ca5..ce9b23202a 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -6,10 +6,12 @@ from decimal import Decimal
from unittest.mock import patch
import pytest
+from bs4 import BeautifulSoup, Tag
from django.core import serializers
from django.core.exceptions import ValidationError
from django.core.serializers.base import DeserializationError
from django.http import JsonResponse
+from django.template.response import TemplateResponse
from django.urls import reverse
from freezegun import freeze_time
from prices import Money, MoneyRange, TaxedMoney
@@ -20,8 +22,16 @@ from saleor.checkout.models import Checkout
from saleor.checkout.utils import add_variant_to_checkout
from saleor.menu.models import MenuItemTranslation
from saleor.menu.utils import update_menu
-from saleor.product import ProductAvailabilityStatus, models
-from saleor.product.models import DigitalContentUrl, validate_attribute_json
+from saleor.product import AttributeInputType, ProductAvailabilityStatus, models
+from saleor.product.models import (
+ Attribute,
+ AttributeTranslation,
+ AttributeValue,
+ AttributeValueTranslation,
+ DigitalContentUrl,
+ Product,
+ validate_attribute_json,
+)
from saleor.product.thumbnails import create_product_thumbnails
from saleor.product.utils import (
allocate_stock,
@@ -413,6 +423,70 @@ def test_product_filter_product_exists(authorized_client, product, category):
assert list(response.context["filter_set"].qs) == list(products)
+def test_product_filter_multi_values_attribute(
+ authorized_client, product_with_multiple_values_attributes, category
+):
+ """This tests the filters against multiple values attributes.
+
+ It ensures:
+ - It can filter by selecting multiple values
+ - It can filter by selecting only one value
+ - Having no occurrence can actually happen
+ """
+
+ product = product_with_multiple_values_attributes
+ product_type = product.product_type
+ attribute = product_type.product_attributes.first()
+ attribute_values = attribute.values.in_bulk() # type: dict
+
+ url = reverse(
+ "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
+ )
+
+ # Try selecting all the values
+ data = {"modes": list(attribute_values.keys())}
+ response = authorized_client.get(url, data)
+ assert list(response.context["filter_set"].qs) == [product]
+
+ # Try filtering with only one value
+ data["modes"].pop()
+ response = authorized_client.get(url, data)
+ assert list(response.context["filter_set"].qs) == [product]
+
+ # Try filtering with no occurrence
+ product.attributes[str(attribute.pk)].remove(str(data["modes"][0]))
+ product.save(update_fields=["attributes"])
+ response = authorized_client.get(url, data)
+ assert list(response.context["filter_set"].qs) == []
+
+
+def test_product_filter_non_filterable(
+ authorized_client,
+ product_with_multiple_values_attributes,
+ category,
+ product_list_published,
+):
+ """Ensures one cannot filter using a non filterable attribute"""
+
+ product = product_with_multiple_values_attributes
+ product_type = product.product_type
+ attribute = product_type.product_attributes.first()
+ attribute_values = attribute.values.in_bulk() # type: dict
+ attribute.filterable_in_storefront = False
+ attribute.save(update_fields=["filterable_in_storefront"])
+
+ url = reverse(
+ "product:category", kwargs={"slug": category.slug, "category_id": category.pk}
+ )
+
+ # Try selecting by the disabled attribute
+ data = {"modes": list(attribute_values.keys())}
+ response = authorized_client.get(url, data)
+
+ # Nothing should have been filtered, thus returning all the products
+ assert list(response.context["filter_set"].qs) == list(Product.objects.all())
+
+
def test_product_filter_product_does_not_exist(authorized_client, product, category):
url = reverse(
"product:category", kwargs={"slug": category.slug, "category_id": category.pk}
@@ -477,13 +551,31 @@ def test_get_variant_picker_data_proper_variant_count(product):
assert len(data["variantAttributes"][0]["values"]) == 1
-def test_get_variant_picker_data_no_nested_attributes(
- variant_with_multiple_values_attributes
-):
+def test_get_variant_picker_data_no_nested_attributes(variant, product_type, category):
"""Ensures that if someone bypassed variant attributes checks (e.g. a raw SQL query)
and inserted an attribute with multiple values, it doesn't return invalid data
to the storefront that would crash it."""
- product = variant_with_multiple_values_attributes.product
+
+ variant_attr = Attribute.objects.create(
+ slug="modes", name="Available Modes", input_type=AttributeInputType.MULTISELECT
+ )
+
+ attr_val_1 = AttributeValue.objects.create(
+ attribute=variant_attr, name="Eco Mode", slug="eco"
+ )
+ attr_val_2 = AttributeValue.objects.create(
+ attribute=variant_attr, name="Performance Mode", slug="power"
+ )
+
+ product_type.variant_attributes.clear()
+ product_type.variant_attributes.add(variant_attr)
+
+ variant.attributes = {
+ str(variant_attr.pk): [str(attr_val_1.pk), str(attr_val_2.pk)]
+ }
+ variant.save(update_fields=["attributes"])
+
+ product = variant.product
data = get_variant_picker_data(
product, discounts=None, taxes=None, local_currency=None
)
@@ -505,6 +597,99 @@ def test_render_product_page_with_no_variant(unavailable_product, admin_client):
assert response.status_code == 200
+def test_render_product_page_with_multi_values_attribute(
+ client, product_with_multiple_values_attributes
+):
+ """This test ensures the rendering of a product without attribute doesn't fail."""
+ product = product_with_multiple_values_attributes
+ url = reverse(
+ "product:details", kwargs={"product_id": product.pk, "slug": product.get_slug()}
+ )
+ response = client.get(url)
+ assert response.status_code == 200
+
+
+def test_product_page_renders_attributes_properly(
+ settings, client, product_with_multiple_values_attributes, color_attribute
+):
+ """This test ensures the product attributes are properly rendered as expected
+ including the translations."""
+
+ settings.LANGUAGE_CODE = "fr"
+
+ product = product_with_multiple_values_attributes
+ multi_values_attribute = product.product_type.product_attributes.first()
+
+ # Retrieve the attributes' values
+ red, blue = color_attribute.values.all()
+ eco_mode, performance_mode = multi_values_attribute.values.all()
+
+ # Assign the dropdown attribute to the product
+ product.product_type.product_attributes.add(color_attribute)
+ product.attributes[str(color_attribute.pk)] = [
+ str(color_attribute.values.first().pk)
+ ]
+ product.save(update_fields=["attributes"])
+
+ # Create the attribute name translations
+ AttributeTranslation.objects.bulk_create(
+ [
+ AttributeTranslation(
+ language_code="fr",
+ attribute=multi_values_attribute,
+ name="Multiple Valeurs",
+ ),
+ AttributeTranslation(
+ language_code="fr", attribute=color_attribute, name="Couleur"
+ ),
+ ]
+ )
+
+ # Create the attribute value translations
+ AttributeValueTranslation.objects.bulk_create(
+ [
+ AttributeValueTranslation(
+ language_code="fr", attribute_value=red, name="Rouge"
+ ),
+ AttributeValueTranslation(
+ language_code="fr", attribute_value=blue, name="Bleu"
+ ),
+ AttributeValueTranslation(
+ language_code="fr", attribute_value=eco_mode, name="Mode économique"
+ ),
+ AttributeValueTranslation(
+ language_code="fr",
+ attribute_value=performance_mode,
+ name="Mode performance",
+ ),
+ ]
+ )
+
+ # Render the page
+ url = reverse(
+ "product:details", kwargs={"product_id": product.pk, "slug": product.get_slug()}
+ )
+ response = client.get(url) # type: TemplateResponse
+ assert response.status_code == 200
+
+ # Retrieve the attribute table
+ soup = BeautifulSoup(response.content, "lxml")
+ attribute_table = soup.select_one(".product__info table") # type: Tag
+ assert attribute_table, "Did not find the attribute table"
+
+ # Retrieve the table rows
+ expected_attributes = {
+ "Multiple Valeurs:Mode économique, Mode performance",
+ "Couleur:Rouge",
+ }
+ actual_attributes = [
+ row.get_text(strip=True) for row in attribute_table.select("tr")
+ ]
+
+ assert len(actual_attributes) == 2
+ assert set(actual_attributes) == expected_attributes
+
+
def test_include_products_from_subcategories_in_main_view(
category, product, authorized_client
):
@@ -755,6 +940,35 @@ def test_get_product_attributes_data_translation(
assert translated_attribute.name in attributes_keys
+def test_get_product_attributes_data_excludes_hidden_attributes(
+ product, color_attribute
+):
+ """Ensures hidden attributes are ignored."""
+
+ attributes_data = get_product_attributes_data(product)
+ assert attributes_data
+
+ color_attribute.visible_in_storefront = False
+ color_attribute.save(update_fields=["visible_in_storefront"])
+ attributes_data = get_product_attributes_data(product)
+ assert not attributes_data
+
+
+def test_get_product_attributes_data_multi_values_attribute(
+ product_with_multiple_values_attributes
+):
+ """Ensures attributes with multiple values are properly resolved and handled."""
+
+ product = product_with_multiple_values_attributes
+ attribute = product.product_type.product_attributes.first()
+
+ attributes_data = get_product_attributes_data(product)
+ values = list(attributes_data.values())
+
+ assert len(values) == 1
+ assert values[0] == ", ".join([v.name for v in attribute.values.all()])
+
+
def test_homepage_collection_render(client, site_settings, collection, product_list):
collection.products.add(*product_list)
site_settings.homepage_collection = collection