Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use only one field for search stocks
Mira Szewczyk26a43f6944ae
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex c2e8c494ea..667050d8f3 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3492,7 +3492,7 @@ type Query {webhookEvents: [WebhookEvent]webhookSamplePayload(eventType: WebhookEventTypeEnum!): JSONStringwarehouse(id: ID!): Warehouse- warehouses(filter: WarehouseFilterInput, query: String, before: String, after: String, first: Int, last: Int): WarehouseCountableConnection+ warehouses(filter: WarehouseFilterInput, before: String, after: String, first: Int, last: Int): WarehouseCountableConnectiontranslations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnectiontranslation(id: ID!, kind: TranslatableKinds!): TranslatableItemstock(id: ID!): Stock@@ -4201,8 +4201,7 @@ type StockError {input StockFilterInput {quantity: FloatquantityAllocated: Float- productVariant: String- warehouse: String+ search: String}input StockInput {@@ -4673,10 +4672,7 @@ type WarehouseError {}input WarehouseFilterInput {- name: String- companyName: String- email: String- address: String+ search: String}type WarehouseUpdate {diff --git a/saleor/graphql/stock/filters.py b/saleor/graphql/stock/filters.pyindex dfd1cbdc11..58abdbea7a 100644--- a/saleor/graphql/stock/filters.py+++ b/saleor/graphql/stock/filters.py@@ -1,35 +1,27 @@import django_filters-from django.db.models import Qfrom ...stock.models import Stockfrom ..core.types import FilterInputObjectType---def qs_prefetched_for_filters(qs):- return qs.prefetch_related("product_variant__product").select_related(- "product_variant"- )---def filter_product_variant(qs, _, value):- qs = qs_prefetched_for_filters(qs).filter(- Q(product_variant__product__name__icontains=value)- | Q(product_variant__name__icontains=value)- )- return qs---def filter_warehouse(qs, _, value):- qs = qs_prefetched_for_filters(qs).filter(- Q(warehouse__name__icontains=value)- | Q(warehouse__company_name__icontains=value)+from ..utils import filter_by_query_param+++def filter_search(qs, _, value):+ search_fields = [+ "product_variant__product__name",+ "product_variant__name",+ "warehouse__name",+ "warehouse__company_name",+ ]+ qs = qs.select_related("product_variant", "warehouse").prefetch_related(+ "product_variant__product")+ if value:+ qs = filter_by_query_param(qs, value, search_fields)return qsclass StockFilter(django_filters.FilterSet):- product_variant = django_filters.CharFilter(method=filter_product_variant)- warehouse = django_filters.CharFilter(method=filter_warehouse)+ search = django_filters.CharFilter(method=filter_search)class Meta:model = Stockdiff --git a/saleor/graphql/warehouse/filters.py b/saleor/graphql/warehouse/filters.pyindex f15b5a0f64..bc0b8869b5 100644--- a/saleor/graphql/warehouse/filters.py+++ b/saleor/graphql/warehouse/filters.py@@ -1,31 +1,34 @@import django_filters-from django.db.models import Qfrom ...warehouse.models import Warehousefrom ..core.types import FilterInputObjectType+from ..utils import filter_by_query_param-def filter_string(qs, name, value):- lookup = "__".join([name, "icontains"])- return qs.filter(**{lookup: value})+def prefech_qs_for_filter(qs):+ return qs.select_related("address")-def filter_address(qs, _, value):- qs = qs.filter(- Q(address__street_address_1__icontains=value)- | Q(address__street_address_2__icontains=value)- | Q(address__city__icontains=value)- | Q(address__postal_code=value)- | Q(address__phone__contains=value)- )+def filter_search(qs, _, value):+ search_fields = [+ "name",+ "company_name",+ "email",+ "address__street_address_1",+ "address__street_address_2",+ "address__city",+ "address__postal_code",+ "address__phone",+ ]+ qs = prefech_qs_for_filter(qs)++ if value:+ qs = filter_by_query_param(qs, value, search_fields)return qsclass WarehouseFilter(django_filters.FilterSet):- name = django_filters.CharFilter(method=filter_string)- company_name = django_filters.CharFilter(method=filter_string)- email = django_filters.CharFilter(method=filter_string)- address = django_filters.CharFilter(method=filter_address)+ search = django_filters.CharFilter(method=filter_search)class Meta:model = Warehousediff --git a/saleor/graphql/warehouse/schema.py b/saleor/graphql/warehouse/schema.pyindex 978db5ea39..014a4ab576 100644--- a/saleor/graphql/warehouse/schema.py+++ b/saleor/graphql/warehouse/schema.py@@ -18,10 +18,7 @@ class WarehouseQueries(graphene.ObjectType):),)warehouses = FilterInputConnectionField(- Warehouse,- description="List of warehouses.",- filter=WarehouseFilterInput(),- query=graphene.String(),+ Warehouse, description="List of warehouses.", filter=WarehouseFilterInput())@permission_required("warehouse.manage_warehouses")diff --git a/tests/api/test_stock.py b/tests/api/test_stock.pyindex efe0683d81..3a20b4c1fb 100644--- a/tests/api/test_stock.py+++ b/tests/api/test_stock.py@@ -365,7 +365,7 @@ def test_query_stocks_with_filters_warehouse(staff_api_client.user.user_permissions.add(permission_manage_stocks)warehouse = stock.warehouseresponse_name = staff_api_client.post_graphql(- QUERY_STOCKS_WITH_FILTERS, variables={"filter": {"warehouse": warehouse.name}}+ QUERY_STOCKS_WITH_FILTERS, variables={"filter": {"search": warehouse.name}})content = get_graphql_content(response_name)total_count = content["data"]["stocks"]["totalCount"]@@ -379,7 +379,7 @@ def test_query_stocks_with_filters_product_variant(product_variant = stock.product_variantresponse_name = staff_api_client.post_graphql(QUERY_STOCKS_WITH_FILTERS,- variables={"filter": {"productVariant": product_variant.name}},+ variables={"filter": {"search": product_variant.name}},)content = get_graphql_content(response_name)total_count = content["data"]["stocks"]["totalCount"]@@ -395,8 +395,7 @@ def test_query_stocks_with_filters_product_variant__product(staff_api_client.user.user_permissions.add(permission_manage_stocks)product = stock.product_variant.productresponse_name = staff_api_client.post_graphql(- QUERY_STOCKS_WITH_FILTERS,- variables={"filter": {"productVariant": product.name}},+ QUERY_STOCKS_WITH_FILTERS, variables={"filter": {"search": product.name}})content = get_graphql_content(response_name)total_count = content["data"]["stocks"]["totalCount"]diff --git a/tests/api/test_warehouse.py b/tests/api/test_warehouse.pyindex db75f5876a..aed814d908 100644--- a/tests/api/test_warehouse.py+++ b/tests/api/test_warehouse.py@@ -206,7 +206,7 @@ def test_query_warehouses_with_filters_name(staff_api_client, permission_manage_warehouses, warehouse):staff_api_client.user.user_permissions.add(permission_manage_warehouses)- variables_exists = {"filters": {"name": "warehouse"}}+ variables_exists = {"filters": {"search": "warehouse"}}response = staff_api_client.post_graphql(QUERY_WAREHOUSES_WITH_FILTERS, variables=variables_exists)@@ -214,7 +214,7 @@ def test_query_warehouses_with_filters_name(warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)content_warehouse = content["data"]["warehouses"]["edges"][0]["node"]assert content_warehouse["id"] == warehouse_id- variables_does_not_exists = {"filters": {"name": "Absolutelywrong name"}}+ variables_does_not_exists = {"filters": {"search": "Absolutelywrong name"}}response1 = staff_api_client.post_graphql(QUERY_WAREHOUSES_WITH_FILTERS, variables=variables_does_not_exists)@@ -227,7 +227,7 @@ def test_query_warehouse_with_filters_email(staff_api_client, permission_manage_warehouses, warehouse):staff_api_client.user.user_permissions.add(permission_manage_warehouses)- variables_exists = {"filters": {"email": "test"}}+ variables_exists = {"filters": {"search": "test"}}response_exists = staff_api_client.post_graphql(QUERY_WAREHOUSES_WITH_FILTERS, variables=variables_exists)@@ -236,7 +236,7 @@ def test_query_warehouse_with_filters_email(content_warehouse = content_exists["data"]["warehouses"]["edges"][0]["node"]assert content_warehouse["id"] == warehouse_id- variable_does_not_exists = {"filters": {"email": "[email protected]"}}+ variable_does_not_exists = {"filters": {"search": "[email protected]"}}response_not_exists = staff_api_client.post_graphql(QUERY_WAREHOUSES_WITH_FILTERS, variables=variable_does_not_exists)