Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add new filters to products (#10784)
Krzysztof Waliczekc7615d1a7625
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3c1d4cf933..71704c4306 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,11 @@ All notable, unreleased changes to this project will be documented in this file.- Add ability to filter by slug. #10578 by @kadewu- Affected types: Attribute, Category, Collection, Menu, Page, Product, ProductType, Warehouse- Deprecated `slug` in filter for `menus`. Use `slugs` instead+- Add new `products` filters. #10784 by @kadewu+ - `isAvailable`+ - `publishedFrom`+ - `availableFrom`+ - `isVisibleInListing`- Add ability to filter payments by list of ids. #10821 by @kadewu- Add ability to filter customers by ids. #10694 by @kadewu- Add `User.checkouts` field. #10862 by @zedziordiff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.pyindex ab80e99c98..27b19c870a 100644--- a/saleor/graphql/product/filters.py+++ b/saleor/graphql/product/filters.py@@ -36,6 +36,7 @@ from ...product.models import (from ...product.search import search_productsfrom ...warehouse.models import Allocation, Stock, Warehousefrom ..channel.filters import get_channel_slug_from_filter_data+from ..core.descriptions import ADDED_IN_38from ..core.filters import (EnumFilter,GlobalIDMultipleChoiceFilter,@@ -451,6 +452,46 @@ def _filter_products_is_published(qs, _, value, channel_slug):)+def _filter_products_is_available(qs, _, value, channel_slug):+ channel = Channel.objects.filter(slug=channel_slug).values("pk")+ now = datetime.datetime.now(pytz.UTC)+ if value:+ product_channel_listings = ProductChannelListing.objects.filter(+ Exists(channel.filter(pk=OuterRef("channel_id"))),+ available_for_purchase_at__lte=now,+ ).values("product_id")+ else:+ product_channel_listings = ProductChannelListing.objects.filter(+ Exists(channel.filter(pk=OuterRef("channel_id"))),+ Q(available_for_purchase_at__gt=now)+ | Q(available_for_purchase_at__isnull=True),+ ).values("product_id")++ return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))+++def _filter_products_channel_field_from_date(qs, _, value, channel_slug, field):+ channel = Channel.objects.filter(slug=channel_slug).values("pk")+ lookup = {+ f"{field}__lte": value,+ }+ product_channel_listings = ProductChannelListing.objects.filter(+ Exists(channel.filter(pk=OuterRef("channel_id"))),+ **lookup,+ ).values("product_id")++ return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))+++def _filter_products_visible_in_listing(qs, _, value, channel_slug):+ channel = Channel.objects.filter(slug=channel_slug).values("pk")+ product_channel_listings = ProductChannelListing.objects.filter(+ Exists(channel.filter(pk=OuterRef("channel_id"))), visible_in_listings=value+ ).values("product_id")++ return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))++def _filter_variant_price(qs, _, value, channel_slug):qs = filter_products_by_variant_price(qs, channel_slug, price_lte=value.get("lte"), price_gte=value.get("gte")@@ -594,6 +635,24 @@ class ProductStockFilterInput(graphene.InputObjectType):class ProductFilter(MetadataFilterBase):is_published = django_filters.BooleanFilter(method="filter_is_published")+ published_from = ObjectTypeFilter(+ input_class=graphene.DateTime,+ method="filter_published_from",+ help_text=f"Filter by the publication date. {ADDED_IN_38}",+ )+ is_available = django_filters.BooleanFilter(+ method="filter_is_available",+ help_text=f"Filter by availability for purchase. {ADDED_IN_38}",+ )+ available_from = ObjectTypeFilter(+ input_class=graphene.DateTime,+ method="filter_available_from",+ help_text=f"Filter by the date of availability for purchase. {ADDED_IN_38}",+ )+ is_visible_in_listing = django_filters.BooleanFilter(+ method="filter_listed",+ help_text=f"Filter by visibility in product listings. {ADDED_IN_38}",+ )collections = GlobalIDMultipleChoiceFilter(method=filter_collections)categories = GlobalIDMultipleChoiceFilter(method=filter_categories)has_category = django_filters.BooleanFilter(method=filter_has_category)@@ -664,6 +723,44 @@ class ProductFilter(MetadataFilterBase):channel_slug,)+ def filter_published_from(self, queryset, name, value):+ channel_slug = get_channel_slug_from_filter_data(self.data)+ return _filter_products_channel_field_from_date(+ queryset,+ name,+ value,+ channel_slug,+ "published_at",+ )++ def filter_is_available(self, queryset, name, value):+ channel_slug = get_channel_slug_from_filter_data(self.data)+ return _filter_products_is_available(+ queryset,+ name,+ value,+ channel_slug,+ )++ def filter_available_from(self, queryset, name, value):+ channel_slug = get_channel_slug_from_filter_data(self.data)+ return _filter_products_channel_field_from_date(+ queryset,+ name,+ value,+ channel_slug,+ "available_for_purchase_at",+ )++ def filter_listed(self, queryset, name, value):+ channel_slug = get_channel_slug_from_filter_data(self.data)+ return _filter_products_visible_in_listing(+ queryset,+ name,+ value,+ channel_slug,+ )+def filter_stock_availability(self, queryset, name, value):channel_slug = get_channel_slug_from_filter_data(self.data)return _filter_stock_availability(queryset, name, value, channel_slug)diff --git a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.pyindex bcda88b6b7..88894943fc 100644--- a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py+++ b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py@@ -66,6 +66,7 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):is_published=True,discounted_price_amount=Decimal(5),published_at=datetime.datetime(2002, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=None,),ProductChannelListing(product=products[1],@@ -73,6 +74,9 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):is_published=True,discounted_price_amount=Decimal(15),published_at=datetime.datetime(2000, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=datetime.datetime(+ 2003, 1, 1, tzinfo=pytz.UTC+ ),),ProductChannelListing(product=products[2],@@ -80,13 +84,20 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):is_published=False,discounted_price_amount=Decimal(4),published_at=datetime.datetime(1999, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=datetime.datetime(+ 2000, 1, 1, tzinfo=pytz.UTC+ ),),ProductChannelListing(product=products[3],channel=channel_USD,is_published=True,+ visible_in_listings=True,discounted_price_amount=Decimal(7),published_at=datetime.datetime(2001, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=datetime.datetime(+ 2001, 1, 1, tzinfo=pytz.UTC+ ),),# Second channelProductChannelListing(@@ -95,6 +106,9 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):is_published=False,discounted_price_amount=Decimal(15),published_at=datetime.datetime(2003, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=datetime.datetime(+ 2003, 1, 1, tzinfo=pytz.UTC+ ),),ProductChannelListing(product=products[1],@@ -102,6 +116,9 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):is_published=True,discounted_price_amount=Decimal(4),published_at=datetime.datetime(1999, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=datetime.datetime(+ 2002, 1, 1, tzinfo=pytz.UTC+ ),),ProductChannelListing(product=products[2],@@ -109,13 +126,18 @@ def products_for_sorting_with_channels(category, channel_USD, channel_PLN):is_published=True,discounted_price_amount=Decimal(5),published_at=datetime.datetime(2000, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=None,),ProductChannelListing(product=products[4],channel=channel_PLN,is_published=True,+ visible_in_listings=True,discounted_price_amount=Decimal(7),published_at=datetime.datetime(1998, 1, 1, tzinfo=pytz.UTC),+ available_for_purchase_at=datetime.datetime(+ 2000, 1, 1, tzinfo=pytz.UTC+ ),),])@@ -514,6 +536,12 @@ def test_products_with_filtering_without_channel([({"isPublished": True}, 3),({"isPublished": False}, 1),+ ({"isAvailable": True}, 3),+ ({"isAvailable": False}, 1),+ ({"publishedFrom": "2001-01-01T00:00:00+00:00"}, 3),+ ({"availableFrom": "2001-01-01T00:00:00+00:00"}, 2),+ ({"isVisibleInListing": True}, 1),+ ({"isVisibleInListing": False}, 3),({"price": {"lte": 8}}, 2),({"price": {"gte": 11}}, 1),({"minimalPrice": {"lte": 4}}, 1),@@ -553,6 +581,12 @@ def test_products_with_filtering_with_channel_USD([({"isPublished": True}, 3),({"isPublished": False}, 1),+ ({"isAvailable": True}, 3),+ ({"isAvailable": False}, 1),+ ({"publishedFrom": "2001-01-01T00:00:00+00:00"}, 3),+ ({"availableFrom": "2001-01-01T00:00:00+00:00"}, 1),+ ({"isVisibleInListing": True}, 1),+ ({"isVisibleInListing": False}, 3),({"price": {"lte": 8}}, 2),({"price": {"gte": 11}}, 1),({"minimalPrice": {"lte": 4}}, 1),@@ -589,6 +623,12 @@ def test_products_with_filtering_with_channel_PLN([{"isPublished": True},{"isPublished": False},+ {"isAvailable": True},+ {"isAvailable": False},+ {"publishedFrom": "2001-01-01T00:00:00+00:00"},+ {"availableFrom": "2001-01-01T00:00:00+00:00"},+ {"isVisibleInListing": True},+ {"isVisibleInListing": False},{"price": {"lte": 8}},{"price": {"gte": 11}},{"minimalPrice": {"lte": 4}},diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex d747ba0d00..f9ddf5f333 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -6006,6 +6006,34 @@ input ProductFilterInput {stocks: ProductStockFilterInputsearch: Stringmetadata: [MetadataFilter!]++ """+ Filter by the publication date.++ Added in Saleor 3.8.+ """+ publishedFrom: DateTime++ """+ Filter by availability for purchase.++ Added in Saleor 3.8.+ """+ isAvailable: Boolean++ """+ Filter by the date of availability for purchase.++ Added in Saleor 3.8.+ """+ availableFrom: DateTime++ """+ Filter by visibility in product listings.++ Added in Saleor 3.8.+ """+ isVisibleInListing: Booleanprice: PriceRangeInput"""Filter by the lowest variant price after discounts."""