Skip to content

Saleor Core: the high performance, composable, headless commerce API.

WIP

Pacu28a02b7dd23af
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index bf3439c1d5..acd77d29b1 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -3,12 +3,13 @@ import graphene
from ...shipping import models
from ..core.mutations import ModelDeleteMutation, ModelMutation
from ..core.types.common import Decimal
-from .types import ShippingMethodTypeEnum, WeightScalar
+from .types import ShippingMethodTypeEnum, WeightScalar, ShippingZone
+from ...dashboard.shipping.forms import default_shipping_zone_exists
class ShippingPriceInput(graphene.InputObjectType):
name = graphene.String(
- description='Name of the shipping method.')
+ description='Name of the shipping method. Visible to customers')
price = Decimal(description='Shipping price of the shipping method.')
minimum_order_price = Decimal(
description='Minimum order price to use this shipping method',
@@ -17,25 +18,48 @@ class ShippingPriceInput(graphene.InputObjectType):
description='Maximum order price to use this shipping method',
required=False)
minimum_order_weight = WeightScalar(
- description='Minimum order price to use this shipping method',
+ description='Minimum order weight to use this shipping method',
required=False)
maximum_order_weight = WeightScalar(
- description='Maximum order price to use this shipping method',
+ description='Maximum order weight to use this shipping method',
required=False)
type = ShippingMethodTypeEnum(
description='Shipping type: price or weight based.')
shipping_zone = graphene.ID(
- description='Related shipping zone name.', name='shippingZone')
+ description='Shipping zone this method belongs to.',
+ name='shippingZone')
class ShippingZoneInput(graphene.InputObjectType):
- name = graphene.String(description='Shipping zone\'s name.')
+ name = graphene.String(
+ description='Shipping zone\'s name. Visible only to the staff.')
countries = graphene.List(
graphene.String,
- description='List of countries available in the shop.')
+ description='List of countries in this shipping zone.')
+ default = graphene.Boolean(
+ description="""
+ Is default shipping zone, that will be used
+ for countries not covered by other zones.""")
+
+
+class ShippingZoneMixin(object):
+
+ @classmethod
+ def clean_input(cls, info, instance, input, errors):
+ cleaned_input = super().clean_input(info, instance, input, errors)
+ if cleaned_input.get('default'):
+ if default_shipping_zone_exists(instance.pk):
+ cls.add_error(
+ errors, 'default', 'Default shipping zone already exists.')
+ elif cleaned_input.get('countries'):
+ cleaned_input['countries'] = []
+ return cleaned_input
+
+class ShippingZoneCreate(ShippingZoneMixin, ModelMutation):
+ shipping_zone = graphene.Field(
+ ShippingZone, description='Created shipping zone.')
-class ShippingZoneCreate(ModelMutation):
class Arguments:
input = ShippingZoneInput(
description='Fields required to create a shipping zone.',
@@ -50,7 +74,10 @@ class ShippingZoneCreate(ModelMutation):
return user.has_perm('shipping.manage_shipping')
-class ShippingZoneUpdate(ModelMutation):
+class ShippingZoneUpdate(ShippingZoneMixin, ModelMutation):
+ shipping_zone = graphene.Field(
+ ShippingZone, description='Updated shipping zone.')
+
class Arguments:
id = graphene.ID(
description='ID of a shipping zone to update.', required=True)
@@ -90,33 +117,30 @@ class ShippingPriceMixin(object):
if not type:
return cleaned_input
- field_errors = []
if type == ShippingMethodTypeEnum.PRICE.value:
min_price = cleaned_input.get('minimum_order_price')
max_price = cleaned_input.get('maximum_order_price')
if min_price is None:
- field_errors.append((
- 'minimum_order_price',
+ cls.add_error(
+ errors, 'minimum_order_price',
'Minimum order price is required'
- ' for Price Based shipping.'))
+ ' for Price Based shipping.')
elif max_price is not None and max_price <= min_price:
- field_errors.append((
- 'maximum_order_price',
- 'Maximum order price should be larger than the minimum.'))
+ cls.add_error(
+ errors, 'maximum_order_price',
+ 'Maximum order price should be larger than the minimum.')
else:
min_weight = cleaned_input.get('minimum_order_weight')
max_weight = cleaned_input.get('maximum_order_weight')
if min_weight is None:
- field_errors.append((
- 'minimum_order_weight',
+ cls.add_error(
+ errors, 'minimum_order_weight',
'Minimum order weight is required for'
- ' Weight Based shipping.'))
+ ' Weight Based shipping.')
elif max_weight is not None and max_weight <= min_weight:
- field_errors.append((
- 'maximum_order_weight',
- 'Maximum order weight should be larger than the minimum.'))
- for err in field_errors:
- cls.add_error(errors, field=err[0], message=err[1])
+ cls.add_error(
+ errors, 'maximum_order_weight',
+ 'Maximum order weight should be larger than the minimum.')
return cleaned_input
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index 4efc40e2ca..2f4a9496cd 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -67,7 +67,9 @@ class ShippingMethodQueryset(models.QuerySet):
"""
# If dedicated shipping zone for the country exists, we should use it
# in the first place
- qs = self.filter(shipping_zone__countries__contains=country_code)
+ qs = self.filter(
+ shipping_zone__countries__contains=country_code,
+ shipping_zone__default=False)
if not qs.exists():
# Otherwise default shipping zone should be used
qs = self.filter(shipping_zone__default=True)
diff --git a/tests/api/test_shipping_method.py b/tests/api/test_shipping_method.py
index deb8b1bc82..8b2798b6dc 100644
--- a/tests/api/test_shipping_method.py
+++ b/tests/api/test_shipping_method.py
@@ -88,7 +88,6 @@ def test_shipping_zone_query(user_api_client, shipping_zone):
}
}
"""
-
ID = graphene.Node.to_global_id('ShippingZone', shipping.id)
variables = json.dumps({'id': ID})
response = user_api_client.post(
@@ -125,22 +124,30 @@ def test_shipping_zones_query(user_api_client, shipping_zone):
def test_create_shipping_zone(admin_api_client):
query = """
- mutation createShipping{
+ mutation createShipping(
+ $id: ID!, $name: String, $default: Boolean, $countries: [String]) {
shippingZoneCreate(
- input: {name: "test shipping", countries: ["PL"]}) {
- shippingZone {
- name
- countries
- }
+ input: {name: $name, countries: $countries, default: $default})
+ {
+ shippingZone {
+ name
+ countries
+ default
}
+ }
}
"""
- response = admin_api_client.post(reverse('api'), {'query': query})
+ variables = json.dumps(
+ {'default': True, 'name': "test shipping", 'countries': ['PL']})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query}, variables)
content = get_graphql_content(response)
+
assert 'errors' not in content
data = content['data']['shippingZoneCreate']['shippingZone']
assert data['name'] == 'test shipping'
assert data['countries'] == ['PL']
+ assert data['default'] == True
def test_update_shipping_zone(admin_api_client, shipping_zone):