Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filtering for warehouse query
Mira Szewczykfdbb055d55c3
diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.pyindex 5d35188666..130a9bbb63 100644--- a/saleor/graphql/core/fields.py+++ b/saleor/graphql/core/fields.py@@ -178,6 +178,7 @@ class FilterInputConnectionField(BaseDjangoConnectionField):on_resolve = partial(cls.resolve_connection, connection, default_manager, args)filter_input = args.get(filters_name)+if filter_input and filterset_class:iterable = filterset_class(data=dict(filter_input), queryset=iterable, request=info.contextdiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 404d7df9bb..c287bae691 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3489,7 +3489,7 @@ type Query {webhookEvents: [WebhookEvent]webhookSamplePayload(eventType: WebhookEventTypeEnum!): JSONStringwarehouse(id: ID!): Warehouse- warehouses(before: String, after: String, first: Int, last: Int): WarehouseCountableConnection+ warehouses(filter: WarehouseFilterInput, query: String, before: String, after: String, first: Int, last: Int): WarehouseCountableConnectiontranslations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnectiontranslation(id: ID!, kind: TranslatableKinds!): TranslatableItemshop: Shop@@ -4585,6 +4585,13 @@ type WarehouseError {code: ExtensionsErrorCode}+input WarehouseFilterInput {+ name: String+ companyName: String+ email: String+ address: String+}+type WarehouseUpdate {errors: [Error!]warehouseErrors: [WarehouseError!]diff --git a/saleor/graphql/warehouse/filters.py b/saleor/graphql/warehouse/filters.pynew file mode 100644index 0000000000..f15b5a0f64--- /dev/null+++ b/saleor/graphql/warehouse/filters.py@@ -0,0 +1,37 @@+import django_filters+from django.db.models import Q++from ...warehouse.models import Warehouse+from ..core.types import FilterInputObjectType+++def filter_string(qs, name, value):+ lookup = "__".join([name, "icontains"])+ return qs.filter(**{lookup: value})+++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)+ )+ return qs+++class 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)++ class Meta:+ model = Warehouse+ fields = []+++class WarehouseFilterInput(FilterInputObjectType):+ class Meta:+ filterset_class = WarehouseFilterdiff --git a/saleor/graphql/warehouse/schema.py b/saleor/graphql/warehouse/schema.pyindex 1ea8dc0af9..2d66796e56 100644--- a/saleor/graphql/warehouse/schema.py+++ b/saleor/graphql/warehouse/schema.py@@ -2,8 +2,9 @@ import grapheneimport graphene_django_optimizer as gql_optimizerfrom ...warehouse import models-from ..core.fields import PrefetchingConnectionField+from ..core.fields import FilterInputConnectionFieldfrom ..decorators import permission_required+from .filters import WarehouseFilterInputfrom .mutations import WarehouseCreate, WarehouseDelete, WarehouseUpdatefrom .types import Warehouse@@ -16,8 +17,11 @@ class WarehouseQueries(graphene.ObjectType):graphene.ID, description="ID of an warehouse", required=True),)- warehouses = PrefetchingConnectionField(- Warehouse, description="List of warehouses."+ warehouses = FilterInputConnectionField(+ Warehouse,+ description="List of warehouses.",+ filter=WarehouseFilterInput(),+ query=graphene.String(),)@permission_required("warehouse.manage_warehouses")diff --git a/tests/api/test_warehouse.py b/tests/api/test_warehouse.pyindex 4666d3b175..db75f5876a 100644--- a/tests/api/test_warehouse.py+++ b/tests/api/test_warehouse.py@@ -38,6 +38,22 @@ query {}"""+QUERY_WAREHOUSES_WITH_FILTERS = """+query Warehouses($filters: WarehouseFilterInput) {+ warehouses(first:100, filter: $filters) {+ totalCount+ edges {+ node {+ id+ name+ companyName+ }+ }+ }+}+"""+QUERY_WAREHOUSE = """query warehouse($id: ID!){warehouse(id: $id) {@@ -186,6 +202,49 @@ def test_query_warehouses(staff_api_client, warehouse, permission_manage_warehou)+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"}}+ response = staff_api_client.post_graphql(+ QUERY_WAREHOUSES_WITH_FILTERS, variables=variables_exists+ )+ content = get_graphql_content(response)+ 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"}}+ response1 = staff_api_client.post_graphql(+ QUERY_WAREHOUSES_WITH_FILTERS, variables=variables_does_not_exists+ )+ content1 = get_graphql_content(response1)+ total_count = content1["data"]["warehouses"]["totalCount"]+ assert total_count == 0+++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"}}+ response_exists = staff_api_client.post_graphql(+ QUERY_WAREHOUSES_WITH_FILTERS, variables=variables_exists+ )+ content_exists = get_graphql_content(response_exists)+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)+ content_warehouse = content_exists["data"]["warehouses"]["edges"][0]["node"]+ assert content_warehouse["id"] == warehouse_id++ variable_does_not_exists = {"filters": {"email": "[email protected]"}}+ response_not_exists = staff_api_client.post_graphql(+ QUERY_WAREHOUSES_WITH_FILTERS, variables=variable_does_not_exists+ )+ content_not_exists = get_graphql_content(response_not_exists)+ total_count = content_not_exists["data"]["warehouses"]["totalCount"]+ assert total_count == 0++def test_mutation_create_warehouse_requires_permission(staff_api_client):assert not staff_api_client.user.has_perm("warehouse.manage_warehouses")variables = {