Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Allow assigning channels to shipping zones
IKarbowiak11bfe01779ef
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 0ce3484574..d8a56a4a1f 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -5041,6 +5041,7 @@ input ShippingZoneCreateInput {countries: [String]default: BooleanaddWarehouses: [ID]+ addChannels: [ID!]}type ShippingZoneDelete {@@ -5061,7 +5062,9 @@ input ShippingZoneUpdateInput {countries: [String]default: BooleanaddWarehouses: [ID]+ addChannels: [ID!]removeWarehouses: [ID]+ removeChannels: [ID!]}type Shop {diff --git a/saleor/graphql/shipping/mutations/shippings.py b/saleor/graphql/shipping/mutations/shippings.pyindex 242a5dd2c7..959eb72f1d 100644--- a/saleor/graphql/shipping/mutations/shippings.py+++ b/saleor/graphql/shipping/mutations/shippings.py@@ -1,3 +1,5 @@+from collections import defaultdict+import graphenefrom django.core.exceptions import ValidationErrorfrom django.db import transaction@@ -15,9 +17,9 @@ from ...channel.types import ChannelContextfrom ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ...core.scalars import WeightScalarfrom ...core.types.common import ShippingError-from ...core.utils import get_duplicates_idsfrom ...product import types as product_typesfrom ...utils import resolve_global_ids_to_primary_keys+from ...utils.validators import check_for_duplicatesfrom ..enums import PostalCodeRuleInclusionTypeEnum, ShippingMethodTypeEnumfrom ..types import ShippingMethod, ShippingMethodPostalCodeRule, ShippingZone@@ -78,6 +80,10 @@ class ShippingZoneCreateInput(graphene.InputObjectType):graphene.ID,description="List of warehouses to assign to a shipping zone",)+ add_channels = graphene.List(+ graphene.NonNull(graphene.ID),+ description="List of channels to assign to the shipping zone.",+ )class ShippingZoneUpdateInput(ShippingZoneCreateInput):@@ -85,33 +91,50 @@ class ShippingZoneUpdateInput(ShippingZoneCreateInput):graphene.ID,description="List of warehouses to unassign from a shipping zone",)+ remove_channels = graphene.List(+ graphene.NonNull(graphene.ID),+ description="List of channels to unassign from the shipping zone.",+ )class ShippingZoneMixin:@classmethoddef clean_input(cls, info, instance, data, input_cls=None):- duplicates_ids = get_duplicates_ids(- data.get("add_warehouses"), data.get("remove_warehouses")+ errors = defaultdict(list)+ cls.check_duplicates(+ errors, data, "add_warehouses", "remove_warehouses", "warehouses"+ )+ cls.check_duplicates(+ errors, data, "add_channels", "remove_channels", "channels")- if duplicates_ids:- error_msg = (- "The same object cannot be in both lists "- "for adding and removing items."- )- raise ValidationError(- {- "removeWarehouses": ValidationError(- error_msg,- code=ShippingErrorCode.DUPLICATED_INPUT_ITEM.value,- params={"warehouses": list(duplicates_ids)},- )- }- )++ if errors:+ raise ValidationError(errors)cleaned_input = super().clean_input(info, instance, data)cleaned_input = cls.clean_default(instance, cleaned_input)return cleaned_input+ @classmethod+ def check_duplicates(+ cls,+ errors: dict,+ input_data: dict,+ add_field: str,+ remove_field: str,+ error_class_field: str,+ ):+ """Check if any items are on both input field.++ Raise error if some of items are duplicated.+ """+ error = check_for_duplicates(+ input_data, add_field, remove_field, error_class_field+ )+ if error:+ error.code = ShippingErrorCode.DUPLICATED_INPUT_ITEM.value+ errors[error_class_field].append(error)+@classmethoddef clean_default(cls, instance, data):default = data.get("default")@@ -145,6 +168,14 @@ class ShippingZoneMixin:if remove_warehouses:instance.warehouses.remove(*remove_warehouses)+ add_channels = cleaned_data.get("add_channels")+ if add_channels:+ instance.channels.add(*add_channels)++ remove_channels = cleaned_data.get("remove_channels")+ if remove_channels:+ instance.channels.remove(*remove_channels)+class ShippingZoneCreate(ShippingZoneMixin, ModelMutation):class Arguments:diff --git a/saleor/graphql/shipping/tests/test_shipping_method.py b/saleor/graphql/shipping/tests/test_shipping_method.pyindex 1165c8fa6e..8a09449a59 100644--- a/saleor/graphql/shipping/tests/test_shipping_method.py+++ b/saleor/graphql/shipping/tests/test_shipping_method.py@@ -266,6 +266,7 @@ CREATE_SHIPPING_ZONE_QUERY = """$default: Boolean$countries: [String]$addWarehouses: [ID]+ $addChannels: [ID!]) {shippingZoneCreate(input: {@@ -274,11 +275,14 @@ CREATE_SHIPPING_ZONE_QUERY = """countries: $countriesdefault: $defaultaddWarehouses: $addWarehouses+ addChannels: $addChannels}) {shippingErrors {fieldcode+ message+ channels}shippingZone {name@@ -290,19 +294,26 @@ CREATE_SHIPPING_ZONE_QUERY = """warehouses {name}+ channels {+ id+ }}}}"""-def test_create_shipping_zone(staff_api_client, warehouse, permission_manage_shipping):+def test_create_shipping_zone(+ staff_api_client, warehouse, permission_manage_shipping, channel_PLN+):warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)+ channel_id = graphene.Node.to_global_id("Channel", channel_PLN.pk)variables = {"name": "test shipping","description": "test description","countries": ["PL"],"addWarehouses": [warehouse_id],+ "addChannels": [channel_id],}response = staff_api_client.post_graphql(CREATE_SHIPPING_ZONE_QUERY, variables, permissions=[permission_manage_shipping]@@ -310,10 +321,14 @@ def test_create_shipping_zone(staff_api_client, warehouse, permission_manage_shicontent = get_graphql_content(response)data = content["data"]["shippingZoneCreate"]zone = data["shippingZone"]+ assert not data["shippingErrors"]assert zone["name"] == "test shipping"assert zone["description"] == "test description"assert zone["countries"] == [{"code": "PL"}]+ assert len(zone["warehouses"]) == 1assert zone["warehouses"][0]["name"] == warehouse.name+ assert len(zone["channels"]) == 1+ assert zone["channels"][0]["id"] == channel_idassert zone["default"] is False@@ -338,7 +353,7 @@ def test_create_shipping_zone_with_empty_warehouses(assert zone["default"] is False-def test_create_shipping_zone_without_warehouses(+def test_create_shipping_zone_without_warehouses_and_channels(staff_api_client, permission_manage_shipping):variables = {@@ -389,7 +404,12 @@ def test_create_duplicated_default_shipping_zone(shipping_zone.default = Trueshipping_zone.save()- variables = {"default": True, "name": "test shipping", "countries": ["PL"]}+ variables = {+ "default": True,+ "name": "test shipping",+ "countries": ["PL"],+ "addChannels": [],+ }response = staff_api_client.post_graphql(CREATE_SHIPPING_ZONE_QUERY, variables, permissions=[permission_manage_shipping])@@ -409,6 +429,8 @@ UPDATE_SHIPPING_ZONE_QUERY = """$countries: [String]$addWarehouses: [ID]$removeWarehouses: [ID]+ $addChannels: [ID!]+ $removeChannels: [ID!]) {shippingZoneUpdate(id: $id@@ -419,6 +441,8 @@ UPDATE_SHIPPING_ZONE_QUERY = """countries: $countriesaddWarehouses: $addWarehousesremoveWarehouses: $removeWarehouses+ addChannels:$addChannels+ removeChannels: $removeChannels}) {shippingZone {@@ -428,11 +452,15 @@ UPDATE_SHIPPING_ZONE_QUERY = """nameslug}+ channels {+ id+ }}shippingErrors {fieldcodewarehouses+ channels}}}@@ -639,7 +667,7 @@ def test_update_shipping_zone_same_warehouse_id_in_add_and_remove(content = get_graphql_content(response)data = content["data"]["shippingZoneUpdate"]assert data["shippingErrors"]- assert data["shippingErrors"][0]["field"] == "removeWarehouses"+ assert data["shippingErrors"][0]["field"] == "warehouses"assert (data["shippingErrors"][0]["code"]== ShippingErrorCode.DUPLICATED_INPUT_ITEM.name@@ -647,6 +675,122 @@ def test_update_shipping_zone_same_warehouse_id_in_add_and_remove(assert data["shippingErrors"][0]["warehouses"][0] == warehouse_id+def test_update_shipping_zone_add_channels(+ staff_api_client,+ shipping_zone,+ channel_USD,+ channel_PLN,+ permission_manage_shipping,+):+ shipping_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)+ channel_ids = [+ graphene.Node.to_global_id("Channel", channel.pk)+ for channel in [channel_USD, channel_PLN]+ ]++ variables = {+ "id": shipping_id,+ "name": shipping_zone.name,+ "addChannels": channel_ids,+ }+ response = staff_api_client.post_graphql(+ UPDATE_SHIPPING_ZONE_QUERY, variables, permissions=[permission_manage_shipping]+ )+ content = get_graphql_content(response)+ data = content["data"]["shippingZoneUpdate"]+ assert not data["shippingErrors"]+ data = content["data"]["shippingZoneUpdate"]["shippingZone"]+ assert len(data["channels"]) == len(channel_ids)+ assert {channel["id"] for channel in data["channels"]} == set(channel_ids)+++def test_update_shipping_zone_remove_channels(+ staff_api_client,+ shipping_zone,+ channel_USD,+ channel_PLN,+ permission_manage_shipping,+):+ shipping_zone.channels.add(channel_USD, channel_PLN)+ shipping_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)+ channel_id = graphene.Node.to_global_id("Channel", channel_PLN.pk)++ variables = {+ "id": shipping_id,+ "name": shipping_zone.name,+ "removeChannels": [channel_id],+ }+ response = staff_api_client.post_graphql(+ UPDATE_SHIPPING_ZONE_QUERY, variables, permissions=[permission_manage_shipping]+ )+ content = get_graphql_content(response)+ data = content["data"]["shippingZoneUpdate"]+ assert not data["shippingErrors"]+ data = content["data"]["shippingZoneUpdate"]["shippingZone"]+ assert len(data["channels"]) == 1+ assert data["channels"][0]["id"] == graphene.Node.to_global_id(+ "Channel", channel_USD.pk+ )+++def test_update_shipping_zone_add_and_remove_channels(+ staff_api_client,+ shipping_zone,+ channel_USD,+ channel_PLN,+ permission_manage_shipping,+):+ shipping_zone.channels.add(channel_USD)+ shipping_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)+ add_channel_id = graphene.Node.to_global_id("Channel", channel_PLN.pk)+ remove_channel_id = graphene.Node.to_global_id("Channel", channel_USD.pk)++ variables = {+ "id": shipping_id,+ "name": shipping_zone.name,+ "removeChannels": [remove_channel_id],+ "addChannels": [add_channel_id],+ }+ response = staff_api_client.post_graphql(+ UPDATE_SHIPPING_ZONE_QUERY, variables, permissions=[permission_manage_shipping]+ )+ content = get_graphql_content(response)+ data = content["data"]["shippingZoneUpdate"]+ assert not data["shippingErrors"]+ data = content["data"]["shippingZoneUpdate"]["shippingZone"]+ assert len(data["channels"]) == 1+ assert data["channels"][0]["id"] == add_channel_id+++def test_update_shipping_zone_same_channel_id_in_add_and_remove_list(+ staff_api_client,+ shipping_zone,+ channel_USD,+ channel_PLN,+ permission_manage_shipping,+):+ shipping_zone.channels.add(channel_USD)+ shipping_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)+ add_channel_id = graphene.Node.to_global_id("Channel", channel_PLN.pk)+ remove_channel_id = graphene.Node.to_global_id("Channel", channel_USD.pk)++ variables = {+ "id": shipping_id,+ "name": shipping_zone.name,+ "removeChannels": [remove_channel_id],+ "addChannels": [add_channel_id],+ }+ response = staff_api_client.post_graphql(+ UPDATE_SHIPPING_ZONE_QUERY, variables, permissions=[permission_manage_shipping]+ )+ content = get_graphql_content(response)+ data = content["data"]["shippingZoneUpdate"]+ assert not data["shippingErrors"]+ data = content["data"]["shippingZoneUpdate"]["shippingZone"]+ assert len(data["channels"]) == 1+ assert data["channels"][0]["id"] == add_channel_id++def test_delete_shipping_zone(staff_api_client, shipping_zone, permission_manage_shipping):