Skip to content

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

Explicitly assign list of countries to default shipping zone

Marcin Gębala66f7d2a82515
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 0ae355a287..62077503c2 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -5,7 +5,10 @@ from django.db import transaction
from ...core.permissions import ShippingPermissions
from ...shipping import models
from ...shipping.error_codes import ShippingErrorCode
-from ...shipping.utils import default_shipping_zone_exists
+from ...shipping.utils import (
+ default_shipping_zone_exists,
+ get_countries_without_shipping_zone,
+)
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import Decimal, WeightScalar
from ..core.types.common import ShippingError
@@ -81,7 +84,12 @@ class ShippingZoneMixin:
)
cleaned_input = super().clean_input(info, instance, data)
- default = cleaned_input.get("default")
+ cleaned_input = cls.clean_default(instance, cleaned_input)
+ return cleaned_input
+
+ @classmethod
+ def clean_default(cls, instance, data):
+ default = data.get("default")
if default:
if default_shipping_zone_exists(instance.pk):
raise ValidationError(
@@ -92,11 +100,12 @@ class ShippingZoneMixin:
)
}
)
- elif cleaned_input.get("countries"):
- cleaned_input["countries"] = []
+ else:
+ countries = get_countries_without_shipping_zone()
+ data["countries"] = countries
else:
- cleaned_input["default"] = False
- return cleaned_input
+ data["default"] = False
+ return data
@classmethod
@transaction.atomic
diff --git a/saleor/graphql/shipping/tests/test_shipping_method.py b/saleor/graphql/shipping/tests/test_shipping_method.py
index 125eda3395..09b8eaf13a 100644
--- a/saleor/graphql/shipping/tests/test_shipping_method.py
+++ b/saleor/graphql/shipping/tests/test_shipping_method.py
@@ -2,6 +2,7 @@ import graphene
import pytest
from ....shipping.error_codes import ShippingErrorCode
+from ....shipping.utils import get_countries_without_shipping_zone
from ...tests.utils import get_graphql_content
from ..types import ShippingMethodTypeEnum
@@ -169,6 +170,7 @@ def test_create_shipping_zone_without_warehouses(
def test_create_default_shipping_zone(
staff_api_client, warehouse, permission_manage_shipping
):
+ unassigned_countries = set(get_countries_without_shipping_zone())
warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
variables = {
"default": True,
@@ -184,9 +186,10 @@ def test_create_default_shipping_zone(
assert not data["shippingErrors"]
zone = data["shippingZone"]
assert zone["name"] == "test shipping"
- assert zone["countries"] == []
assert zone["warehouses"][0]["name"] == warehouse.name
assert zone["default"] is True
+ zone_countries = {c.code for c in zone["countries"]}
+ assert zone_countries == unassigned_countries
def test_create_duplicated_default_shipping_zone(
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index 7452e39e39..1864d20091 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -98,17 +98,9 @@ class ShippingMethodQueryset(models.QuerySet):
It is based on the given country code, and by shipping methods that are
applicable to the given price & weight total.
"""
- # 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,
- shipping_zone__default=False,
- currency=price.currency,
+ shipping_zone__countries__contains=country_code, currency=price.currency,
)
- if not qs.exists():
- # Otherwise default shipping zone should be used
- qs = self.filter(shipping_zone__default=True, currency=price.currency)
-
qs = qs.prefetch_related("shipping_zone").order_by("price_amount")
price_based_methods = _applicable_price_based_methods(price, qs)
weight_based_methods = _applicable_weight_based_methods(weight, qs)
diff --git a/saleor/shipping/tests/test_shipping.py b/saleor/shipping/tests/test_shipping.py
index 7576323ce4..2b785b2800 100644
--- a/saleor/shipping/tests/test_shipping.py
+++ b/saleor/shipping/tests/test_shipping.py
@@ -1,9 +1,10 @@
import pytest
+from django_countries import countries
from measurement.measures import Weight
from prices import Money
from ..models import ShippingMethod, ShippingMethodType, ShippingZone
-from ..utils import default_shipping_zone_exists
+from ..utils import default_shipping_zone_exists, get_countries_without_shipping_zone
def test_shipping_get_total(monkeypatch, shipping_zone):
@@ -126,6 +127,9 @@ def test_use_default_shipping_zone(shipping_zone):
shipping_zone.save()
default_zone = ShippingZone.objects.create(default=True, name="Default")
+ default_zone.countries = get_countries_without_shipping_zone()
+ default_zone.save(update_fields=["countries"])
+
weight_method = default_zone.shipping_methods.create(
minimum_order_weight=Weight(kg=1),
maximum_order_weight=Weight(kg=10),
@@ -142,3 +146,8 @@ def test_default_shipping_zone_exists(shipping_zone):
shipping_zone.save()
assert default_shipping_zone_exists()
assert not default_shipping_zone_exists(shipping_zone.pk)
+
+
+def test_get_countries_without_shipping_zone(shipping_zone_without_countries):
+ countries_no_shipping_zone = set(get_countries_without_shipping_zone())
+ assert {c.code for c in countries} == countries_no_shipping_zone
diff --git a/saleor/shipping/utils.py b/saleor/shipping/utils.py
index 72a0edc7ec..5ffc1c5060 100644
--- a/saleor/shipping/utils.py
+++ b/saleor/shipping/utils.py
@@ -1,5 +1,15 @@
+from django_countries import countries
+
from .models import ShippingZone
def default_shipping_zone_exists(zone_pk=None):
return ShippingZone.objects.exclude(pk=zone_pk).filter(default=True)
+
+
+def get_countries_without_shipping_zone():
+ """Return countries that are not assigned to any shipping zone."""
+ covered_countries = set()
+ for zone in ShippingZone.objects.all():
+ covered_countries.update({c.code for c in zone.countries})
+ return (country[0] for country in countries if country[0] not in covered_countries)
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index c25743c7ef..a05718c2bc 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -3,7 +3,7 @@ import uuid
from typing import Set
from django.db import models
-from django.db.models import F, Q, Sum
+from django.db.models import F, Sum
from django.db.models.functions import Coalesce
from ..account.models import Address
@@ -64,8 +64,7 @@ class StockQuerySet(models.QuerySet):
def for_country(self, country_code: str):
query_warehouse = models.Subquery(
Warehouse.objects.filter(
- Q(shipping_zones__countries__contains=country_code)
- | Q(shipping_zones__default=True)
+ shipping_zones__countries__contains=country_code
).values("pk")
)
return self.select_related("product_variant", "warehouse").filter(
diff --git a/saleor/warehouse/tests/test_stock.py b/saleor/warehouse/tests/test_stock.py
index cddabba133..ab223a1bf7 100644
--- a/saleor/warehouse/tests/test_stock.py
+++ b/saleor/warehouse/tests/test_stock.py
@@ -18,18 +18,6 @@ def test_stocks_for_country(variant_with_many_stocks):
assert COUNTRY_CODE in warehouse2.countries
-def test_stocks_for_default_shipping_zone(variant, warehouse):
- shipping_zone = warehouse.shipping_zones.get()
- shipping_zone.countries = []
- shipping_zone.default = True
- shipping_zone.save(update_fields=["countries", "default"])
- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=1)
- warehouse.refresh_from_db()
-
- stock_qs = Stock.objects.for_country("PL")
- assert stock_qs
-
-
def test_stock_for_country_does_not_exists(product, warehouse):
shipping_zone = warehouse.shipping_zones.first()
shipping_zone.countries = [COUNTRY_CODE]