Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix filtering product attributes by date range (#9543)
Iga Karbowiak9f8dd193c59e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e0245411a9..902a20ae57 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -26,6 +26,7 @@ All notable, unreleased changes to this project will be documented in this file.- Add `HANDLE_CHECKOUTS` permission (only for apps)- Refactor app tokens - #9438 by @IKarbowiak- Store app tokens hashes instead of plain text.+- Fix filtering product attributes by date range - #9543 by @IKarbowiak- Save images to product media from external URLs - #9329 by @krzysztofwolskidiff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.pyindex 8671bf3eba..6ace66da83 100644--- a/saleor/graphql/product/filters.py+++ b/saleor/graphql/product/filters.py@@ -141,7 +141,7 @@ def _clean_product_attributes_date_time_range_filter_input(values_map = {attr.slug: {"pk": attr.pk,- "values": {val.date_time: val.pk for val in attr.values.all()},+ "values": {val.pk: val.date_time for val in attr.values.all()},}for attr in attributes}@@ -152,7 +152,7 @@ def _clean_product_attributes_date_time_range_filter_input(lte = val_range.get("lte")matching_values_pk = []- for value, pk in attr_values.items():+ for pk, value in attr_values.items():if is_date:value = value.date()diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 18785c7956..8b823bd914 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -2226,6 +2226,342 @@ def test_products_query_with_filter_swatch_attributes(assert products[0]["node"]["name"] == second_product.name+def test_products_query_with_filter_date_range_date_attributes(+ query_products_with_filter,+ staff_api_client,+ product_list,+ permission_manage_products,+ date_attribute,+ channel_USD,+):+ """Ensure both products will be returned when filtering attributes by date range,+ products with the same date attribute value."""++ # given+ product_type = product_list[0].product_type+ date_value = timezone.now()+ product_type.product_attributes.add(date_attribute)+ attr_value_1 = AttributeValue.objects.create(+ attribute=date_attribute, name="First", slug="first", date_time=date_value+ )+ attr_value_2 = AttributeValue.objects.create(+ attribute=date_attribute, name="Second", slug="second", date_time=date_value+ )+ attr_value_3 = AttributeValue.objects.create(+ attribute=date_attribute,+ name="Third",+ slug="third",+ date_time=date_value - timedelta(days=1),+ )++ associate_attribute_values_to_instance(+ product_list[0], date_attribute, attr_value_1+ )+ associate_attribute_values_to_instance(+ product_list[1], date_attribute, attr_value_2+ )+ associate_attribute_values_to_instance(+ product_list[2], date_attribute, attr_value_3+ )++ variables = {+ "filter": {+ "attributes": [+ {+ "slug": date_attribute.slug,+ "date": {"gte": date_value.date(), "lte": date_value.date()},+ }+ ],+ },+ }++ staff_api_client.user.user_permissions.add(permission_manage_products)++ # when+ response = staff_api_client.post_graphql(query_products_with_filter, variables)++ # then+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]+ assert len(products) == 2+ assert {node["node"]["id"] for node in products} == {+ graphene.Node.to_global_id("Product", instance.id)+ for instance in product_list[:2]+ }+++def test_products_query_with_filter_date_range_date_variant_attributes(+ query_products_with_filter,+ staff_api_client,+ product_list,+ permission_manage_products,+ date_attribute,+ channel_USD,+):+ """Ensure both products will be returned when filtering attributes by date range,+ variants with the same date attribute value."""++ # given+ product_type = product_list[0].product_type+ date_value = timezone.now()+ product_type.variant_attributes.add(date_attribute)+ attr_value_1 = AttributeValue.objects.create(+ attribute=date_attribute,+ name="First",+ slug="first",+ date_time=date_value - timedelta(days=1),+ )+ attr_value_2 = AttributeValue.objects.create(+ attribute=date_attribute, name="Second", slug="second", date_time=date_value+ )+ attr_value_3 = AttributeValue.objects.create(+ attribute=date_attribute, name="Third", slug="third", date_time=date_value+ )++ associate_attribute_values_to_instance(+ product_list[0].variants.first(), date_attribute, attr_value_1+ )+ associate_attribute_values_to_instance(+ product_list[1].variants.first(), date_attribute, attr_value_2+ )+ associate_attribute_values_to_instance(+ product_list[2].variants.first(), date_attribute, attr_value_3+ )++ variables = {+ "filter": {+ "attributes": [+ {+ "slug": date_attribute.slug,+ "date": {"gte": date_value.date(), "lte": date_value.date()},+ }+ ],+ },+ }++ staff_api_client.user.user_permissions.add(permission_manage_products)++ # when+ response = staff_api_client.post_graphql(query_products_with_filter, variables)++ # then+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]+ assert len(products) == 2+ assert {node["node"]["id"] for node in products} == {+ graphene.Node.to_global_id("Product", instance.id)+ for instance in product_list[1:]+ }+++def test_products_query_with_filter_date_range_date_time_attributes(+ query_products_with_filter,+ staff_api_client,+ product_list,+ permission_manage_products,+ date_time_attribute,+ channel_USD,+):+ """Ensure both products will be returned when filtering attributes by date time+ range, products with the same date time attribute value."""++ # given+ product_type = product_list[0].product_type+ date_value = timezone.now()+ product_type.product_attributes.add(date_time_attribute)+ attr_value_1 = AttributeValue.objects.create(+ attribute=date_time_attribute, name="First", slug="first", date_time=date_value+ )+ attr_value_2 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="Second",+ slug="second",+ date_time=date_value,+ )+ attr_value_3 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="Third",+ slug="third",+ date_time=date_value - timedelta(days=1),+ )++ associate_attribute_values_to_instance(+ product_list[0], date_time_attribute, attr_value_1+ )+ associate_attribute_values_to_instance(+ product_list[1], date_time_attribute, attr_value_2+ )+ associate_attribute_values_to_instance(+ product_list[2], date_time_attribute, attr_value_3+ )++ variables = {+ "filter": {+ "attributes": [+ {+ "slug": date_time_attribute.slug,+ "date": {"gte": date_value.date(), "lte": date_value.date()},+ }+ ],+ },+ }++ staff_api_client.user.user_permissions.add(permission_manage_products)++ # when+ response = staff_api_client.post_graphql(query_products_with_filter, variables)++ # then+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]+ assert len(products) == 2+ assert {node["node"]["id"] for node in products} == {+ graphene.Node.to_global_id("Product", instance.id)+ for instance in product_list[:2]+ }+++def test_products_query_with_filter_date_range_date_time_variant_attributes(+ query_products_with_filter,+ staff_api_client,+ product_list,+ permission_manage_products,+ date_time_attribute,+ channel_USD,+):+ """Ensure both products will be returned when filtering attributes by date time+ range, variant and product with the same date time attribute value."""++ # given+ product_type = product_list[0].product_type+ date_value = timezone.now()+ product_type.variant_attributes.add(date_time_attribute)+ attr_value_1 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="First",+ slug="first",+ date_time=date_value - timedelta(days=1),+ )+ attr_value_2 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="Second",+ slug="second",+ date_time=date_value,+ )+ attr_value_3 = AttributeValue.objects.create(+ attribute=date_time_attribute, name="Third", slug="third", date_time=date_value+ )++ associate_attribute_values_to_instance(+ product_list[0].variants.first(), date_time_attribute, attr_value_1+ )+ associate_attribute_values_to_instance(+ product_list[1].variants.first(), date_time_attribute, attr_value_2+ )+ associate_attribute_values_to_instance(+ product_list[2].variants.first(), date_time_attribute, attr_value_3+ )++ variables = {+ "filter": {+ "attributes": [+ {+ "slug": date_time_attribute.slug,+ "date": {"gte": date_value.date(), "lte": date_value.date()},+ }+ ],+ },+ }++ staff_api_client.user.user_permissions.add(permission_manage_products)++ # when+ response = staff_api_client.post_graphql(query_products_with_filter, variables)++ # then+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]+ assert len(products) == 2+ assert {node["node"]["id"] for node in products} == {+ graphene.Node.to_global_id("Product", instance.id)+ for instance in product_list[1:]+ }+++def test_products_query_with_filter_date_time_range_date_time_attributes(+ query_products_with_filter,+ staff_api_client,+ product_list,+ permission_manage_products,+ date_time_attribute,+ channel_USD,+):+ """Ensure both products will be returned when filtering by attributes by date range+ variants with the same date attribute value."""++ # given+ product_type = product_list[0].product_type+ date_value = timezone.now()+ product_type.product_attributes.add(date_time_attribute)+ product_type.variant_attributes.add(date_time_attribute)+ attr_value_1 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="First",+ slug="first",+ date_time=date_value - timedelta(hours=2),+ )+ attr_value_2 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="Second",+ slug="second",+ date_time=date_value + timedelta(hours=3),+ )+ attr_value_3 = AttributeValue.objects.create(+ attribute=date_time_attribute,+ name="Third",+ slug="third",+ date_time=date_value - timedelta(hours=6),+ )++ associate_attribute_values_to_instance(+ product_list[0], date_time_attribute, attr_value_1+ )+ associate_attribute_values_to_instance(+ product_list[1].variants.first(), date_time_attribute, attr_value_2+ )+ associate_attribute_values_to_instance(+ product_list[2].variants.first(), date_time_attribute, attr_value_3+ )++ variables = {+ "filter": {+ "attributes": [+ {+ "slug": date_time_attribute.slug,+ "dateTime": {+ "gte": date_value - timedelta(hours=4),+ "lte": date_value + timedelta(hours=4),+ },+ }+ ],+ },+ }++ staff_api_client.user.user_permissions.add(permission_manage_products)++ # when+ response = staff_api_client.post_graphql(query_products_with_filter, variables)++ # then+ content = get_graphql_content(response)+ products = content["data"]["products"]["edges"]+ assert len(products) == 2+ assert {node["node"]["id"] for node in products} == {+ graphene.Node.to_global_id("Product", instance.id)+ for instance in product_list[:2]+ }++def test_products_query_filter_by_non_existing_attribute(query_products_with_filter, api_client, product_list, channel_USD):